-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.cpp
More file actions
242 lines (221 loc) · 7.31 KB
/
Tile.cpp
File metadata and controls
242 lines (221 loc) · 7.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "Tile.h"
Tile::Tile(int grid_x, int grid_y, float offset_X, float offset_Y, tile_states tile_state)
: gridX(grid_x), gridY(grid_y), keytimeMax(1.f), keytime(0.f), offsetX(offset_X), offsetY(offset_Y)
{
tile = sf::CircleShape(50, 6);
tile.setFillColor(sf::Color(194, 178, 128, 255));
tile.setPosition(gridX * 88.6f + offset_X, gridY * 103.5f + offset_Y);
tile.setOutlineThickness(1.f);
tile.setOutlineColor(sf::Color(169, 158, 140));
tileState = tile_state;
previousTileState = tileState;
healthTexture.loadFromFile("textures/health.png");
goldTexture.loadFromFile("textures/gold.png");
wallTexture.loadFromFile("textures/wall.png");
swordTexture.loadFromFile("textures/sword.png");
spawnTexture.loadFromFile("textures/base.png");
}
Tile::~Tile()
{
delete tileTexture;
}
const bool Tile::isClicked() const
{
return tileState == TILE_CLICKED;
}
void Tile::setTileState(tile_states state)
{
tileState = state;
}
tile_states Tile::getTileState() const
{
return previousTileState;
}
const bool Tile::isLastClicked() const{
return lastClicked;
}
void Tile::updateKeytime(const float& dt)
{
if (keytime < keytimeMax) {
keytime += 10.f * dt;
}
}
void Tile::updateTileBase(Turn current_Turn)
{
if(current_Turn == PLAYER1_TURN)
{
if (tileState == TILE_BASE || tileState == TILE_GOLD || tileState == TILE_SWORD || tileState == TILE_HEALTH) {
tileState = TILE_PLAYER1_BASE;
tileTexture = nullptr;
lastClicked = true;
}
} else
{
if (tileState == TILE_BASE || tileState == TILE_GOLD || tileState == TILE_SWORD || tileState == TILE_HEALTH) {
tileState = TILE_PLAYER2_BASE;
tileTexture = nullptr;
lastClicked = true;
}
}
}
void Tile::updateTileEnemy(Turn current_Turn)
{
if(current_Turn == PLAYER1_TURN)
{
if (tileState == TILE_PLAYER2_BASE) {
tileState = TILE_PLAYER1_BASE;
lastClicked = true;
} else if(tileState == TILE_PLAYER2_WALL)
{
tileState = TILE_PLAYER2_BASE;
tileTexture = nullptr;
lastClicked = true;
}
} else
{
if (tileState == TILE_PLAYER1_BASE) {
tileState = TILE_PLAYER2_BASE;
lastClicked = true;
} else if(tileState == TILE_PLAYER1_WALL)
{
tileState = TILE_PLAYER1_BASE;
tileTexture = nullptr;
lastClicked = true;
}
}
}
void Tile::updateTileState(Turn current_Turn)
{
updateTileBase(current_Turn);
updateTileEnemy(current_Turn);
}
void Tile::update(const sf::Vector2f& mousePos, const float &dt, Turn current_Turn)
{
updateKeytime(dt);
previousTileState = tileState;
if (tile.getGlobalBounds().contains(mousePos)) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && keytime >= keytimeMax) {
keytime = 0.f;
if(tileState == TILE_PLAYER1_SPAWN || tileState == TILE_PLAYER2_SPAWN)
{
lastClicked = true;
}
if (current_Turn == PLAYER1_TURN) {
if (tileState == TILE_BASE || tileState == TILE_GOLD || tileState == TILE_SWORD || tileState == TILE_HEALTH) {
lastClicked = true;
}
else if(tileState == TILE_PLAYER1_WALL || tileState == TILE_PLAYER1_SPAWN || tileState == TILE_PLAYER1_BASE)
{
lastClicked = true;
}
else if (tileState == TILE_PLAYER2_BASE) {
lastClicked = true;
} else if(tileState == TILE_PLAYER2_WALL)
{
lastClicked = true;
}
} else if (current_Turn == PLAYER2_TURN) {
if (tileState == TILE_BASE || tileState == TILE_GOLD || tileState == TILE_SWORD || tileState == TILE_HEALTH) {
lastClicked = true;
}
else if(tileState == TILE_PLAYER2_WALL || tileState == TILE_PLAYER2_SPAWN || tileState == TILE_PLAYER2_BASE)
{
lastClicked = true;
}
else if (tileState == TILE_PLAYER1_BASE) {
lastClicked = true;
} else if(tileState == TILE_PLAYER1_WALL)
{
lastClicked = true;
}
} else {
lastClicked = false;
}
} else if(sf::Mouse::isButtonPressed(sf::Mouse::Right) && keytime >= keytimeMax) {
keytime = 0.f;
if (current_Turn == PLAYER1_TURN && tileState == TILE_PLAYER1_BASE)
{
tileState = TILE_PLAYER1_WALL;
lastClicked = true;
} else if(current_Turn == PLAYER2_TURN && tileState == TILE_PLAYER2_BASE)
{
tileState = TILE_PLAYER2_WALL;
lastClicked = true;
} else
{
lastClicked = false;
}
}
} else {
lastClicked = false;
}
switch (tileState) {
case TILE_BASE:
{
tile.setFillColor(sf::Color(194, 178, 128, 255));
break;
}
case TILE_HEALTH: {
tile.setFillColor(sf::Color(194, 178, 128, 255));
setTileTexture(healthTexture, offsetX, offsetY);
break;
}
case TILE_GOLD: {
tile.setFillColor(sf::Color(194, 178, 128, 255));
setTileTexture(goldTexture, offsetX, offsetY);
break;
}
case TILE_SWORD: {
tile.setFillColor(sf::Color(194, 178, 128, 255));
setTileTexture(swordTexture, offsetX, offsetY);
break;
}
case TILE_PLAYER1_BASE: {
tile.setFillColor(sf::Color(202, 252, 200, 255));
break;
}
case TILE_PLAYER1_WALL: {
tile.setFillColor(sf::Color(202, 252, 200, 255));
setTileTexture(wallTexture, offsetX, offsetY);
break;
}
case TILE_PLAYER1_SPAWN: {
tile.setFillColor(sf::Color(202, 252, 200, 255));
setTileTexture(spawnTexture, offsetX, offsetY);
break;
}
case TILE_PLAYER2_BASE: {
tile.setFillColor(sf::Color(245, 146, 140, 255));
break;
}
case TILE_PLAYER2_WALL: {
tile.setFillColor(sf::Color(245, 146, 140, 255));
setTileTexture(wallTexture, offsetX, offsetY);
break;
}
case TILE_PLAYER2_SPAWN: {
tile.setFillColor(sf::Color(245, 146, 140, 200));
setTileTexture(spawnTexture, offsetX, offsetY);
break;
}
}
}
sf::Vector2f Tile::getPosition() const {
return tile.getPosition();
}
void Tile::setTileTexture(const sf::Texture& texture, float offsetX, float offsetY)
{
tileTexture = new sf::CircleShape(25);
tileTexture->setTexture(&texture);
float centerX = gridX * 88.6f + offsetX + 50;
float centerY = gridY * 103.5f + offsetY + 50;
tileTexture->setPosition(
centerX - 23.f,
centerY - 22.5f);
}
void Tile::render(sf::RenderTarget* target) {
target->draw(tile);
if (tileTexture) {
target->draw(*tileTexture);
}
}