-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.cpp
71 lines (66 loc) · 1.82 KB
/
Projectile.cpp
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
#include "Projectile.hpp"
#include "misc.hpp"
#include "Damagable.hpp"
#include "Engine/Scene.hpp"
Projectile::Projectile(engine::Scene* scene): SpriteNode(scene), m_hits(-1),
m_damage(4), m_contactHandler(this), m_dead(false), m_enemyHitsOnly(false) {
m_scene->OnContact.AddHandler(&m_contactHandler);
}
Projectile::~Projectile() {
m_scene->OnContact.RemoveHandler(&m_contactHandler);
}
void Projectile::Hit(engine::Node* node) {
if (!m_hit && (!m_enemyHitsOnly || node->GetType() == NT_ENEMY)) {
m_hits--;
m_hit=true;
PlayAnimation("hit", "default");
}
if (node->GetType() != NT_ENEMY) {
return;
}
Damagable* enemy = static_cast<Damagable*>(node);
enemy->Damage(m_damage);
}
bool Projectile::initialize(Json::Value& root) {
if (!engine::SpriteNode::initialize(root)) {
return false;
}
m_damage = root.get("damage", 4).asFloat();
m_hits = root.get("hits", -1).asInt();
m_enemyHitsOnly = root.get("enemyHitsOnly", false).asBool();
auto it = m_animations.find("end");
if (it != m_animations.end()) {
it->second->OnOver = [this](){
Delete();
};
}
it = m_animations.find("default");
if (it != m_animations.end() && it->second->GetSpeed() != 0) {
it->second->OnOver = [this](){
Delete();
};
}
return true;
}
void Projectile::OnUpdate(sf::Time interval) {
if (m_hits == 0 && !m_dead) {
m_dead=true;
GetBody()->SetActive(false);
PlayAnimation("end");
return;
}
m_hit = false;
}
void Projectile::ContactHandler::handle(b2Contact* contact, bool begin) {
void* udA = contact->GetFixtureA()->GetBody()->GetUserData();
void* udB = contact->GetFixtureB()->GetBody()->GetUserData();
engine::Node* other = nullptr;
if (udA == m_projectile) {
other = static_cast<engine::Node*>(udB);
} else if (udB == m_projectile) {
other = static_cast<engine::Node*>(udA);
}
if (other) {
m_projectile->Hit(other);
}
}