-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaserController.cpp
More file actions
102 lines (68 loc) · 3.06 KB
/
LaserController.cpp
File metadata and controls
102 lines (68 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "LaserController.h"
#include "CollisionManager.h"
#include "Game.h"
#include "Scene.h"
#include "AudioManager.h"
unsigned int LaserController::s_iHitAudioID = 0;
LaserController::LaserController() : m_xRay(glm::vec3(), glm::vec3(), false, CollisionManager::ENEMY) {
m_fLifeTime = 1.0f;
if (s_iHitAudioID == 0)
s_iHitAudioID = AudioManager::instance()->loadAudio("hit.wav");
//if (Game::instance()->getState() == Game::PLAY_STATE)
// Game::instance()->registerEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
//Game::instance()->registerEventHandler(Event::GAME_ENTER_STATE, this, &LaserController::enterStateHandler);
//Game::instance()->registerEventHandler(Event::GAME_LEAVE_STATE, this, &LaserController::leaveStateHandler);
}
LaserController::~LaserController() {
//Game::instance()->removeEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
//Game::instance()->removeEventHandler(Event::GAME_ENTER_STATE, this, &LaserController::enterStateHandler);
//Game::instance()->removeEventHandler(Event::GAME_LEAVE_STATE, this, &LaserController::leaveStateHandler);
m_xGameObject->getScene()->removeEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
}
void LaserController::init(bool enemy) {
m_xGameObject->getScene()->registerEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
m_xRay.direction = m_xGameObject->forward() * 10.0f;
m_xRay.start = m_xGameObject->getWorldPosition() - m_xRay.direction;
m_xRay.infinite = false;
m_xRay.mask = enemy ? CollisionManager::PLAYER : CollisionManager::ENEMY;
rayTest();
}
void LaserController::update(const Event& e) {
m_fLifeTime -= e.game.delta;
m_xGameObject->setScale(glm::vec3(1.0f, 1.0f, 1.0f + 4.0f * m_fLifeTime));
glm::vec3 movement = m_xGameObject->forward() * (300.0f * e.game.delta);
m_xRay.start = m_xGameObject->getWorldPosition();
m_xRay.direction = movement;
rayTest();
m_xGameObject->appendPosition(movement);
if (m_fLifeTime <= 0.0f)
m_xGameObject->destroy();
if (m_xGameObject->getPosition().y < 0.0f) {
m_xGameObject->destroy();
AudioManager::instance()->playAudio(s_iHitAudioID, m_xGameObject);
}
}
void LaserController::rayTest() {
RaycastResult result = m_xGameObject->getScene()->raycast(m_xRay);
if (result.hit) {
AudioManager::instance()->playAudio(s_iHitAudioID, result.position);
m_xGameObject->destroy();
Event e(Event::COLLISION);
e.collision.other = m_xGameObject;
e.collision.position.x = result.position.x;
e.collision.position.y = result.position.y;
e.collision.position.z = result.position.z;
e.collision.push.x = 0.0f;
e.collision.push.y = 0.0f;
e.collision.push.z = 0.0f;
result.gameObject->dispatchEvent(e);
}
}
/*void LaserController::enterStateHandler(const Event& e) {
if (e.game.state == Game::PLAY_STATE)
Game::instance()->registerEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
}
void LaserController::leaveStateHandler(const Event& e) {
if (e.game.state == Game::PLAY_STATE)
Game::instance()->removeEventHandler(Event::GAME_UPDATE, this, &LaserController::update);
}*/