-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.cpp
More file actions
238 lines (210 loc) · 8.66 KB
/
Copy pathTile.cpp
File metadata and controls
238 lines (210 loc) · 8.66 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
// Copyright 2018 Corey Selover
#include "Tile.h"
#include "Game.h"
#include "Map.h"
#include "Texture.h"
Tile::Tile(Map* map, int x, int y, TileType type)
: m_map(map),
m_x(x),
m_y(y),
m_type(type) {
setSpriteTexture();
setStatsAccordingToType();
m_northIsGrass = false;
m_southIsGrass = false;
m_eastIsGrass = false;
m_westIsGrass = false;
}
void Tile::update() { }
sf::Sprite& Tile::sprite() {
return m_sprite;
}
void Tile::setSpriteTexture() {
switch(m_type) {
case TILE_BLANK:
m_sprite.setTexture(m_map->texture("resources/textures/TEXTURE_BLANK.png")->get());
break;
case TILE_GRASS:
m_sprite.setTexture(m_map->texture("resources/textures/TEXTURE_GRASS.png")->get());
break;
case TILE_WATER:
m_sprite.setTexture(m_map->texture("resources/textures/TEXTURE_WATER.png")->get());
break;
case TILE_ROCK:
m_sprite.setTexture(m_map->texture("resources/textures/TEXTURE_ROCK.png")->get());
break;
default:
m_sprite.setTexture(m_map->texture("resources/textures/TEXTURE_BLANK.png")->get());
break;
}
m_sprite.setOrigin(sf::Vector2f(0,0));
m_sprite.setPosition(m_x * m_map->m_game->tileWidth(), m_y * m_map->m_game->tileHeight());
m_sprite.setTextureRect(sf::IntRect(2*m_map->m_game->tileWidth(), 2*m_map->m_game->tileHeight(), m_map->m_game->tileWidth(), m_map->m_game->tileHeight()));
}
void Tile::setStatsAccordingToType() {
switch(m_type) {
case TILE_BLANK:
m_maxHealth = -1;
m_movementSpeed = 0;
break;
case TILE_GRASS:
// TODO - shouldn't it be game's responsibility to give us this value in a readable way
// so we don't have to cast it?
m_maxHealth = atoi(m_map->m_game->getGameValue("tile_grass_health").c_str());
m_movementSpeed = atoi(m_map->m_game->getGameValue("tile_grass_speed").c_str());
break;
case TILE_WATER:
m_maxHealth = atoi(m_map->m_game->getGameValue("tile_water_health").c_str());
m_movementSpeed = atoi(m_map->m_game->getGameValue("tile_water_speed").c_str());
break;
case TILE_ROCK:
m_maxHealth = atoi(m_map->m_game->getGameValue("tile_rock_health").c_str());;
m_movementSpeed = atoi(m_map->m_game->getGameValue("tile_rock_speed").c_str());
break;
default:
m_maxHealth = -1;
m_movementSpeed = 0;
break;
}
m_currentHealth = m_maxHealth;
}
void Tile::updateSprite() {
// North
if(m_y > 0 && m_map->tileByGrid(m_x, m_y - 1)->getType() == m_type) {
m_northIsGrass = true;
}
// South
if(m_y < m_map->m_game->mapHeight() && m_map->tileByGrid(m_x, m_y + 1)->getType() == m_type) {
m_southIsGrass = true;
}
// West
if(m_x > 0 && m_map->tileByGrid(m_x - 1, m_y)->getType() == m_type) {
m_westIsGrass = true;
}
// East
if(m_x < m_map->m_game->mapWidth() && m_map->tileByGrid(m_x + 1, m_y)->getType() == m_type) {
m_eastIsGrass = true;
}
if(m_northIsGrass) {
/* __________Tile layout_______________
* | | | |
* | None | S | NE |
* |___________|___________|___________|
* | | | |
* | N | SE | All |
* |___________|___________|___________|
* | | | |
* | E | SW | |
* |___________|___________|___________|
* | | | |
* | W | NW | |
* |___________|___________|___________|
*/
if(m_southIsGrass || (m_eastIsGrass && m_westIsGrass)) {
// 4 sides
m_sprite.setTextureRect(sf::IntRect(2*m_map->m_game->tileWidth(),
m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else if(m_eastIsGrass) {
// north and east
m_sprite.setTextureRect(sf::IntRect(2*m_map->m_game->tileWidth(),
0,
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else if (m_westIsGrass) {
// north and west
m_sprite.setTextureRect(sf::IntRect(m_map->m_game->tileWidth(),
3*m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else {
// just north
m_sprite.setTextureRect(sf::IntRect(0,
m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
}
else if(m_southIsGrass) {
if(m_eastIsGrass && m_westIsGrass) {
// 4 sides
m_sprite.setTextureRect(sf::IntRect(2*m_map->m_game->tileWidth(),
m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else if(m_eastIsGrass) {
// south and east
m_sprite.setTextureRect(sf::IntRect(m_map->m_game->tileWidth(),
m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else if (m_westIsGrass) {
// south and west
m_sprite.setTextureRect(sf::IntRect(m_map->m_game->tileWidth(),
2*m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else {
// just south
m_sprite.setTextureRect(sf::IntRect(m_map->m_game->tileWidth(),
0,
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
}
else if(m_eastIsGrass && !m_westIsGrass) {
// just east
m_sprite.setTextureRect(sf::IntRect(0,
2*m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else if(m_westIsGrass && !m_eastIsGrass) {
// just west
m_sprite.setTextureRect(sf::IntRect(0,
3*m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
else { // East and West
// 4 sides
m_sprite.setTextureRect(sf::IntRect(2*m_map->m_game->tileWidth(),
m_map->m_game->tileHeight(),
m_map->m_game->tileWidth(),
m_map->m_game->tileHeight()));
}
}
void Tile::changeType(TileType newType) {
bool sameType = (m_type == newType);
m_type = newType;
setSpriteTexture();
if(!sameType) setStatsAccordingToType();
}
void Tile::decreaseHealth(int damage) {
m_currentHealth -= damage;
if (m_currentHealth <= 0) {
changeType(TileType::TILE_BLANK);
m_map->removeFromValidMonsterTiles(m_x, m_y);
}
}
TileType Tile::getType() {
return m_type;
}
int Tile::getSpeed() {
return m_movementSpeed;
}
sf::Vector2f Tile::coordinatesAsPixels() {
return sf::Vector2f(m_x * m_map->m_game->tileWidth(), m_y * m_map->m_game->tileHeight());
}
sf::Vector2f Tile::centerCoordsAsPixels() {
return sf::Vector2f(m_x * m_map->m_game->tileWidth() + (m_map->m_game->tileWidth() / 2),
m_y * m_map->m_game->tileHeight() + (m_map->m_game->tileHeight() / 2));
}
Tile::~Tile() { }