-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgame_event.cpp
More file actions
227 lines (188 loc) · 6.31 KB
/
Copy pathgame_event.cpp
File metadata and controls
227 lines (188 loc) · 6.31 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "game_event.hpp"
#include "game.hpp"
#include "map.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <chrono>
#include <functional>
#include <deque>
#include <memory>
#include <cassert>
#include <cmath>
namespace spiced {
Queued_Action::Queued_Action(std::function<void(const Game_State &)> t_action)
: m_action(std::move(t_action))
{ }
void Queued_Action::update(const Game_State &t_game)
{
m_action(t_game);
m_done = true;
}
bool Queued_Action::is_done() const
{
return m_done;
}
Message_Box::Message_Box(sf::String t_string, sf::Font t_font, int t_font_size,
sf::Color t_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness, Location t_loc,
const sf::Texture *t_texture)
: Game_Event(),
m_string(std::move(t_string)), m_font(std::move(t_font)), m_font_color(std::move(t_font_color)),
m_fill_color(std::move(t_fill_color)), m_outline_color(std::move(t_outline_color)),
m_outline_thickness(t_outlineThickness),
m_text(t_string, m_font, t_font_size),
m_location(std::move(t_loc)),
m_portrait(t_texture?new sf::Sprite(*t_texture):nullptr)
{
/// \todo un-hardcode this
if (m_portrait) m_portrait->setScale(2,2);
m_text.setColor(m_font_color);
}
void Message_Box::update(const Game_State &t_game)
{
if (m_start_time == 0) m_start_time = t_game.state().game_time;
if (t_game.state().game_time - m_start_time >= .5 && sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
{
m_is_done = true;
}
}
bool Message_Box::is_done() const
{
return m_is_done;
}
void Message_Box::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
const auto size = m_location.get_size(target.getView().getSize());
auto transform = getTransform();
transform.translate(m_location.get_position(target.getView().getSize()));
// apply the transform
states.transform *= transform;
sf::RectangleShape rect(size);
rect.setFillColor(m_fill_color);
rect.setOutlineColor(m_outline_color);
rect.setOutlineThickness(m_outline_thickness);
target.draw(rect, states);
target.draw(m_text, states);
if (m_portrait) {
target.draw(*m_portrait);
}
}
Selection_Menu::Selection_Menu(sf::Font t_font, int t_font_size,
sf::Color t_font_color, sf::Color t_selected_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness,
std::vector<Game_Action> t_actions, const size_t t_selection, Location t_location)
: Game_Event(),
m_font(std::move(t_font)), m_font_color(std::move(t_font_color)),
m_selected_color(std::move(t_selected_font_color)),
m_selected_cur_color(m_selected_color),
m_fill_color(std::move(t_fill_color)), m_outline_color(std::move(t_outline_color)),
m_outline_thickness(t_outlineThickness), m_actions(std::move(t_actions)),
m_current_item(t_selection),
m_location(std::move(t_location))
{
auto pos = 0.0f;
for (const auto &action : m_actions)
{
m_texts.push_back([t_font_size, t_font_color, &action, &pos, this]()
{
sf::Text txt(action.description, m_font, t_font_size);
txt.setColor(t_font_color);
txt.setPosition(15, pos);
pos += t_font_size*1.1f;
return txt;
}()
);
}
}
void Selection_Menu::update(const Game_State &t_game)
{
if (m_start_time == 0) m_start_time = t_game.state().game_time;
if (t_game.state().game_time - m_start_time >= .5 && sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
{
m_actions[m_current_item].action(t_game);
m_is_done = true;
}
if (std::remainder(t_game.state().game_time, .5f) > 0)
{
m_selected_cur_color = m_selected_color;
}
else {
m_selected_cur_color = m_font_color;
}
const auto direction = [](const sf::Vector2f &inp) {
if (inp.y == 0) {
return 0;
}
else if (inp.y > 0) {
return 1;
}
else {
return -1;
}
}(Game::get_input_direction_vector());
const auto new_item = [&]() {
if (m_last_direction != direction)
{
if (m_current_item == 0 && direction == -1) {
return m_actions.size() - 1;
}
else if (m_current_item == (m_actions.size() - 1) && direction == 1) {
return size_t(0);
}
else {
return m_current_item += direction;
}
}
else {
return m_current_item;
}
}();
m_current_item = new_item;
m_last_direction = direction;
}
bool Selection_Menu::is_done() const
{
return m_is_done;
}
void Selection_Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
const auto size = m_location.get_size(target.getView().getSize());
auto transform = getTransform();
transform.translate(m_location.get_position(target.getView().getSize()));
// apply the transform
states.transform *= transform;
sf::RectangleShape rect(size);
rect.setFillColor(m_fill_color);
rect.setOutlineColor(m_outline_color);
rect.setOutlineThickness(m_outline_thickness);
target.draw(rect, states);
auto item = 0u;
for (auto txt : m_texts)
{
if (m_current_item == item)
{
txt.setColor(m_selected_cur_color);
}
else {
txt.setColor(m_font_color);
}
target.draw(txt, states);
++item;
}
}
Object_Interaction_Menu::Object_Interaction_Menu(Object &t_obj, sf::Font t_font, int t_font_size,
sf::Color t_font_color, sf::Color t_selected_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness,
const std::vector<Object_Action> &t_actions, Location t_location)
: Selection_Menu(std::move(t_font), t_font_size, std::move(t_font_color), std::move(t_selected_font_color), std::move(t_fill_color), std::move(t_outline_color),
t_outlineThickness,
[](const std::vector<Object_Action> &t_act, Object &t_o) {
std::vector<Game_Action> res;
for (auto &act : t_act) {
auto func = act.action;
using namespace std::placeholders;
res.emplace_back(act.description, std::bind(act.action, _1, std::ref(t_o)));
}
return res;
}(t_actions, t_obj), 0, std::move(t_location))
{
}
}