Bessere Tötungsabfrage bei Sprung auf Gegner.
[hackover2013-badge-firmware.git] / badge / jumpnrun / jumpnrun.c
1 #include "jumpnrun.h"
2 #include "collision.h"
3 #include "levels.h"
4
5 #include "../ui/display.h"
6 #include "../ui/event.h"
7 #include "../ui/sprite.h"
8 #include "../util/util.h"
9
10 #include <assert.h>
11 #include <math.h>
12 #include <stddef.h>
13 #include <stdio.h>
14
15 static vec2d const gravity = { FIXED_POINT_I(0, 0), FIXED_POINT_I(0, 56) };
16 static vec2d const move_max = { FIXED_POINT_I(0, 600), FIXED_POINT_I(1, 300) };
17 static fixed_point const accel_horiz = FIXED_POINT_I(0, 50);
18 static fixed_point const accel_vert = FIXED_POINT_I(0, 167);
19 static fixed_point const drag_factor = FIXED_POINT_I(0, 854);
20 static fixed_point const speed_jump_x = FIXED_POINT_I(0, 600);
21
22 static vec2d const hacker_extent = { FIXED_INT_I(5), FIXED_INT_I(8) };
23 static vec2d const shot_spawn_inertia = { FIXED_POINT_I(1, 0), FIXED_POINT_I(0, -800) };
24
25 static badge_sprite const anim_hacker[] = {
26 { 5, 8, (uint8_t const *) "\x1c\xff\xfd\x04\x04" },
27 { 5, 8, (uint8_t const *) "\x1c\xff\x3d\xc4\x04" },
28 { 5, 8, (uint8_t const *) "\xdc\x3f\x1d\x24\xc4" },
29 { 5, 8, (uint8_t const *) "\x1c\xff\x3d\xc4\x04" }
30 /*
31 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x31" },
32 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x52" },
33 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x94\x8c" },
34 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x52" }
35 */
36 /*
37 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\xc3\x30" },
38 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x43\x51" },
39 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x35\x82" },
40 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x43\x51" }
41 */
42 /*
43 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
44 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
45 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
46 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" }
47 */
48 };
49
50 static badge_sprite const anim_sickle[] = {
51 { 3, 3, (uint8_t const *) "\x8a\x01" },
52 { 3, 3, (uint8_t const *) "\x6a" },
53 { 3, 3, (uint8_t const *) "\xa3" },
54 { 3, 3, (uint8_t const *) "\xac" }
55 };
56
57 enum {
58 JUMPNRUN_SHOT_EXTENT = 3,
59 JUMPNRUN_SHOT_TICKS_PER_FRAME = 24
60 };
61
62 static void jumpnrun_shot_despawn(jumpnrun_shot *shot) {
63 shot->inertia.x = FIXED_INT(0);
64 }
65
66 static void jumpnrun_shot_spawn(jumpnrun_shot *shot, jumpnrun_game_state const *state) {
67 shot->tick = 0;
68 shot->inertia = shot_spawn_inertia;
69
70 if(state->player.anim_direction == BADGE_BLT_MIRRORED) {
71 shot->current_box = rectangle_new((vec2d) { fixed_point_sub(rectangle_left(&state->player.current_box), FIXED_INT(JUMPNRUN_SHOT_EXTENT)), rectangle_top(&state->player.current_box) },
72 (vec2d) { FIXED_INT(JUMPNRUN_SHOT_EXTENT), FIXED_INT(JUMPNRUN_SHOT_EXTENT) });
73 shot->inertia.x = fixed_point_neg(shot->inertia.x);
74 } else {
75 shot->current_box = rectangle_new((vec2d) { rectangle_right(&state->player.current_box), rectangle_top(&state->player.current_box) },
76 (vec2d) { FIXED_INT(JUMPNRUN_SHOT_EXTENT), FIXED_INT(JUMPNRUN_SHOT_EXTENT) });
77 }
78 }
79
80 static inline int imax(int x, int y) {
81 return x < y ? y : x;
82 }
83
84 static inline fixed_point hacker_left (vec2d const *pos, jumpnrun_game_state const *state) { (void) state; return pos->x; }
85 static inline fixed_point hacker_top (vec2d const *pos, jumpnrun_game_state const *state) { (void) state; return pos->y; }
86 static inline fixed_point hacker_right (vec2d const *pos, jumpnrun_game_state const *state) { return fixed_point_add(hacker_left(pos, state), hacker_extent.x); }
87 static inline fixed_point hacker_bottom(vec2d const *pos, jumpnrun_game_state const *state) { return fixed_point_add(hacker_top (pos, state), hacker_extent.y); }
88
89 int jumpnrun_level_assert_left_side(jumpnrun_game_state const *state) {
90 static int const lmargin = 20;
91 static int const rmargin = 50;
92
93 int pos_cur = fixed_point_cast_int(state->player.current_box.pos.x);
94 int pos_rel = pos_cur - state->left;
95
96 if(pos_rel < lmargin) {
97 return imax(0, pos_cur - lmargin);
98 } else if(pos_rel > BADGE_DISPLAY_WIDTH - rmargin) {
99 return pos_cur - (BADGE_DISPLAY_WIDTH - rmargin);
100 }
101
102 return state->left;
103 }
104
105 static int jumpnrun_bsearch_tile(jumpnrun_level const *lv, jumpnrun_game_state const *state) {
106 int front = 0;
107 int len = lv->header.tile_count;
108
109 while(len > 0) {
110 int mid = front + len / 2;
111
112 if(fixed_point_lt(tile_right(&lv->tiles[mid]), FIXED_INT(state->left - JUMPNRUN_MAX_SPAWN_MARGIN))) {
113 front = mid + 1;
114 len -= len / 2 + 1;
115 } else {
116 len /= 2;
117 }
118 }
119
120 return front;
121 }
122
123 jumpnrun_tile_range jumpnrun_visible_tiles(jumpnrun_level const *lv,
124 jumpnrun_game_state const *state) {
125 jumpnrun_tile_range r;
126
127 r.first = jumpnrun_bsearch_tile(lv, state);
128
129 for(r.last = r.first;
130 r.last < lv->header.tile_count && lv->tiles[r.last].pos.x * JUMPNRUN_TILE_PIXEL_WIDTH < state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN;
131 ++r.last)
132 ;
133
134 return r;
135 }
136
137 void jumpnrun_passive_movement(vec2d *inertia)
138 {
139 *inertia = vec2d_add(*inertia, gravity);
140
141 inertia->x = fixed_point_min(fixed_point_max(fixed_point_neg(move_max.x), inertia->x), move_max.x);
142 inertia->y = fixed_point_min(fixed_point_max(fixed_point_neg(move_max.y), inertia->y), move_max.y);
143 }
144
145 static void jumpnrun_apply_movement(jumpnrun_level const *lv,
146 jumpnrun_tile_range const *tilerange,
147 jumpnrun_game_state *state,
148 vec2d *inertia_mod) {
149 switch(badge_event_current_input_state() &
150 (BADGE_EVENT_KEY_LEFT |
151 BADGE_EVENT_KEY_RIGHT)) {
152 case BADGE_EVENT_KEY_LEFT:
153 // state->player.inertia.x = state->player.touching_ground ? fixed_point_sub(state->player.inertia.x, accel_horiz) : fixed_point_neg(speed_jump_x);
154 state->player.inertia.x = fixed_point_sub(state->player.inertia.x, accel_horiz);
155 state->player.anim_direction = BADGE_BLT_MIRRORED;
156 break;
157 case BADGE_EVENT_KEY_RIGHT:
158 // state->player.inertia.x = state->player.touching_ground ? fixed_point_add(state->player.inertia.x, accel_horiz) : speed_jump_x;
159 state->player.inertia.x = fixed_point_add(state->player.inertia.x, accel_horiz);
160 state->player.anim_direction = 0;
161 break;
162 default:
163 if(state->player.touching_ground) {
164 state->player.inertia.x = fixed_point_mul(state->player.inertia.x, drag_factor);
165 } //else {
166 //state->player.inertia.x = FIXED_INT(0);
167 //}
168
169 break;
170 }
171
172 if(state->player.jumpable_frames == 0) {
173 // intentionally left blank.
174 } else if(badge_event_current_input_state() & BADGE_EVENT_KEY_BTN_A) {
175 state->player.inertia.y = fixed_point_sub(state->player.inertia.y, accel_vert);
176 --state->player.jumpable_frames;
177 } else {
178 state->player.jumpable_frames = 0;
179 }
180
181 jumpnrun_passive_movement(&state->player.inertia);
182
183 vec2d new_pos = vec2d_add(state->player.current_box.pos, state->player.inertia);
184
185 if(fixed_point_lt(new_pos.x, FIXED_INT(state->left))) {
186 new_pos.x = FIXED_INT(state->left);
187 state->player.inertia.x = FIXED_INT(0);
188 }
189
190 *inertia_mod = state->player.inertia;
191 collisions_tiles_displace(&new_pos, &state->player, lv, tilerange, inertia_mod);
192 state->player.inertia = *inertia_mod;
193
194 if(fixed_point_gt(state->player.current_box.pos.y, FIXED_INT(BADGE_DISPLAY_HEIGHT))) {
195 state->status = JUMPNRUN_DEAD;
196 }
197 }
198
199 void jumpnrun_level_tick(jumpnrun_level *lv,
200 jumpnrun_game_state *state)
201 {
202 jumpnrun_tile_range tilerange = jumpnrun_visible_tiles(lv, state);
203 vec2d inertia_mod = state->player.inertia;
204
205 jumpnrun_apply_movement(lv, &tilerange, state, &inertia_mod);
206 state->left = jumpnrun_level_assert_left_side(state);
207
208 if(state->player.tick_minor == 0) {
209 badge_framebuffer fb;
210 badge_framebuffer_clear(&fb);
211
212 for(size_t tile = tilerange.first; tile < tilerange.last; ++tile) {
213 badge_framebuffer_blt(&fb,
214 fixed_point_cast_int(tile_left(&lv->tiles[tile])) - state->left,
215 fixed_point_cast_int(tile_top (&lv->tiles[tile])),
216 &tile_type(&lv->tiles[tile])->sprite,
217 0);
218 }
219
220 for(size_t item = 0; item < lv->header.item_count; ++item) {
221 int screenpos = fixed_point_cast_int(lv->items[item].pos.x) - state->left;
222 if(screenpos > -lv->items[item].type->sprite.width &&
223 screenpos < BADGE_DISPLAY_WIDTH) {
224 rectangle item_rect = rect_from_item(&lv->items[item]);
225
226 if(rectangle_intersect(&state->player.current_box, &item_rect)) {
227 lv->items[item].type->on_collect(state);
228 }
229
230 badge_framebuffer_blt(&fb,
231 screenpos,
232 fixed_point_cast_int(lv->items[item].pos.y),
233 &lv->items[item].type->sprite,
234 0);
235 }
236 }
237
238 for(size_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
239 jumpnrun_shot *shot = &state->shots[shot_ix];
240
241 if(jumpnrun_shot_spawned(shot)) {
242 rectangle_move_rel(&shot->current_box, shot->inertia);
243 jumpnrun_passive_movement(&shot->inertia);
244 if(fixed_point_gt(rectangle_top(&shot->current_box), FIXED_INT(BADGE_DISPLAY_HEIGHT))) {
245 jumpnrun_shot_despawn(shot);
246 }
247
248 badge_framebuffer_blt(&fb,
249 fixed_point_cast_int(shot->current_box.pos.x) - state->left,
250 fixed_point_cast_int(shot->current_box.pos.y),
251 &anim_sickle[shot->tick / JUMPNRUN_SHOT_TICKS_PER_FRAME],
252 fixed_point_lt(shot->inertia.x, FIXED_INT(0)) ? BADGE_BLT_MIRRORED : 0);
253
254 ++shot->tick;
255 if(shot->tick == ARRAY_SIZE(anim_sickle) * JUMPNRUN_SHOT_TICKS_PER_FRAME) {
256 shot->tick = 0;
257 }
258 }
259 }
260
261 for(size_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
262 jumpnrun_enemy *enemy = &lv->enemies[enemy_ix];
263 jumpnrun_process_enemy(enemy, &fb, state, lv, &tilerange, &inertia_mod);
264 }
265
266 badge_framebuffer_blt(&fb,
267 fixed_point_cast_int(state->player.current_box.pos.x) - state->left,
268 fixed_point_cast_int(state->player.current_box.pos.y),
269 &anim_hacker[state->player.anim_frame],
270 state->player.anim_direction);
271
272 badge_framebuffer_flush(&fb);
273
274 if(!state->player.touching_ground) {
275 state->player.anim_frame = 2;
276 } else if(fixed_point_gt(fixed_point_abs(state->player.inertia.x), FIXED_POINT(0, 200))) {
277 state->player.anim_frame = (state->player.anim_frame + 1) % ARRAY_SIZE(anim_hacker);
278 } else {
279 state->player.anim_frame = 0;
280 }
281 } else {
282 for(size_t shot_ix = 0; shot_ix < JUMPNRUN_MAX_SHOTS; ++shot_ix) {
283 jumpnrun_shot *shot = &state->shots[shot_ix];
284
285 if(jumpnrun_shot_spawned(shot)) {
286 rectangle_move_rel(&shot->current_box, shot->inertia);
287 jumpnrun_passive_movement(&shot->inertia);
288 if(fixed_point_gt(rectangle_top(&shot->current_box), FIXED_INT(BADGE_DISPLAY_HEIGHT))) {
289 jumpnrun_shot_despawn(shot);
290 }
291
292 ++shot->tick;
293 if(shot->tick == ARRAY_SIZE(anim_sickle) * JUMPNRUN_SHOT_TICKS_PER_FRAME) {
294 shot->tick = 0;
295 }
296 }
297 }
298
299 for(size_t enemy_ix = 0; enemy_ix < lv->header.enemy_count; ++enemy_ix) {
300 jumpnrun_enemy *enemy = &lv->enemies[enemy_ix];
301 jumpnrun_process_enemy(enemy, NULL, state, lv, &tilerange, &inertia_mod);
302 }
303 }
304
305 state->player.inertia = inertia_mod;
306 ++state->player.tick_minor;
307 if(state->player.tick_minor == 3) {
308 state->player.tick_minor = 0;
309 }
310 }
311
312 uint8_t jumpnrun_play(char const *lvname) {
313 jumpnrun_level lv;
314
315 JUMPNRUN_LEVEL_LOAD(lv, lvname);
316
317 jumpnrun_game_state gs;
318 memset(&gs, 0, sizeof(gs));
319
320 gs.player.current_box = rectangle_new(lv.start_pos, hacker_extent);
321
322 while(gs.status == JUMPNRUN_PLAYING) {
323 badge_event_t ev = badge_event_wait();
324
325 switch(badge_event_type(ev)) {
326 case BADGE_EVENT_USER_INPUT:
327 {
328 uint8_t old_state = badge_event_old_input_state(ev);
329 uint8_t new_state = badge_event_new_input_state(ev);
330 uint8_t new_buttons = new_state & (old_state ^ new_state);
331
332 if((new_buttons & BADGE_EVENT_KEY_BTN_A) && gs.player.touching_ground) {
333 gs.player.jumpable_frames = 12;
334 }
335
336 if((new_buttons & BADGE_EVENT_KEY_BTN_B)) {
337 uint8_t i;
338 for(i = 0; i < JUMPNRUN_MAX_SHOTS && jumpnrun_shot_spawned(&gs.shots[i]); ++i)
339 ;
340
341 if(i < JUMPNRUN_MAX_SHOTS) {
342 jumpnrun_shot_spawn(gs.shots + i, &gs);
343 }
344 }
345
346 break;
347 }
348 case BADGE_EVENT_GAME_TICK:
349 {
350 jumpnrun_level_tick(&lv, &gs);
351 break;
352 }
353 }
354 }
355
356 return gs.status;
357 }
This page took 0.074842 seconds and 5 git commands to generate.