-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlevel.hpp
191 lines (157 loc) · 5.82 KB
/
level.hpp
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
#pragma once
#include <string>
#include <map>
#include <functional>
#include <SFML/Graphics.hpp>
#include "screen.hpp"
#include "spriteinfo.hpp"
#include "hero.hpp"
#include "soundmanager.hpp"
const std::string LEVELS_DIR = "assets/levels/";
class Level : public Screen {
friend class Hero;
public:
enum TileAppearance {
FIELD_VERTICAL,
FIELD_VERTICAL_OPENED_TOP,
FIELD_VERTICAL_OPENED_BOTTOM,
FIELD_HORIZONTAL,
FIELD_HORIZONTAL_OPENED_LEFT,
FIELD_HORIZONTAL_OPENED_RIGHT,
FIELD_UP_RIGHT_TURN,
FIELD_LEFT_UP_TURN,
FIELD_DOWN_RIGHT_TURN,
FIELD_LEFT_DOWN_TURN,
FIELD_CLOSED_TOP,
FIELD_CLOSED_RIGHT,
FIELD_CLOSED_BOTTOM,
FIELD_CLOSED_LEFT,
FIELD_OPENED_ALL_SIDES,
FENCE_TOP_RIGHT,
FENCE_BOTTOM_LEFT,
PLAYER_FACED_TOP,
PLAYER_FACED_BOTTOM,
PLAYER_FACED_LEFT,
PLAYER_FACED_RIGHT,
FINISH_OVERLAY
};
typedef std::map<TileAppearance, SpriteInfo> TileAppearanceToSpriteInfoMap_t;
Level(std::string fileName, float fieldLifetimeSeconds, std::string code, std::string message, sf::Texture &tileset, TileAppearanceToSpriteInfoMap_t tilesSpriteInfo, const sf::Font &font, sf::Sprite &background_sp, const sf::Color &fillColor, const sf::Color &outlineColor, sf::Vector2f targetSize);
virtual void processEvent(const sf::Event &event);
virtual void update();
private:
enum FieldFunction {
NORMAL,
START,
FINISH,
DEACTIVATOR,
TELEPORT
};
struct Field {
TileAppearance tileAppearance;
FieldFunction fieldFunction = NORMAL;
size_t firstVertexIndex = 0; // should be 4 of them
int durability = 1;
bool active = true;
bool dangerous = false;
std::string id = ""; // TODO draw it on field?
std::string functionData = "";
size_t fenceVerticesCount = 0; // if dangerous == true
size_t firstFenceVertexIndex = 0;
sf::Clock sinceStepped = {};
};
enum PlayerMove {
FRONT,
BACK,
ROTATE_CLOCKWISE,
ROTATE_COUNTERCLOCKWISE
};
typedef std::map<std::pair<int,int>, Field> LevelMap_t;
enum GameState {
SHOWING_INFO,
COUNTING,
PLAYING,
WON,
LOST,
AFTER_WON,
WANTS_TO_EXIT,
EXITING
} gameState = SHOWING_INFO;
bool movePlayer(PlayerMove move);
void loadMapFromFile(std::string fileName);
void closeMapBorders();
void parseRow(int index, std::string row);
void addNewField(int row, int column, Field field);
Field& getField(int row, int column);
void setFieldFunction(int row, int column, FieldFunction function);
void addFieldToVertexArray(Field &field, sf::Vector2i pos);
void modifyFieldVertices(Field &field, std::function<void (sf::Vertex &)> modifyVertex);
void stepOnField(int row, int column);
void changeGameState(GameState newState);
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
private:
enum Direction { // TODO duplicate with Player::Face?
TOP = 1,
RIGHT = 2,
BOTTOM = 4,
LEFT = 8
};
const std::map<std::string, TileAppearance> symbolToTileAppearance {
{ R"(|)" , FIELD_VERTICAL },
{ R"(-)" , FIELD_HORIZONTAL },
{ R"(\_)" , FIELD_UP_RIGHT_TURN },
{ R"(_/)" , FIELD_LEFT_UP_TURN },
{ R"(/~)" , FIELD_DOWN_RIGHT_TURN },
{ R"(~\)" , FIELD_LEFT_DOWN_TURN },
{ R"(-v-)", FIELD_CLOSED_TOP },
{ R"(-|)" , FIELD_CLOSED_RIGHT },
{ R"(-^-)", FIELD_CLOSED_BOTTOM },
{ R"(|-)" , FIELD_CLOSED_LEFT },
{ R"(+)" , FIELD_OPENED_ALL_SIDES }
};
const std::map<std::string, FieldFunction> symbolToFieldFunction {
{ "" , NORMAL },
{ "s" , START },
{ "f" , FINISH },
{ "d" , DEACTIVATOR },
{ "t" , TELEPORT }
};
const std::map<TileAppearance, int> fieldMovementInfo {
{ FIELD_VERTICAL, Direction::TOP | Direction::BOTTOM },
{ FIELD_VERTICAL_OPENED_TOP, Direction::TOP },
{ FIELD_VERTICAL_OPENED_BOTTOM, Direction::BOTTOM },
{ FIELD_HORIZONTAL, Direction::LEFT | Direction::RIGHT },
{ FIELD_HORIZONTAL_OPENED_LEFT, Direction::LEFT },
{ FIELD_HORIZONTAL_OPENED_RIGHT, Direction::RIGHT },
{ FIELD_UP_RIGHT_TURN, Direction::TOP | Direction::RIGHT },
{ FIELD_LEFT_UP_TURN, Direction::TOP | Direction::LEFT },
{ FIELD_DOWN_RIGHT_TURN, Direction::BOTTOM | Direction::RIGHT },
{ FIELD_LEFT_DOWN_TURN, Direction::BOTTOM | Direction::LEFT },
{ FIELD_CLOSED_TOP, Direction::RIGHT | Direction::BOTTOM | Direction::LEFT },
{ FIELD_CLOSED_RIGHT, Direction::TOP | Direction::BOTTOM | Direction::LEFT },
{ FIELD_CLOSED_BOTTOM, Direction::TOP | Direction::RIGHT | Direction::LEFT },
{ FIELD_CLOSED_LEFT, Direction::TOP | Direction::RIGHT | Direction::BOTTOM },
{ FIELD_OPENED_ALL_SIDES, Direction::TOP | Direction::RIGHT | Direction::BOTTOM | Direction::LEFT }
};
sf::Clock countingClock;
const float countingLength = 3.;
sf::Clock wonClock;
const float wonLength = 1.5;
sf::Clock lostClock;
const float lostLength = 1.5;
bool teleporting = false;
sf::Clock teleportClock;
const float teleportLength = 0.1;
sf::Vector2i teleportPos;
float fieldLifetimeSeconds;
std::string code;
std::string message;
sf::Texture &tileset;
TileAppearanceToSpriteInfoMap_t tilesSpriteInfo;
Hero hero;
sf::VertexArray vertices;
sf::Sprite &background_sp;
LevelMap_t map;
SoundManager& soundManager;
sf::Vector2f levelTranslation;
};