cc72db335d1294becd55803cdc077ba3082ac38a
[hackover2013-badge-firmware.git] / badge / jumpnrun / enemies.c
1 #include "enemies.h"
2
3 #include "collision.h"
4 #include "tiles.h"
5 #include "jumpnrun.h"
6
7 static badge_sprite const anim_cat[] = {
8 { 8, 5, (uint8_t const *) "\xc7\x3f\xce\x38\x11" },
9 { 8, 5, (uint8_t const *) "\xd7\x7d\xc6\x19\x25" }
10 };
11
12 static badge_sprite const anim_mushroom[] = {
13 { 7, 7, (uint8_t const *) "\x10\x0c\x9f\xcf\xc7\x40" },
14 { 7, 7, (uint8_t const *) "\x20\x18\x1e\x8f\x87\x81" },
15 { 7, 7, (uint8_t const *) "\x10\x0c\x9f\xcf\xc7\x40" },
16 { 7, 7, (uint8_t const *) "\x08\x86\xdf\xef\x67\x20" },
17 { 7, 7, (uint8_t const *) "\x04\xc3\xef\xf7\x33\x10" },
18 { 7, 7, (uint8_t const *) "\x04\xc3\xe7\xf3\x31\x10" }
19 };
20
21 static badge_sprite const anim_bunny[] = {
22 { 7, 5, (uint8_t const *) "\x60\x30\xbe\x31\x02" },
23 { 7, 5, (uint8_t const *) "\x42\x30\xbe\x31\x01" },
24 { 7, 5, (uint8_t const *) "\x42\x30\xae\x35\x01" },
25 { 7, 5, (uint8_t const *) "\x60\x30\xae\x35\x02" },
26 { 7, 5, (uint8_t const *) "\x60\x30\xbe\x31\x01" }
27 };
28
29 static badge_sprite const anim_snake[] = {
30 { 10, 6, (uint8_t const *) "\x10\x86\x83\x30\x04\x83\xa2\x07" },
31 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x0c\xc1\xa2\x07" },
32 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x82\xe0\x07" },
33 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x86\xe1\x07" }
34 };
35
36 static badge_sprite const anim_spiral[] = {
37 { 10, 10, (uint8_t const *) "\xff\x07\xd8\x6f\xa1\xb5\xf6\x1a\xe8\xbf\x00\xfe\x0f" },
38 { 10, 10, (uint8_t const *) "\xff\x07\xd8\x6f\xa1\xb5\xd6\xda\x69\xb0\x7f\x02\x0c" },
39 { 10, 10, (uint8_t const *) "\xff\x07\xd8\x6f\xa1\xb5\xd6\x5a\x6a\xaf\x81\xfe\x0b" },
40 { 10, 10, (uint8_t const *) "\xff\x07\xd8\x4f\x61\x35\xd5\x55\x54\x5f\x01\xfd\x07" },
41 { 10, 10, (uint8_t const *) "\xff\x07\xd0\x7f\x81\xf5\xd6\x5a\x68\xbf\x01\xfe\x0f" },
42 { 10, 10, (uint8_t const *) "\x03\xe4\xdf\x60\xb9\xb5\xd6\x5a\x68\xbf\x01\xfe\x0f" },
43 { 10, 10, (uint8_t const *) "\xfd\x17\x58\x6f\xa5\xb5\xd6\x5a\x68\xbf\x01\xfe\x0f" },
44 { 10, 10, (uint8_t const *) "\xfe\x0b\xa8\xaf\xa2\xba\xca\x6a\x28\xbf\x01\xfe\x0f" }
45 };
46
47 static badge_sprite const anim_rotor[] = {
48 { 9, 9, (uint8_t const *) "\x00\x00\x00\x00\xf0\xe1\x42\x0c\x18\x70\x00" },
49 { 9, 9, (uint8_t const *) "\x1c\x30\x60\x80\xf0\xe1\x40\x00\x00\x00\x00" },
50 { 9, 9, (uint8_t const *) "\x1c\x30\x60\x84\x0e\x1f\x00\x00\x00\x00\x00" },
51 { 9, 9, (uint8_t const *) "\x00\x00\x00\x04\x0e\x1f\x02\x0c\x18\x70\x00" }
52 };
53
54 static badge_sprite const anim_dog[] = {
55 { 8, 5, (uint8_t const *) "\xc1\xf8\xc6\xb8\x08" },
56 { 8, 5, (uint8_t const *) "\xc1\xba\xce\x99\x0c" },
57 { 8, 5, (uint8_t const *) "\xc1\xf8\xc6\xb8\x10" },
58 { 8, 5, (uint8_t const *) "\xc1\xf8\xc6\xb8\x20" }
59 };
60
61 static badge_sprite const anim_giraffe[] = {
62 { 8, 10, (uint8_t const *) "\x00\x10\x60\x80\xff\xc1\x00\x03\x3c\x08" },
63 { 8, 10, (uint8_t const *) "\x00\x10\x60\xa0\x7f\xc1\x00\x03\x1c\x88" },
64 { 9, 10, (uint8_t const *) "\x00\x10\x68\x90\x3f\xc1\x00\x03\x0c\x48\x00\x02" },
65 { 8, 10, (uint8_t const *) "\x00\x10\x60\xb0\x3f\xc1\x00\x03\x0c\xc8" },
66 { 8, 10, (uint8_t const *) "\x08\x30\xc0\x81\xfc\xc0\x00\x03\x3c\x08" },
67 { 8, 10, (uint8_t const *) "\x00\x10\x60\x80\xff\xc1\x00\x03\x3c\x08" }
68 };
69
70 static badge_sprite const anim_bird[] = {
71 { 9, 7, (uint8_t const *) "\x10\x0c\x05\xf7\x4a\xa9\x58\x10" },
72 { 9, 7, (uint8_t const *) "\x10\x0c\x05\x87\x47\xa2\x70\x10" }
73 /*
74 { 10, 8, (uint8_t const *) "\x10\x10\x18\x14\x38\x2e\x29\x29\x2e\x10" },
75 { 10, 8, (uint8_t const *) "\x10\x10\x18\x14\x38\x78\xa8\xa8\x78\x10" }
76 */
77 };
78
79 static void enemy_animation_advance(jumpnrun_enemy *enemy) {
80 ++enemy->base.tick_minor;
81 if(enemy->base.tick_minor == enemy->type->animation_ticks_per_frame) {
82 enemy->base.tick_minor = 0;
83
84 ++enemy->base.anim_frame;
85 if(enemy->base.anim_frame >= enemy->type->animation_length) {
86 enemy->base.anim_frame = 0;
87 }
88 }
89 }
90
91 void jumpnrun_process_enemy(jumpnrun_enemy *self,
92 badge_framebuffer *fb,
93 struct jumpnrun_game_state *state,
94 struct jumpnrun_level *lv,
95 struct jumpnrun_tile_range const *visible_tiles,
96 vec2d *player_inertia_mod) {
97 int const spawn_margin = 1 + self->type->animation_frames[self->base.anim_frame].width;
98
99 if(self->flags & JUMPNRUN_ENEMY_SPAWNED) {
100 if(fixed_point_lt(rectangle_left(enemy_box(self)), FIXED_POINT(state->left - spawn_margin, 0)) ||
101 fixed_point_gt(rectangle_left(enemy_box(self)), FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 0)) ||
102 fixed_point_gt(rectangle_top (enemy_box(self)), FIXED_POINT(BADGE_DISPLAY_HEIGHT , 0))) {
103 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
104 } else {
105 self->type->move_tick(self, state, lv, visible_tiles, player_inertia_mod);
106 self->type->collision_shots(self, state);
107
108 if(fb) {
109 badge_framebuffer_blt(fb,
110 fixed_point_cast_int(rectangle_left(enemy_box(self))) - state->left,
111 fixed_point_cast_int(rectangle_top (enemy_box(self))),
112 enemy_sprite(self),
113 enemy_render_flags(self));
114 }
115 }
116 } else if(self->flags & JUMPNRUN_ENEMY_UNAVAILABLE) {
117 if(state->left > fixed_point_cast_int(self->spawn_pos.x) + spawn_margin ||
118 state->left + BADGE_DISPLAY_WIDTH + spawn_margin < fixed_point_cast_int(self->spawn_pos.x)) {
119 self->flags &= ~JUMPNRUN_ENEMY_UNAVAILABLE;
120 }
121 } else if((fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left - spawn_margin, 0)) &&
122 fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left - spawn_margin / 2, 0))) ||
123 (fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 0)) &&
124 fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH, 0)))) {
125 // enemy unspawned, available and in spawn zone.
126 self->flags = JUMPNRUN_ENEMY_SPAWNED | JUMPNRUN_ENEMY_UNAVAILABLE;
127 self->base.current_box = rectangle_new(self->spawn_pos, self->type->extent);
128 self->base.inertia = self->type->spawn_inertia;
129 self->base.anim_frame = 0;
130 self->base.tick_minor = 0;
131 self->base.touching_ground = false;
132 self->base.jumpable_frames = 0;
133 }
134 }
135
136 void enemy_collision_tiles_bounce_horiz(jumpnrun_enemy *self,
137 vec2d *desired_position,
138 jumpnrun_level *lv,
139 jumpnrun_tile_range const *visible_tiles) {
140 vec2d inertia_mod = self->base.inertia;
141
142 bool killed = collisions_tiles_displace(desired_position,
143 &self->base,
144 lv,
145 visible_tiles,
146 &inertia_mod);
147
148 if(killed) {
149 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
150 }
151
152 if(fixed_point_ne(inertia_mod.x, self->base.inertia.x)) {
153 self->base.inertia.x = fixed_point_neg(self->base.inertia.x);
154 }
155 }
156
157 void enemy_collision_player_deadly(struct jumpnrun_enemy *self,
158 struct jumpnrun_game_state *state,
159 vec2d *player_inertia_mod) {
160 (void) player_inertia_mod;
161
162 rectangle rect_self = enemy_hitbox(self);
163
164 if(rectangle_intersect(&rect_self, &state->player.current_box)) {
165 state->status = JUMPNRUN_DEAD;
166 }
167 }
168
169 void enemy_collision_player_jumpable(jumpnrun_enemy *self,
170 jumpnrun_game_state *state,
171 vec2d *player_inertia_mod)
172 {
173 rectangle rect_self = enemy_hitbox(self);
174
175 if(rectangle_intersect(&rect_self, &state->player.current_box)) {
176 if(fixed_point_lt(rectangle_top(&state->player.current_box), rectangle_top(&rect_self)) &&
177 fixed_point_gt(state->player.inertia.y, FIXED_POINT(0, 0)))
178 {
179 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
180 player_inertia_mod->y = FIXED_POINT(0, -250);
181 state->player.jumpable_frames = 12;
182 } else {
183 state->status = JUMPNRUN_DEAD;
184 }
185 }
186 }
187
188 void enemy_collision_tiles_pass_through(struct jumpnrun_enemy *self,
189 vec2d *desired_position,
190 struct jumpnrun_level *lv,
191 struct jumpnrun_tile_range const *visible_tiles) {
192 (void) self;
193 (void) desired_position;
194 (void) lv;
195 (void) visible_tiles;
196 return;
197 }
198
199 void enemy_collision_shots_die(struct jumpnrun_enemy *self,
200 struct jumpnrun_game_state *state) {
201 rectangle rect_self = enemy_hitbox(self);
202
203 for(uint8_t i = 0; i < JUMPNRUN_MAX_SHOTS; ++i) {
204 jumpnrun_shot *shot = &state->shots[i];
205
206 if(jumpnrun_shot_spawned(shot)) {
207 if(rectangle_intersect(&rect_self, &shot->current_box)) {
208 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
209 jumpnrun_shot_despawn(shot);
210 }
211 }
212 }
213 }
214
215 void enemy_collision_shots_bounce(struct jumpnrun_enemy *self,
216 struct jumpnrun_game_state *state) {
217 rectangle rect_self = enemy_hitbox(self);
218
219 for(uint8_t i = 0; i < JUMPNRUN_MAX_SHOTS; ++i) {
220 jumpnrun_shot *shot = &state->shots[i];
221
222 if(jumpnrun_shot_spawned(shot)) {
223 if(rectangle_intersect(&rect_self, &shot->current_box)) {
224 if(fixed_point_gt(shot->inertia.x, FIXED_INT(0))) {
225 rectangle_move_to_x(&shot->current_box, fixed_point_sub(rectangle_left(&rect_self), rectangle_width(&shot->current_box)));
226 } else {
227 rectangle_move_to_x(&shot->current_box, rectangle_right(&rect_self));
228 }
229
230 shot->inertia.x = fixed_point_neg(shot->inertia.x);
231 }
232 }
233 }
234 }
235
236 void enemy_collision_shots_dontcare(struct jumpnrun_enemy *self,
237 struct jumpnrun_game_state *state) {
238 (void) self;
239 (void) state;
240 }
241
242 void enemy_tick_straight_ahead(jumpnrun_enemy *self,
243 jumpnrun_game_state *state,
244 jumpnrun_level *lv,
245 jumpnrun_tile_range const *visible_tiles,
246 vec2d *player_inertia_mod) {
247 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
248
249 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
250 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
251 return;
252 }
253
254 jumpnrun_passive_movement(&self->base.inertia);
255
256 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
257 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
258 self->type->collision_player(self, state, player_inertia_mod);
259 rectangle_move_to(&self->base.current_box, new_pos);
260
261 enemy_animation_advance(self);
262 }
263
264 void enemy_tick_straight_follow(jumpnrun_enemy *self,
265 jumpnrun_game_state *state,
266 jumpnrun_level *lv,
267 jumpnrun_tile_range const *visible_tiles,
268 vec2d *player_inertia_mod) {
269 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
270
271 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
272 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
273 return;
274 }
275
276 jumpnrun_passive_movement(&self->base.inertia);
277
278 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
279 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
280 self->type->collision_player(self, state, player_inertia_mod);
281 rectangle_move_to(&self->base.current_box, new_pos);
282
283 if(fixed_point_le(rectangle_mid_x(&state->player.current_box), rectangle_mid_x(enemy_box(self)))) {
284 self->base.inertia.x = self->type->spawn_inertia.x;
285 } else {
286 self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
287 }
288
289
290 enemy_animation_advance(self);
291 }
292
293 void enemy_tick_swing_up_and_down(struct jumpnrun_enemy *self,
294 struct jumpnrun_game_state *state,
295 struct jumpnrun_level *lv,
296 struct jumpnrun_tile_range const *visible_tiles,
297 vec2d *player_inertia_mod) {
298 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
299
300 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
301 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
302 return;
303 }
304
305 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
306 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
307 self->type->collision_player(self, state, player_inertia_mod);
308 rectangle_move_to(&self->base.current_box, new_pos);
309
310 self->base.inertia.y =
311 fixed_point_add(fixed_point_add(self->base.inertia.y,
312 fixed_point_div(self->type->spawn_inertia.y, FIXED_INT(3))),
313 fixed_point_mul(FIXED_POINT(0, 5),
314 fixed_point_sub(self->spawn_pos.y,
315 enemy_position(self).y)));
316
317 enemy_animation_advance(self);
318 }
319
320 void enemy_tick_stationary(struct jumpnrun_enemy *self,
321 struct jumpnrun_game_state *state,
322 struct jumpnrun_level *lv,
323 struct jumpnrun_tile_range const *visible_tiles,
324 vec2d *player_inertia_mod) {
325 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
326
327 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
328 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
329 return;
330 }
331
332 self->type->collision_tiles(self, &self->base.current_box.pos, lv, visible_tiles);
333 self->type->collision_player(self, state, player_inertia_mod);
334
335 enemy_animation_advance(self);
336 }
337
338 void enemy_tick_jumper(jumpnrun_enemy *self,
339 jumpnrun_game_state *state,
340 jumpnrun_level *lv,
341 jumpnrun_tile_range const *visible_tiles,
342 vec2d *player_inertia_mod) {
343 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
344
345 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
346 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
347 return;
348 }
349
350 jumpnrun_passive_movement(&self->base.inertia);
351
352 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
353 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
354 self->type->collision_player(self, state, player_inertia_mod);
355 rectangle_move_to(&self->base.current_box, new_pos);
356
357 if(self->base.touching_ground) {
358 self->base.inertia.y = self->type->spawn_inertia.y;
359 }
360
361 enemy_animation_advance(self);
362 }
363
364 void enemy_tick_dog(jumpnrun_enemy *self,
365 jumpnrun_game_state *state,
366 jumpnrun_level *lv,
367 jumpnrun_tile_range const *visible_tiles,
368 vec2d *player_inertia_mod) {
369 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
370
371 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
372 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
373 return;
374 }
375
376 jumpnrun_passive_movement(&self->base.inertia);
377
378 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
379 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
380 self->type->collision_player(self, state, player_inertia_mod);
381 rectangle_move_to(&self->base.current_box, new_pos);
382
383 if(self->base.tick_minor % self->type->animation_ticks_per_frame == 0) {
384 switch(self->base.tick_minor / self->type->animation_ticks_per_frame) {
385 case 12:
386 self->base.tick_minor = 0;
387 case 0:
388 case 2:
389 case 4:
390 case 6:
391 self->base.anim_frame = 0;
392 if(self->flags & JUMPNRUN_ENEMY_FACING_RIGHT) {
393 self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
394 } else {
395 self->base.inertia.x = self->type->spawn_inertia.x;
396 }
397
398 break;
399
400 case 1:
401 case 3:
402 case 5:
403 case 7:
404 self->base.anim_frame = 1;
405 if(self->flags & JUMPNRUN_ENEMY_FACING_RIGHT) {
406 self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
407 } else {
408 self->base.inertia.x = self->type->spawn_inertia.x;
409 }
410 break;
411
412 case 8:
413 case 10:
414 self->base.anim_frame = 2;
415 self->base.inertia.x = FIXED_INT(0);
416 break;
417
418 case 9:
419 case 11:
420 self->base.anim_frame = 3;
421 self->base.inertia.x = FIXED_INT(0);
422 break;
423 }
424 }
425
426 ++self->base.tick_minor;
427 if (fixed_point_lt(self->base.inertia.x, FIXED_INT(0))) { self->flags &= ~JUMPNRUN_ENEMY_FACING_RIGHT; }
428 else if(fixed_point_ne(self->base.inertia.x, FIXED_INT(0))) { self->flags |= JUMPNRUN_ENEMY_FACING_RIGHT; }
429 }
430
431 void enemy_tick_giraffe(jumpnrun_enemy *self,
432 jumpnrun_game_state *state,
433 jumpnrun_level *lv,
434 jumpnrun_tile_range const *visible_tiles,
435 vec2d *player_inertia_mod) {
436 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
437
438 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
439 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
440 return;
441 }
442
443 bool was_on_ground = self->base.touching_ground;
444
445 jumpnrun_passive_movement(&self->base.inertia);
446
447 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
448 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
449 self->type->collision_player(self, state, player_inertia_mod);
450 rectangle_move_to(&self->base.current_box, new_pos);
451
452 if(self->base.touching_ground) {
453 if(was_on_ground) {
454 enemy_animation_advance(self);
455 if(self->base.anim_frame == 0) {
456 self->base.inertia = self->type->spawn_inertia;
457
458 if(fixed_point_gt(rectangle_mid_x(&state->player.current_box), rectangle_mid_x(enemy_box(self)))) {
459 self->base.inertia.x = fixed_point_neg(self->base.inertia.x);
460 }
461 }
462 } else {
463 self->base.tick_minor = 0;
464 self->base.anim_frame = 3;
465 self->base.inertia.x = FIXED_INT(0);
466 }
467 } else if(was_on_ground) {
468 self->base.tick_minor = 0;
469 self->base.anim_frame = 1;
470 } else if(self->base.anim_frame == 1) {
471 enemy_animation_advance(self);
472 }
473
474 ++self->base.tick_minor;
475 if (fixed_point_lt(self->base.inertia.x, FIXED_INT(0))) { self->flags &= ~JUMPNRUN_ENEMY_FACING_RIGHT; }
476 else if(fixed_point_ne(self->base.inertia.x, FIXED_INT(0))) { self->flags |= JUMPNRUN_ENEMY_FACING_RIGHT; }
477 }
478
479 void enemy_tick_fly_straight(struct jumpnrun_enemy *self,
480 struct jumpnrun_game_state *state,
481 struct jumpnrun_level *lv,
482 struct jumpnrun_tile_range const *visible_tiles,
483 vec2d *player_inertia_mod) {
484 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
485
486 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
487 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
488 return;
489 }
490
491 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
492 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
493 self->type->collision_player(self, state, player_inertia_mod);
494 rectangle_move_to(&self->base.current_box, new_pos);
495
496 enemy_animation_advance(self);
497 }
498
499 void enemy_tick_fly_straight_and_dip(struct jumpnrun_enemy *self,
500 struct jumpnrun_game_state *state,
501 struct jumpnrun_level *lv,
502 struct jumpnrun_tile_range const *visible_tiles,
503 vec2d *player_inertia_mod) {
504 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
505
506 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
507 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
508 return;
509 }
510
511 if(fixed_point_lt(fixed_point_abs(fixed_point_sub(enemy_position(self).x,
512 state->player.current_box.pos.x)),
513 FIXED_INT(20))) {
514 self->flags |= JUMPNRUN_ENEMY_EVENT_TRIGGER1;
515 }
516
517 if(self->flags & JUMPNRUN_ENEMY_EVENT_TRIGGER1) {
518 self->base.inertia.y =
519 fixed_point_add(fixed_point_add(self->base.inertia.y,
520 fixed_point_div(self->type->spawn_inertia.y, FIXED_INT(3))),
521 fixed_point_mul(FIXED_POINT(0, 5),
522 fixed_point_sub(self->spawn_pos.y,
523 enemy_position(self).y)));
524 } else {
525 self->base.inertia.y = FIXED_INT(0);
526 }
527
528 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);;
529 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
530 self->type->collision_player(self, state, player_inertia_mod);
531 rectangle_move_to(&self->base.current_box, new_pos);
532
533 enemy_animation_advance(self);
534 }
535
536 jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = {
537 {
538 .animation_ticks_per_frame = 18,
539 .animation_length = ARRAY_SIZE(anim_cat),
540 .animation_frames = anim_cat,
541 .extent = { FIXED_INT_I(8), FIXED_INT_I(5) },
542 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(2) },
543 { FIXED_INT_I(6), FIXED_INT_I(3) } },
544 .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_INT_I(0) },
545 .collision_tiles = enemy_collision_tiles_bounce_horiz,
546 .collision_player = enemy_collision_player_jumpable,
547 .collision_shots = enemy_collision_shots_die,
548 .move_tick = enemy_tick_straight_ahead
549 }, {
550 .animation_ticks_per_frame = 12,
551 .animation_length = ARRAY_SIZE(anim_mushroom),
552 .animation_frames = anim_mushroom,
553 .extent = { FIXED_INT_I(7), FIXED_INT_I(7) },
554 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
555 { FIXED_INT_I(5), FIXED_INT_I(4) } },
556 .spawn_inertia = { FIXED_POINT_I(0, -50), FIXED_INT_I(0) },
557 .collision_tiles = enemy_collision_tiles_bounce_horiz,
558 .collision_player = enemy_collision_player_jumpable,
559 .collision_shots = enemy_collision_shots_die,
560 .move_tick = enemy_tick_straight_follow
561 }, {
562 .animation_ticks_per_frame = 9,
563 .animation_length = ARRAY_SIZE(anim_bunny),
564 .animation_frames = anim_bunny,
565 .extent = { FIXED_INT_I(7), FIXED_INT_I(5) },
566 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(2) },
567 { FIXED_INT_I(5), FIXED_INT_I(3) } },
568 .spawn_inertia = { FIXED_POINT_I(0, -80), FIXED_POINT_I(0, -800) },
569 .collision_tiles = enemy_collision_tiles_bounce_horiz,
570 .collision_player = enemy_collision_player_jumpable,
571 .collision_shots = enemy_collision_shots_die,
572 .move_tick = enemy_tick_jumper
573 }, {
574 .animation_ticks_per_frame = 6,
575 .animation_length = ARRAY_SIZE(anim_snake),
576 .animation_frames = anim_snake,
577 .extent = { FIXED_INT_I(10), FIXED_INT_I(6) },
578 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(4) },
579 { FIXED_INT_I(8), FIXED_INT_I(2) } },
580 .spawn_inertia = { FIXED_POINT_I(0, -150), FIXED_INT_I(0) },
581 .collision_tiles = enemy_collision_tiles_bounce_horiz,
582 .collision_player = enemy_collision_player_jumpable,
583 .collision_shots = enemy_collision_shots_die,
584 .move_tick = enemy_tick_straight_ahead
585 }, {
586 .animation_ticks_per_frame = 6,
587 .animation_length = ARRAY_SIZE(anim_spiral),
588 .animation_frames = anim_spiral,
589 .extent = { FIXED_INT_I(10), FIXED_INT_I(10) },
590 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
591 { FIXED_INT_I(8), FIXED_INT_I(8) } },
592 .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, -200) },
593 .collision_tiles = enemy_collision_tiles_pass_through,
594 .collision_player = enemy_collision_player_deadly,
595 .collision_shots = enemy_collision_shots_dontcare,
596 .move_tick = enemy_tick_swing_up_and_down
597 }, {
598 .animation_ticks_per_frame = 5,
599 .animation_length = ARRAY_SIZE(anim_rotor),
600 .animation_frames = anim_rotor,
601 .extent = { FIXED_INT_I(9), FIXED_INT_I(9) },
602 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
603 { FIXED_INT_I(7), FIXED_INT_I(7) } },
604 .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, 0) },
605 .collision_tiles = enemy_collision_tiles_pass_through,
606 .collision_player = enemy_collision_player_deadly,
607 .collision_shots = enemy_collision_shots_dontcare,
608 .move_tick = enemy_tick_stationary
609 }, {
610 .animation_ticks_per_frame = 18,
611 .animation_length = ARRAY_SIZE(anim_dog),
612 .animation_frames = anim_dog,
613 .extent = { FIXED_INT_I(8), FIXED_INT_I(5) },
614 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
615 { FIXED_INT_I(6), FIXED_INT_I(4) } },
616 .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_POINT_I(0, 0) },
617 .collision_tiles = enemy_collision_tiles_bounce_horiz,
618 .collision_player = enemy_collision_player_jumpable,
619 .collision_shots = enemy_collision_shots_die,
620 .move_tick = enemy_tick_dog
621 }, {
622 .animation_ticks_per_frame = 36,
623 .animation_length = ARRAY_SIZE(anim_giraffe),
624 .animation_frames = anim_giraffe,
625 .extent = { FIXED_INT_I(7), FIXED_INT_I(10) },
626 .hitbox = { { FIXED_INT_I(2), FIXED_INT_I(1) },
627 { FIXED_INT_I(4), FIXED_INT_I(9) } },
628 .spawn_inertia = { FIXED_POINT_I(0, -150), FIXED_POINT_I(-1, -200) },
629 .collision_tiles = enemy_collision_tiles_bounce_horiz,
630 .collision_player = enemy_collision_player_jumpable,
631 .collision_shots = enemy_collision_shots_bounce,
632 .move_tick = enemy_tick_giraffe
633 }, {
634 .animation_ticks_per_frame = 24,
635 .animation_length = ARRAY_SIZE(anim_bird),
636 .animation_frames = anim_bird,
637 .extent = { FIXED_INT_I(9), FIXED_INT_I(7) },
638 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) },
639 { FIXED_INT_I(7), FIXED_INT_I(3) } },
640 .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_POINT_I(0, -150) },
641 .collision_tiles = enemy_collision_tiles_bounce_horiz,
642 .collision_player = enemy_collision_player_jumpable,
643 .collision_shots = enemy_collision_shots_die,
644 .move_tick = enemy_tick_swing_up_and_down
645 }, {
646 .animation_ticks_per_frame = 24,
647 .animation_length = ARRAY_SIZE(anim_bird),
648 .animation_frames = anim_bird,
649 .extent = { FIXED_INT_I(9), FIXED_INT_I(7) },
650 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) },
651 { FIXED_INT_I(7), FIXED_INT_I(3) } },
652 .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_INT_I(0) },
653 .collision_tiles = enemy_collision_tiles_bounce_horiz,
654 .collision_player = enemy_collision_player_jumpable,
655 .collision_shots = enemy_collision_shots_die,
656 .move_tick = enemy_tick_fly_straight
657 }, {
658 .animation_ticks_per_frame = 24,
659 .animation_length = ARRAY_SIZE(anim_bird),
660 .animation_frames = anim_bird,
661 .extent = { FIXED_INT_I(9), FIXED_INT_I(7) },
662 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) },
663 { FIXED_INT_I(7), FIXED_INT_I(3) } },
664 .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_POINT_I(0, 200) },
665 .collision_tiles = enemy_collision_tiles_bounce_horiz,
666 .collision_player = enemy_collision_player_jumpable,
667 .collision_shots = enemy_collision_shots_die,
668 .move_tick = enemy_tick_fly_straight_and_dip
669 }
670 };
This page took 0.103509 seconds and 3 git commands to generate.