-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDamageable.cpp
More file actions
49 lines (43 loc) · 1.49 KB
/
Damageable.cpp
File metadata and controls
49 lines (43 loc) · 1.49 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
#include "Damageable.hpp"
#include <Engine/Scene.hpp>
#include <iostream>
Damageable::ContactHandler::ContactHandler(Damageable* damageable) : m_damageable(damageable) {
}
void Damageable::ContactHandler::handle(b2Contact* contact, const b2ContactImpulse* impulse) {
engine::Node* a = static_cast<engine::Node*> (contact->GetFixtureA()->GetBody()->GetUserData());
engine::Node* b = static_cast<engine::Node*> (contact->GetFixtureB()->GetBody()->GetUserData());
Damageable* damageable = nullptr;
engine::Node* other = nullptr;
if (a == m_damageable) {
damageable = static_cast<Damageable*> (a);
other = b;
} else if (b == m_damageable) {
damageable = static_cast<Damageable*> (b);
other = a;
}
if (damageable && other->GetIdentifier() != "particle") {
float force = 0;
for (int32_t i = 0; i < impulse->count; i++) {
force += impulse->normalImpulses[i];
}
force /= damageable->GetBody()->GetMass();
if (abs(force) > 0.4) {
std::cout << force << std::endl;
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
damageable->Damage(abs(force), other, worldManifold);
}
}
}
Damageable::Damageable(engine::Scene* scene) : SpriteNode(scene), m_contactHandler(this) {
m_scene->OnContactPostSolve.AddHandler(&m_contactHandler);
}
Damageable::~Damageable() {
m_scene->OnContactPostSolve.RemoveHandler(&m_contactHandler);
}
void Damageable::Damage(float damage, Node* by, const b2WorldManifold& manifold){
m_health-=damage;
if (m_health < 0){
Delete();
}
}