-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelScene.cpp
88 lines (77 loc) · 2.05 KB
/
LevelScene.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* File: LevelScene.cpp
* Author: iMer
*
* Created on 6. Dezember 2014, 03:13
*/
#include "LevelScene.hpp"
#include "LD31.hpp"
#include <iostream>
#include "Constants.hpp"
#include <Engine/Factory.hpp>
#include "Ui.hpp"
LevelScene::LevelScene(engine::Game* game) : Scene(game), m_cannon(nullptr), m_score(0), m_ammoLeft(0), m_targetsLeft(0), m_restartTimer(0) {
}
LevelScene::~LevelScene() {
}
void LevelScene::OnUpdate(sf::Time interval) {
engine::Scene::OnUpdate(interval);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
m_restartTimer += interval.asSeconds();
if (m_restartTimer > 1.5) {
Restart();
}
} else {
m_restartTimer = 0;
}
//std::cout << m_game->GetMousePosition().x << "," << m_game->GetMousePosition().y << std::endl;
}
uint8_t LevelScene::GetType() const {
return NT_LEVELSCENE;
}
bool LevelScene::initialize(Json::Value& root) {
if (!engine::Scene::initialize(root)) {
return false;
}
if (root.isMember("ui")) {
Json::Value& ui = root["ui"];
m_ui = engine::Factory::createJson<Ui, Scene*>(ui, this);
}
m_next = root.get("next", "").asString();
return true;
}
void LevelScene::AddScore(uint32_t score) {
m_score += score;
auto ui = static_cast<Ui*> (m_ui);
if (ui->GetScore()) {
std::ostringstream ss;
ss << "Score: " << static_cast<LD31*> (m_game)->GetScore() + m_score;
ui->GetScore()->SetText(ss.str());
}
}
void LevelScene::Next() {
if (m_next != "") {
auto game = static_cast<LD31*> (m_game);
game->SetScore(game->GetScore() + m_score + m_ammoLeft*5);
static_cast<LD31*> (m_game)->Load(m_next);
}
}
void LevelScene::Restart() {
static_cast<LD31*> (m_game)->Load(m_filename);
}
void LevelScene::UpdateNext() {
if (!m_ui) {
return;
}
std::ostringstream ss;
ss << "+" << m_ammoLeft * 5;
Node* bonus = m_ui->GetChildByID("bonusScore");
if (bonus->GetType() == engine::NT_TEXT) {
static_cast<engine::Text*>(bonus)->SetText(ss.str());
}
if (!m_ammoLeft || !m_targetsLeft) {
static_cast<Ui*> (m_ui)->GetNext()->SetActive(true);
} else {
static_cast<Ui*> (m_ui)->GetNext()->SetActive(false);
}
}