-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.cpp
More file actions
72 lines (69 loc) · 1.98 KB
/
Level.cpp
File metadata and controls
72 lines (69 loc) · 1.98 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
#include "Level.hpp"
#include "LD32.hpp"
#include "Slayer.hpp"
#include "Engine/Factory.hpp"
#include <Engine/Button.hpp>
#include <iostream>
#include <sstream>
#include <Engine/Text.hpp>
Level::Level(LD32* game): Scene(game), m_slayer(nullptr), m_initialized(false),
m_paused(false), m_score(0), m_enemies(0) {
}
Level::~Level() {
}
void Level::update(sf::Time interval) {
auto paused = m_ui->GetChildByID("paused");
// meh
if (!m_initialized) {
m_initialized=true;
ChangeScore(static_cast<LD32*>(m_game)->GetScore());
auto button = static_cast<engine::Button*>(paused->GetChildByID("button"));
button->OnClick = [this](engine::Button*, sf::Vector2f){
m_paused = false;
};
}
if (m_enemies == 0) {
m_ui->GetChildByID("next")->SetActive(true);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
m_game->SwitchScene(engine::Factory::create<Level>(m_next, static_cast<LD32*>(m_game)));
}
}
if (!m_game->IsFocus()) {
m_paused = true;
paused->SetActive(true);
}
if (m_paused) {
m_ui->update(interval);
} else {
paused->SetActive(false);
engine::Scene::update(interval);
}
}
void Level::ChangeScore(int amount) {
m_score+=amount;
auto text = static_cast<engine::Text*>(m_ui->GetChildByID("score"));
if (text) {
std::ostringstream ss;
ss << "Score: " << m_score;
text->SetText(ss.str());
if (m_score < 0){
text->SetColor(sf::Color(211, 47, 44));
} else {
text->SetColor(sf::Color::White);
}
}
static_cast<LD32*>(m_game)->SetScore(m_score);
}
void Level::Respawn() {
if (!m_slayer) return;
std::string f = m_slayer->GetFilename();
m_slayer->Delete();
auto m_slayer = engine::Factory::CreateChildFromFile(f, this);
m_slayer->SetPosition(m_respawnPoint.x, m_respawnPoint.y);
}
bool Level::initialize(Json::Value& root) {
if (!engine::Scene::initialize(root)) return false;
m_respawnPoint = sf::Vector2f(root["respawn"].get(0u, 100.0f).asFloat(), root["respawn"].get(1u, 100.0f).asFloat());
m_next = root.get("next", "").asString();
return true;
}