-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
78 lines (70 loc) · 2.1 KB
/
Object.cpp
File metadata and controls
78 lines (70 loc) · 2.1 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
//
// Created by iMer on 22.08.2015.
//
#include "Object.hpp"
#include "Player.hpp"
#include "misc.hpp"
#include "Level.hpp"
#include <Engine/Scene.hpp>
#include <Engine/ParticleSystem.hpp>
#include <iostream>
#include <Engine/ResourceManager.hpp>
sf::Sound *hitSound = nullptr; // yay amazing code
Object::Object(engine::Scene *scene) : SpriteNode(scene), m_contactHandler(this), m_move(true) {
m_scene->OnContactPreSolve.AddHandler(&m_contactHandler);
if (!hitSound) {
hitSound = engine::ResourceManager::instance()->MakeSound("assets/sound/hitobject.wav");
}
}
Object::~Object() {
m_scene->OnContactPreSolve.RemoveHandler(&m_contactHandler);
}
void Object::OnUpdate(sf::Time interval) {
if (m_move) {
m_body->SetLinearVelocity(
b2Vec2(0, m_scene->GetWorld()->GetGravity().y * m_scene->GetPixelMeterRatio() * interval.asSeconds()));
}
if (GetPosition().y > m_scene->GetSize().y + m_size.y * 2) {
Delete();
}
}
uint8_t Object::GetType() const {
return NT_OBJECT;
}
void Object::ContactHandler::handle(b2Contact *contact, const b2Manifold *manifold) {
void *uA = contact->GetFixtureA()->GetBody()->GetUserData();
void *uB = contact->GetFixtureB()->GetBody()->GetUserData();
Player *player = nullptr;
if (uA == m_me && static_cast<engine::Node *>(uB)->GetType() == NT_PLAYER) {
player = static_cast<Player *>(uB);
} else if (uB == m_me && static_cast<engine::Node *>(uA)->GetType() == NT_PLAYER) {
player = static_cast<Player *>(uB);
} else { return; }
if (m_me->IsRender()) {
m_me->OnHit(player);
}
contact->SetEnabled(false);
}
bool Object::initialize(Json::Value &root) {
if (!engine::SpriteNode::initialize(root)) {
return false;
}
m_points = static_cast<uint32_t>(root.get("points", 10).asInt());
m_energy = root.get("energy", 100.0).asFloat();
m_move = root.get("move", true).asBool();
return true;
}
void Object::OnHit(Player *player) {
if (GetType() == NT_OBJECT) {
hitSound->play();
}
SetRender(false);
player->OnHit();
engine::Node *death = GetChildByID("death");
if (death) {
auto p = static_cast<engine::ParticleSystem *>(death);
p->SetActive(true);
} else {
Delete();
}
}