-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.cpp
More file actions
60 lines (53 loc) · 1.63 KB
/
Projectile.cpp
File metadata and controls
60 lines (53 loc) · 1.63 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
/*
* File: Projectile.cpp
* Author: iMer
*
* Created on 6. Dezember 2014, 09:09
*/
#include "Projectile.hpp"
#include "Engine/Factory.hpp"
#include "Engine/util/math.hpp"
#include "Misc.hpp"
Projectile::ContactHandler::ContactHandler(Projectile* projectile): m_projectile(projectile){
}
void Projectile::ContactHandler::handle(b2Contact* contact, bool begin) {
if (!begin) return;
engine::Node* a = static_cast<engine::Node*> (contact->GetFixtureA()->GetBody()->GetUserData());
engine::Node* b = static_cast<engine::Node*> (contact->GetFixtureB()->GetBody()->GetUserData());
Projectile* projectile = nullptr;
engine::Node* other = nullptr;
if (a == m_projectile) {
projectile = static_cast<Projectile*> (a);
other = b;
} else if (b == m_projectile) {
projectile = static_cast<Projectile*> (b);
other = a;
}
if (projectile) {
if (projectile->IsExplode() && other->GetIdentifier() != "shockwave"){
projectile->SetExplode(true);
}
}
}
Projectile::Projectile(engine::Scene* scene): SpriteNode(scene), m_explode(false), m_doesExplode(false), m_contactHandler(this) {
m_scene->OnContact.AddHandler(&m_contactHandler);
}
Projectile::~Projectile() {
m_scene->OnContact.RemoveHandler(&m_contactHandler);
}
bool Projectile::initialize(Json::Value& root){
if (!engine::SpriteNode::initialize(root)){
return false;
}
m_doesExplode = root.get("explode", false).asBool();
return true;
}
void Projectile::OnUpdate(sf::Time interval){
if (m_explode){
m_doesExplode = m_explode = false;
auto gpos = GetGlobalPosition();
CreateExplosion(GetScene(), 2, GetGlobalPosition(), 5);
GetBody()->SetActive(false);
Delete();
}
}