summary refs log tree commit diff stats
path: root/src/mapview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mapview.cpp')
-rw-r--r--src/mapview.cpp393
1 files changed, 0 insertions, 393 deletions
diff --git a/src/mapview.cpp b/src/mapview.cpp deleted file mode 100644 index a85bb31..0000000 --- a/src/mapview.cpp +++ /dev/null
@@ -1,393 +0,0 @@
1#include "mapview.h"
2
3#define JUMP_VELOCITY(h, l) (-2 * (h) / (l))
4#define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l))
5
6// Initialize jump physics
7double jump_height = TILE_HEIGHT*4;
8double jump_length = 0.25 * FRAMES_PER_SECOND;
9//double jump_velocity = -2 * jump_height / jump_length;
10//double jump_gravity = -1 * jump_velocity / jump_length;
11double jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4, 0.30*FRAMES_PER_SECOND);
12double jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4, 0.30*FRAMES_PER_SECOND);
13double jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3, 0.20*FRAMES_PER_SECOND);
14
15MapView::MapView(Map* first, int x, int y)
16{
17 // Initialize player data
18 player = new mob_t();
19 player->x = x;
20 player->y = y;
21 player->x_vel = 0;
22 player->y_vel = 0;
23 player->y_accel = jump_gravity_short;
24 player->x_accel = 0;
25 player->w = 10;
26 player->h = 12;
27 player->onGround = false;
28 player->animFrame = 0;
29
30 bg = createTexture(GAME_WIDTH, GAME_HEIGHT);
31 chara = loadTextureFromBMP("../res/Starla.png");
32 tiles = loadTextureFromBMP("../res/tiles.png");
33
34 loadMap(first);
35}
36
37MapView::~MapView()
38{
39 destroyTexture(bg);
40 destroyTexture(chara);
41 destroyTexture(tiles);
42
43 delete player;
44}
45
46void MapView::loadMap(Map* m)
47{
48 curMap = m;
49
50 left_collisions.clear();
51 right_collisions.clear();
52 up_collisions.clear();
53 down_collisions.clear();
54
55 add_collision(-6, 0, GAME_WIDTH, left, (m->getLeftMap() == NULL) ? 1 : 2);
56 add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (m->getRightMap() == NULL) ? 1 : 2);
57
58 fillTexture(bg, NULL, 0, 0, 0);
59
60 const int* mapbuf = m->mapdata();
61
62 for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++)
63 {
64 int x = i % MAP_WIDTH;
65 int y = i / MAP_WIDTH;
66 Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
67 Rectangle src(mapbuf[i]%8*TILE_WIDTH, mapbuf[i]/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
68
69 if (mapbuf[i] > 0)
70 {
71 blitTexture(tiles, bg, &src, &dst);
72 }
73 //blitTexture(tiles, bg, &src, &dst);
74
75 if ((mapbuf[i] > 0) && (!((mapbuf[i] >= 5) && (mapbuf[i] <= 7))))
76 {
77 //if ((x != 0) && (mapbuf[i-1] != 'X'))
78 {
79 add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0);
80 }
81
82 //if ((x != 39) && (mapbuf[i+1] != 'X'))
83 {
84 add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0);
85 }
86
87 //if ((y != 0) && (mapbuf[i-MAP_WIDTH] != 'X'))
88 {
89 add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0);
90 }
91
92 //if ((y != 23) && (mapbuf[i+MAP_WIDTH] != 'X'))
93 {
94 add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0);
95 }
96 } else if ((mapbuf[i] >= 5) && (mapbuf[i] <= 7))
97 {
98 add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 3);
99 }
100 }
101
102 Texture* font = loadTextureFromBMP("../res/font.bmp");
103 const char* map_name = m->title();
104 int start_x = (40/2) - (strlen(map_name)/2);
105 for (size_t i=0; i<strlen(map_name); i++)
106 {
107 Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8);
108 Rectangle dstRect((start_x + i)*8, 24*8, 8, 8);
109 blitTexture(font, bg, &srcRect, &dstRect);
110 }
111
112 destroyTexture(font);
113}
114
115void MapView::input(int key, int action)
116{
117 if (action == GLFW_PRESS)
118 {
119 switch (key)
120 {
121 case GLFW_KEY_LEFT:
122 holding_left = true;
123 break;
124 case GLFW_KEY_RIGHT:
125 holding_right = true;
126 break;
127 case GLFW_KEY_UP:
128 if (player->onGround)
129 {
130 player->y_vel = jump_velocity;
131 player->onGround = false;
132 holding_up = true;
133 }
134 break;
135 case GLFW_KEY_DOWN:
136 holding_down = true;
137 break;
138 }
139 } else if (action == GLFW_RELEASE)
140 {
141 switch (key)
142 {
143 case GLFW_KEY_LEFT:
144 holding_left = false;
145 if (!holding_right) player->animFrame = 1;
146 break;
147 case GLFW_KEY_RIGHT:
148 holding_right = false;
149 if (!holding_left) player->animFrame = 0;
150 break;
151 case GLFW_KEY_DOWN:
152 holding_down = false;
153 break;
154 case GLFW_KEY_UP:
155 holding_up = false;
156 break;
157 }
158 }
159}
160
161void MapView::tick()
162{
163 if (holding_left && player->x_vel >= 0)
164 {
165 player->x_vel = -2;
166 } else if (holding_right && player->x_vel <= 0)
167 {
168 player->x_vel = 2;
169 } else if (!holding_left && !holding_right) {
170 player->x_vel = 0;
171 }
172
173 player->x_vel += player->x_accel;
174 if (player->x_vel < -16) player->x_vel = -16;
175 if (player->x_vel > 16) player->x_vel = 16;
176 int playerx_next = player->x + player->x_vel;
177
178 if (holding_up)
179 {
180 player->y_vel += jump_gravity;
181 } else {
182 player->y_vel += jump_gravity_short;
183 }
184
185 if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity
186 if (player->y_vel < -16) player->y_vel = -16;
187 int playery_next = player->y + player->y_vel;
188
189 check_collisions(player, playerx_next, playery_next);
190}
191
192void MapView::render(Texture* tex)
193{
194 if (animFrame == 0)
195 {
196 if (holding_left)
197 {
198 if (player->animFrame == 3)
199 {
200 player->animFrame = 5;
201 } else {
202 player->animFrame = 3;
203 }
204 } else if (holding_right)
205 {
206 if (player->animFrame == 2)
207 {
208 player->animFrame = 4;
209 } else {
210 player->animFrame = 2;
211 }
212 }
213 }
214
215 animFrame++;
216 animFrame %= 10;
217
218 // Draw the background
219 blitTexture(bg, tex, NULL, NULL);
220
221 // Draw the player
222 Rectangle src_rect(player->animFrame * 10, 0, 10, 12);
223 Rectangle dst_rect(player->x, player->y, player->w, player->h);
224 blitTexture(chara, tex, &src_rect, &dst_rect);
225}
226
227void MapView::add_collision(int axis, int lower, int upper, direction_t dir, int type)
228{
229 //printf("added collision\n");
230 list<collision_t>::iterator it;
231
232 switch (dir)
233 {
234 case up:
235 it = up_collisions.begin();
236 for (; it!=up_collisions.end(); it++)
237 {
238 if (it->axis < axis) break;
239 }
240
241 up_collisions.insert(it, {axis, lower, upper, type});
242
243 break;
244 case down:
245 it = down_collisions.begin();
246 for (; it!=down_collisions.end(); it++)
247 {
248 if (it->axis > axis) break;
249 }
250
251 down_collisions.insert(it, {axis, lower, upper, type});
252
253 break;
254 case left:
255 it = left_collisions.begin();
256 for (; it!=left_collisions.end(); it++)
257 {
258 if (it->axis < axis) break;
259 }
260
261 left_collisions.insert(it, {axis, lower, upper, type});
262
263 break;
264 case right:
265 it = right_collisions.begin();
266 for (; it!=right_collisions.end(); it++)
267 {
268 if (it->axis > axis) break;
269 }
270
271 right_collisions.insert(it, {axis, lower, upper, type});
272
273 break;
274 }
275}
276
277void MapView::check_collisions(mob_t* mob, int x_next, int y_next)
278{
279 if (x_next < mob->x)
280 {
281 for (list<collision_t>::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++)
282 {
283 if (it->axis > mob->x) continue;
284 if (it->axis < x_next) break;
285
286 if ((mob->y+mob->h > it->lower) && (mob->y < it->upper))
287 {
288 // We have a collision!
289 if (it->type == 0)
290 {
291 x_next = it->axis;
292 mob->x_vel = 0;
293 } else if (it->type == 1)
294 {
295 x_next = GAME_WIDTH-mob->w/2;
296 } else if (it->type == 2)
297 {
298 x_next = GAME_WIDTH-mob->w/2;
299 loadMap(curMap->getLeftMap());
300 }
301
302 break;
303 }
304 }
305 } else if (x_next > mob->x)
306 {
307 for (list<collision_t>::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++)
308 {
309 if (it->axis < mob->x+mob->w) continue;
310 if (it->axis > x_next+mob->w) break;
311
312 if ((mob->y+mob->h > it->lower) && (mob->y < it->upper))
313 {
314 // We have a collision!
315 if (it->type == 0)
316 {
317 x_next = it->axis - mob->w;
318 mob->x_vel = 0;
319 } else if (it->type == 1)
320 {
321 x_next = -mob->w/2;
322 } else if (it->type == 2)
323 {
324 x_next = -mob->w/2;
325 loadMap(curMap->getRightMap());
326 }
327
328 break;
329 }
330 }
331 }
332
333 mob->x = x_next;
334
335 if (y_next < mob->y)
336 {
337 for (list<collision_t>::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++)
338 {
339 if (it->axis > mob->y) continue;
340 if (it->axis < y_next) break;
341
342 if ((mob->x+mob->w > it->lower) && (mob->x < it->upper))
343 {
344 // We have a collision!
345 if (it->type == 0)
346 {
347 y_next = it->axis;
348 mob->y_vel = 0;
349 } else if (it->type == 1)
350 {
351 y_next = GAME_HEIGHT-mob->h/2-1;
352 }
353
354 break;
355 }
356 }
357 } else if (y_next > mob->y)
358 {
359 for (list<collision_t>::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++)
360 {
361 if (it->axis < mob->y+mob->h) continue;
362 if (it->axis > y_next+mob->h) break;
363
364 if ((mob->x+mob->w > it->lower) && (mob->x < it->upper))
365 {
366 // We have a collision!
367 if (it->type == 0)
368 {
369 y_next = it->axis - mob->h;
370 mob->y_vel = 0;
371 mob->onGround = true;
372 } else if (it->type == 1)
373 {
374 y_next = 1 - mob->h/2;
375 } else if (it->type == 3)
376 {
377 if (holding_down)
378 {
379 holding_down = false;
380 } else {
381 y_next = it->axis - mob->h;
382 mob->y_vel = 0;
383 mob->onGround = true;
384 }
385 }
386
387 break;
388 }
389 }
390 }
391
392 mob->y = y_next;
393}