-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerHit.cpp
More file actions
40 lines (25 loc) · 882 Bytes
/
PlayerHit.cpp
File metadata and controls
40 lines (25 loc) · 882 Bytes
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
#include "PlayerHit.h"
#include "AudioManager.h"
#include "HUDModel.h"
unsigned int PlayerHit::s_iHitAudioID = 0;
PlayerHit::~PlayerHit() {
m_xGameObject->removeEventHandler(Event::COLLISION, this, &PlayerHit::collisionHandler);
}
void PlayerHit::init(unsigned int hp) {
m_iMaxHP = hp;
m_iHP = hp;
m_xGameObject->registerEventHandler(Event::COLLISION, this, &PlayerHit::collisionHandler);
if (s_iHitAudioID == 0)
s_iHitAudioID = AudioManager::instance()->loadAudio("hit.wav");
}
void PlayerHit::collisionHandler(const Event& e) {
if (e.collision.other->getTag() == "laser") {
m_iHP -= 10;
HUDModel::instance()->setHPFactor((float)m_iHP / (float)m_iMaxHP);
if (m_iHP == 0) {
m_xGameObject->destroy();
AudioManager::instance()->playAudio(s_iHitAudioID, m_xGameObject);
}
AudioManager::instance()->playAudio(s_iHitAudioID, m_xGameObject);
}
}