-
Notifications
You must be signed in to change notification settings - Fork 1
/
StateDemo.cpp
178 lines (141 loc) · 4.58 KB
/
StateDemo.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "StateDemo.h"
#include "StateMenu.h"
#include "DEFINITIONS.h"
#include <memory>
StateDemo::StateDemo(GameDataRef p_data) : State{ p_data }, m_data(p_data)
{
}
StateDemo::~StateDemo()
{
}
void StateDemo::ToggleFullscren()
{
this->Fullscreen = !this->Fullscreen;
this->m_data->window.close();
this->m_data->window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Androides 2020", this->Fullscreen ? sf::Style::Fullscreen : sf::Style::Default);
this->m_data->window.setFramerateLimit(60);
}
void StateDemo::Init()
{
this->m_str_backToMenu1.setFont(this->m_data->assetManager.GetFont("LodeRunner"));
this->m_str_backToMenu1.setCharacterSize(TILE_SIZE / 2);
this->m_str_backToMenu1.setString(sf::String("DEMO"));
this->m_str_backToMenu1.setFillColor(sf::Color(255, 255, 255, 255));
this->m_str_backToMenu1.setPosition(static_cast<float>(SCREEN_WIDTH) / 2 - m_str_backToMenu1.getGlobalBounds().width / 2, TILE_SIZE * 22 + TILE_SIZE / 2);
this->m_str_backToMenu2 = this->m_str_backToMenu1;
this->m_str_backToMenu2.setString(sf::String("PRESS ANY KEY TO GO BACK TO MENU"));
this->m_str_backToMenu2.setPosition(static_cast<float>(SCREEN_WIDTH) / 2 - m_str_backToMenu2.getGlobalBounds().width / 2, TILE_SIZE * 24 - TILE_SIZE / 2);
this->Start();
}
void StateDemo::Start()
{
if (m_currentLevel == 0)
{
this->m_fadeIn = std::make_unique<Transition>(m_data, "FadeIn", true);
}
else
{
this->m_fadeIn = std::make_unique<Transition>(m_data, "FadeIn", false);
}
this->m_fadeOut = std::make_unique<Transition>(m_data, "FadeOut", false);
m_androids.clear();
m_human.reset();
m_currentLevel = std::make_unique<CurrentLevel>(1);
m_map = std::make_unique<Map>(m_data, m_currentLevel->GetLevel(), m_currentLevel->GetLevelColor());
m_human = std::make_shared<HumanDemo>(m_data, m_currentLevel->GetHumanStartPosition(), m_map->GetPath(), m_currentLevel->GetLevel());
float dtoffset = 0;
for (auto startPosition : m_currentLevel->GetAndroidsStartPositions())
{
auto android = std::make_shared<Android>(m_data, startPosition, m_map->GetPath(), m_currentLevel->GetLevel(), m_human->GetHumanPosition(), dtoffset);
m_androids.push_back(android);
}
this->m_fadeIn->Start();
}
void StateDemo::HandleInput()
{
sf::Event event;
while (this->m_data->window.pollEvent(event))
{
if (sf::Event::Closed == event.type)
{
this->m_data->window.close();
}
if (event.type == sf::Event::Resized)
{
auto m_window_width = event.size.width;
auto m_window_height = event.size.height;
float new_width = ASPECT_RATIO * (float)m_window_height;
float new_height = (float)m_window_width / ASPECT_RATIO;
float offset_width = ((float)m_window_width - new_width) / 2.0f;
float offset_height = ((float)m_window_height - new_height) / 2.0f;
sf::View view = m_data->window.getDefaultView();
if ((float)m_window_width >= ASPECT_RATIO * (float)m_window_height)
{
view.setViewport(sf::FloatRect(offset_width / (float)m_window_width, 0.f, new_width / (float)m_window_width, 1.f));
}
else
{
view.setViewport(sf::FloatRect(0.f, offset_height / (float)m_window_height, 1.f, new_height / (float)m_window_height));
}
m_data->window.setView(view);
}
if (sf::Event::KeyPressed == event.type && !sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
m_fadeOut->Start();
}
}
}
void StateDemo::Update(float dt)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
ToggleFullscren();
}
if (m_human->EndDemo() && !m_fadeIn->IsInProgress() && !m_fadeOut->IsInProgress())
{
m_fadeOut->Start();
}
if (this->m_fadeIn->IsInProgress() && this->m_pause)
{
this->m_fadeIn->Update(dt);
}
if (this->m_fadeIn->IsCompleted() && this->m_pause)
{
this->m_pause = false;
}
m_map->Update();
if (!m_pause)
{
m_map->Update();
m_human->Update(dt);
for (auto const& android : m_androids)
{
android->Update(dt);
}
}
if (this->m_fadeOut->IsInProgress())
{
this->m_fadeOut->Update(dt);
}
if (this->m_fadeOut->IsCompleted())
{
this->m_data->stateMachine.AddState(StateRef(new StateMenu(m_data)), true);
}
}
void StateDemo::Draw(float dt)
{
(void)dt;
this->m_data->window.clear();
this->m_human->Draw();
for (auto const& android : m_androids)
{
android->Draw();
}
this->m_map->Draw();
// this->m_map->DrawPath();
this->m_data->window.draw(m_str_backToMenu1);
this->m_data->window.draw(m_str_backToMenu2);
this->m_fadeIn->Draw();
this->m_fadeOut->Draw();
this->m_data->window.display();
}