-
Notifications
You must be signed in to change notification settings - Fork 6
/
map.cpp
351 lines (306 loc) · 7.61 KB
/
map.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <iostream>
#include <cmath>
#include <algorithm>
#include "map.h"
Map::Map() : speed(1.2), damage(0), enemy_count(0), map(NULL), dist(NULL), sprites(std::vector<Sprite>()), doors(std::vector<Door>())
{
//Loading the map texture
SDL_Surface* map_tex = SDL_LoadBMP("map.bmp");
//Error handling for texture loading
if(!map_tex)
{
std::cerr << "Couldn't load texture file " << SDL_GetError() << std::endl;
return;
}
if(map_tex->format->BytesPerPixel != 3)
{
std::cerr << "Map must be using 8bpp format." << std::endl;
return;
}
//TODO : Fix this issue
//temporary workaround because map sizes other than 32x32 causes crashes
if(map_tex->w != 32 || map_tex->h != 32)
{
std::cerr << "Map must be 32x32." << std::endl;
return;
}
w = map_tex->w;
h = map_tex->h;
map = new char[w*h];
dist = new ushort[w*h];
std::cout<<w<<';'<<h<<"\n";
for(ushort y = 0; y < h; y++)
{
for(ushort x = 0; x < w; x++)
{
int id = y * w + x;
Uint32 pixel = get_pixel(map_tex, x, y);
if(pixel == 0) //black 0,0,0 : grass
map[id] = '0';
else if(pixel == 65280) //green 0,255,0 : bricks
map[id] = '1';
else if(pixel == 255) //blue 0,0,255 : cobblestone
map[id] = '2';
else if(pixel == 16776960) //yellow 255,255,0 : door
{
map[id] = '3';
unsigned int index = doors.size();
doors.push_back(Door());
doors.at(index).x = x;
doors.at(index).y = y;
doors.at(index).animationState = 1;
}
else if(pixel == 16711680) //red 255,0,0 : enemy
{
map[id] = ' ';
unsigned int index = sprites.size();
sprites.push_back(Sprite());
sprites.at(index).x = x + (std::rand() % 100) / 100.0;
sprites.at(index).y = y + (std::rand() % 100) / 100.0;
sprites.at(index).itex = std::rand() % 2 == 1 ? 1 : 4;
sprites.at(index).type = Enemy;
sprites.at(index).size = 500 + ((id * 50) % 100);
enemy_count++;
}
else if(pixel == 65535) //cyan 0,255,255 : flowers
{
map[id] = ' ';
for(int i = 0; i < 6; i++)
{
unsigned int index = sprites.size();
sprites.push_back(Sprite());
sprites.at(index).x = x + (std::rand() % 100) / 100.0;
sprites.at(index).y = y + (std::rand() % 100) / 100.0;
sprites.at(index).itex = 2;
sprites.at(index).type = Decoration;
sprites.at(index).size = 200 + ((id * 50) % 100);
}
}
else if(pixel == 16711935) //magenta 255,0,255 : key
{
map[id] = ' ';
unsigned int index = sprites.size();
sprites.push_back(Sprite());
sprites.at(index).x = x;
sprites.at(index).y = y;
sprites.at(index).itex = 5;
sprites.at(index).type = Key;
sprites.at(index).size = 400;
}
else //empty
map[id] = ' ';
std::cout<<get_tile(x, y);
}
std::cout<<std::endl;
}
std::cout<<"Level loaded with "<<sprites.size()<<" sprites"<<std::endl;
SDL_FreeSurface(map_tex);
update_dist_map(2, 2);
}
char Map::get_tile(ushort x, ushort y)
{
if(x >= w || y >= h)
return '0';
return map[y * w + x];
}
void Map::set_tile(ushort x, ushort y, char tile)
{
if(x > w || y > h)
return;
map[y * w + x] = tile;
}
Door Map::get_door(ushort x, ushort y)
{
unsigned int i = 0;
for(i = 0; i < doors.size() - 1; i++)
{
if(doors.at(i).x == x && doors.at(i).y == y)
break;
}
return doors.at(i);
}
//returns true if the player opened a door
bool Map::update_doors(float player_x, float player_y, float dt)
{
for(unsigned int i = doors.size(); i > 0; i--)
{
float sqr_dist = pow(player_x - doors.at(i-1).x, 2) + pow(player_y - doors.at(i-1).y, 2);
if(sqr_dist < 10)
{
doors.at(i-1).animationState -= dt;
if(doors.at(i-1).animationState < 0.01)
{
set_tile(doors.at(i-1).x, doors.at(i-1).y, ' ');
doors.erase(doors.begin() + i-1);
update_dist_map(player_x, player_y);
return true;
}
}
else
doors.at(i-1).animationState = 1;
}
return false;
}
void Map::sort_sprites(float player_x, float player_y)
{
if(sprites.size() < 2)
return;
for(unsigned int i = sprites.size() - 1; i > 0; i--)
{
sprites.at(i).sqr_dist = pow(player_x - sprites.at(i).x, 2) + pow(player_y - sprites.at(i).y, 2);
if(sprites.at(i).type == Temporary && sprites.at(i).start_time + 500 < SDL_GetTicks())
delete_sprite(i);
}
std::sort(sprites.begin(), sprites.end());
}
int Map::damage_player()
{
int amount = 0;
for(unsigned int i = 0; i < sprites.size(); i++)
{
if(sprites.at(i).type == Enemy && sprites.at(i).sqr_dist < 2)
amount += damage;
}
return amount;
}
bool Map::pickup_keys()
{
if(sprites.size() == 0)
return false;
for(unsigned int i = sprites.size() - 1; i > 0; i--)
{
if(sprites.at(i).type == Key && sprites.at(i).sqr_dist < 2)
{
delete_sprite(i);
return true;
}
}
return false;
}
void Map::animate_sprites()
{
for(unsigned int i = 0; i < sprites.size(); i++)
{
if(sprites.at(i).type == Enemy)
sprites.at(i).itex = sprites.at(i).itex == 1 ? 4 : 1;
}
}
std::vector<Sprite> const& Map::get_sprites()
{
return sprites;
}
void Map::delete_sprite(ushort id)
{
sprites.erase(sprites.begin() + id);
}
void Map::add_temp_sprite(ushort itex, float x, float y, ushort size)
{
unsigned int index = sprites.size();
sprites.push_back(Sprite());
sprites.at(index).x = x;
sprites.at(index).y = y;
sprites.at(index).itex = itex;
sprites.at(index).type = Temporary;
sprites.at(index).size = size;
}
void Map::update_sprites(float player_x, float player_y, float dt)
{
//player position
int px = ushort(player_x);
int py = ushort(player_y);
if(dist[px+py*w] != 0)
update_dist_map(px, py);
for(unsigned int i = 0; i < sprites.size(); i++)
{
if(sprites.at(i).type == Enemy)
{
ushort x = ushort(sprites.at(i).x);
ushort y = ushort(sprites.at(i).y);
int d = dist[x + y * w];
if(d > dist[x+(y-1)*w])
{
sprites.at(i).y -= speed * dt;
sprites.at(i).x += (x - sprites.at(i).x + 0.5) * speed* dt;
}
else if(d > dist[x+(y+1)*w])
{
sprites.at(i).y += speed * dt;
sprites.at(i).x += (x - sprites.at(i).x + 0.5) * speed * dt;
}
else if(d > dist[(x+1)+y*w])
{
sprites.at(i).x += speed * dt;
sprites.at(i).y += (y - sprites.at(i).y + 0.5) * speed * dt;
}
else if(d > dist[(x-1)+y*w])
{
sprites.at(i).x -= speed * dt;
sprites.at(i).y += (y - sprites.at(i).y + 0.5) * speed * dt;
}
}
else if(sprites.at(i).type == Temporary)
{
sprites.at(i).size += 15;
}
}
}
void Map::update_dist_map(ushort px, ushort py)
{
//initialize the distance map
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
dist[j * w + i] = 1000;
}
}
//all for neighbors
const int nx[4] = {0, -1, 1, 0};
const int ny[4] = {-1, 0, 0, 1};
dist[px+py*w] = 0; //sets player's distance to 0
for (int iter = 0; iter < 10; iter++)
{
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
int idx = x + y * w;
if(map[idx] == ' ')
{
//for each neighbor
for(int i = 0; i < 4; i++)
{
int tidx = idx + nx[i] + ny[i] * w;
if(map[tidx] == ' ' && dist[tidx] > dist[idx])
dist[tidx] = dist[idx] + 1;
}
}
}
}
}
/*
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
int idx = i + j * w;
std::cout<<(char)(dist[idx] > 9 ? '-' : dist[idx] + '0');
}
std::cout<<std::endl;
}
*/
}
//Gets a pixel from the texture file
Uint32 Map::get_pixel(SDL_Surface* source, ushort x, ushort y)
{
if(x >= h || y >= w)
return 0;
Uint8 *p = (Uint8 *)source->pixels + y * source->pitch + x * source->format->BytesPerPixel;
return p[0] | p[1] << 8 | p[2] << 16;
}
Map::~Map()
{
delete dist;
delete map;
std::cout<<"Map deleted"<<std::endl;
}