-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUfo.cpp
More file actions
66 lines (61 loc) · 2.03 KB
/
Ufo.cpp
File metadata and controls
66 lines (61 loc) · 2.03 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
#include "Ufo.hpp"
#include "Level.hpp"
#include <Engine/Game.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <Engine/Factory.hpp>
#include <Engine/ResourceManager.hpp>
Ufo::Ufo(engine::Scene* scene) : SpriteNode(scene) {
m_keyListener = m_scene->GetGame()->OnKeyDown.MakeHandler([this](const sf::Event::KeyEvent& e){
if (e.code == sf::Keyboard::Space) {
static_cast<Level*>(m_scene)->RemovePartRope();
}
});
m_flyingSound = engine::ResourceManager::instance()->MakeSound("assets/sound/flying.ogg");
m_flyingSound->setLoop(true);
m_flyingSound->play();
}
Ufo::~Ufo() {
m_scene->GetGame()->OnKeyDown.RemoveHandler(m_keyListener);
delete m_flyingSound;
}
void Ufo::OnUpdate(sf::Time interval) {
auto v = m_body->GetLinearVelocity();
v *= 0.95;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
v.x -= 0.13;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
v.x += 0.13;
}
m_body->SetLinearVelocity(v);
auto joint = m_body->GetJointList();
auto rope = GetChildByID("rope1");
if (joint) {
auto p = joint->joint->GetAnchorA();
auto pB = joint->joint->GetAnchorB();
float rotation = engine::util::b2AngleDeg(p, pB);
p = GetBody()->GetLocalPoint(p);
rope->SetPosition(p.x * m_scene->GetPixelMeterRatio() + getOrigin().x,
p.y * m_scene->GetPixelMeterRatio() + getOrigin().y);
rope->SetRotation(rotation);
joint = joint->next;
rope->SetActive(true);
} else {
rope->SetActive(false);
}
rope = GetChildByID("rope2");
if (joint) {
auto p = joint->joint->GetAnchorA();
auto pB = joint->joint->GetAnchorB();
float rotation = engine::util::b2AngleDeg(p, pB);
p = GetBody()->GetLocalPoint(p);
rope->SetPosition(p.x * m_scene->GetPixelMeterRatio() + getOrigin().x,
p.y * m_scene->GetPixelMeterRatio() + getOrigin().y);
rope->SetRotation(rotation);
rope->SetActive(true);
} else {
rope->SetActive(false);
}
}
void Ufo::PostDraw(sf::RenderTarget& target, sf::RenderStates states, float delta) {
}