1b3573932280d1f8491e1a1819335f56d3c97fc3
[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\x0f" },
31 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x0c\xc1\xa2\x0f" },
32 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x82\xe0\x0f" },
33 { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x86\xe1\x0f" }
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 void enemy_animation_advance(jumpnrun_enemy *enemy) {
62 ++enemy->base.tick_minor;
63 if(enemy->base.tick_minor == enemy->type->animation_ticks_per_frame) {
64 enemy->base.tick_minor = 0;
65
66 ++enemy->base.anim_frame;
67 if(enemy->base.anim_frame >= enemy->type->animation_length) {
68 enemy->base.anim_frame = 0;
69 }
70 }
71 }
72
73 void jumpnrun_process_enemy(jumpnrun_enemy *self,
74 badge_framebuffer *fb,
75 struct jumpnrun_game_state *state,
76 struct jumpnrun_level *lv,
77 struct jumpnrun_tile_range const *visible_tiles,
78 vec2d *player_inertia_mod) {
79 int const spawn_margin = 1 + self->type->animation_frames[self->base.anim_frame].width;
80
81 if(self->flags & JUMPNRUN_ENEMY_SPAWNED) {
82 if(fixed_point_lt(rectangle_left(enemy_box(self)), FIXED_POINT(state->left - spawn_margin, 0)) ||
83 fixed_point_gt(rectangle_left(enemy_box(self)), FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 0)) ||
84 fixed_point_gt(rectangle_top (enemy_box(self)), FIXED_POINT(BADGE_DISPLAY_HEIGHT , 0))) {
85 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
86 } else {
87 self->type->game_tick(self, state, lv, visible_tiles, player_inertia_mod);
88
89 if(fb) {
90 badge_framebuffer_blt(fb,
91 fixed_point_cast_int(rectangle_left(enemy_box(self))) - state->left,
92 fixed_point_cast_int(rectangle_top (enemy_box(self))),
93 enemy_sprite(self),
94 enemy_render_flags(self));
95 }
96 }
97 } else if(self->flags & JUMPNRUN_ENEMY_UNAVAILABLE) {
98 if(state->left > fixed_point_cast_int(self->spawn_pos.x) + spawn_margin ||
99 state->left + BADGE_DISPLAY_WIDTH + spawn_margin < fixed_point_cast_int(self->spawn_pos.x)) {
100 self->flags &= ~JUMPNRUN_ENEMY_UNAVAILABLE;
101 }
102 } else if((fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left - spawn_margin, 0)) &&
103 fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left - spawn_margin / 2, 0))) ||
104 (fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 0)) &&
105 fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH, 0)))) {
106 // enemy unspawned, available and in spawn zone.
107 self->flags |= JUMPNRUN_ENEMY_SPAWNED | JUMPNRUN_ENEMY_UNAVAILABLE;
108 self->base.current_box = rectangle_new(self->spawn_pos, self->type->extent);
109 self->base.inertia = self->type->spawn_inertia;
110 self->base.anim_frame = 0;
111 self->base.tick_minor = 0;
112 self->base.touching_ground = false;
113 self->base.jumpable_frames = 0;
114 }
115 }
116
117 void enemy_collision_tiles_bounce_horiz(jumpnrun_enemy *self,
118 vec2d *desired_position,
119 jumpnrun_level *lv,
120 jumpnrun_tile_range const *visible_tiles) {
121 vec2d inertia_mod = self->base.inertia;
122
123 collisions_tiles_displace(desired_position,
124 &self->base,
125 lv,
126 visible_tiles,
127 &inertia_mod);
128
129 if(fixed_point_ne(inertia_mod.x, self->base.inertia.x)) {
130 self->base.inertia.x = fixed_point_neg(self->base.inertia.x);
131 }
132 }
133
134 void enemy_collision_player_deadly(struct jumpnrun_enemy *self,
135 struct jumpnrun_game_state *state,
136 vec2d *player_inertia_mod) {
137 (void) player_inertia_mod;
138
139 rectangle rect_self = enemy_hitbox(self);
140
141 if(rectangle_intersect(&rect_self, &state->player.current_box)) {
142 state->status = JUMPNRUN_DEAD;
143 }
144 }
145
146 void enemy_collision_player_jumpable(jumpnrun_enemy *self,
147 jumpnrun_game_state *state,
148 vec2d *player_inertia_mod)
149 {
150 rectangle rect_self = enemy_hitbox(self);
151
152 if(rectangle_intersect(&rect_self, &state->player.current_box)) {
153 if(fixed_point_gt(state->player.inertia.y, FIXED_POINT(0, 0))) {
154 self->flags &= ~JUMPNRUN_ENEMY_SPAWNED;
155 player_inertia_mod->y = FIXED_POINT(0, -250);
156 state->player.jumpable_frames = 12;
157 } else {
158 state->status = JUMPNRUN_DEAD;
159 }
160 }
161 }
162
163 void enemy_collision_tiles_pass_through(struct jumpnrun_enemy *self,
164 vec2d *desired_position,
165 struct jumpnrun_level *lv,
166 struct jumpnrun_tile_range const *visible_tiles) {
167 (void) self;
168 (void) desired_position;
169 (void) lv;
170 (void) visible_tiles;
171 return;
172 }
173
174 void enemy_tick_straight_ahead(jumpnrun_enemy *self,
175 jumpnrun_game_state *state,
176 jumpnrun_level *lv,
177 jumpnrun_tile_range const *visible_tiles,
178 vec2d *player_inertia_mod) {
179 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
180
181 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
182 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
183 return;
184 }
185
186 jumpnrun_passive_movement(&self->base.inertia);
187
188 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
189 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
190 self->type->collision_player(self, state, player_inertia_mod);
191 rectangle_move_to(&self->base.current_box, new_pos);
192
193 enemy_animation_advance(self);
194 }
195
196 void enemy_tick_swing_up_and_down(struct jumpnrun_enemy *self,
197 struct jumpnrun_game_state *state,
198 struct jumpnrun_level *lv,
199 struct jumpnrun_tile_range const *visible_tiles,
200 vec2d *player_inertia_mod) {
201 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
202
203 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
204 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
205 return;
206 }
207
208 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
209 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
210 self->type->collision_player(self, state, player_inertia_mod);
211 rectangle_move_to(&self->base.current_box, new_pos);
212
213 self->base.inertia.y =
214 fixed_point_add(fixed_point_add(self->base.inertia.y,
215 fixed_point_div(self->type->spawn_inertia.y, FIXED_INT(3))),
216 fixed_point_mul(FIXED_POINT(0, 5),
217 fixed_point_sub(self->spawn_pos.y,
218 enemy_position(self).y)));
219
220 enemy_animation_advance(self);
221 }
222
223 void enemy_tick_stationary(struct jumpnrun_enemy *self,
224 struct jumpnrun_game_state *state,
225 struct jumpnrun_level *lv,
226 struct jumpnrun_tile_range const *visible_tiles,
227 vec2d *player_inertia_mod) {
228 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
229
230 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
231 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
232 return;
233 }
234
235 self->type->collision_tiles(self, &self->base.current_box.pos, lv, visible_tiles);
236 self->type->collision_player(self, state, player_inertia_mod);
237
238 enemy_animation_advance(self);
239 }
240
241 void enemy_tick_jumper(jumpnrun_enemy *self,
242 jumpnrun_game_state *state,
243 jumpnrun_level *lv,
244 jumpnrun_tile_range const *visible_tiles,
245 vec2d *player_inertia_mod) {
246 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
247
248 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
249 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
250 return;
251 }
252
253 jumpnrun_passive_movement(&self->base.inertia);
254
255 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
256 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
257 self->type->collision_player(self, state, player_inertia_mod);
258 rectangle_move_to(&self->base.current_box, new_pos);
259
260 if(self->base.touching_ground) {
261 self->base.inertia.y = self->type->spawn_inertia.y;
262 }
263
264 enemy_animation_advance(self);
265 }
266
267 void enemy_tick_dog(jumpnrun_enemy *self,
268 jumpnrun_game_state *state,
269 jumpnrun_level *lv,
270 jumpnrun_tile_range const *visible_tiles,
271 vec2d *player_inertia_mod) {
272 int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box));
273
274 if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left ||
275 screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) {
276 return;
277 }
278
279 jumpnrun_passive_movement(&self->base.inertia);
280
281 vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);
282 self->type->collision_tiles(self, &new_pos, lv, visible_tiles);
283 self->type->collision_player(self, state, player_inertia_mod);
284 rectangle_move_to(&self->base.current_box, new_pos);
285
286 if(self->base.tick_minor % self->type->animation_ticks_per_frame == 0) {
287 switch(self->base.tick_minor / self->type->animation_ticks_per_frame) {
288 case 12:
289 self->base.tick_minor = 0;
290 case 0:
291 case 2:
292 case 4:
293 case 6:
294 self->base.anim_frame = 0;
295 if(self->flags & JUMPNRUN_ENEMY_FACING_RIGHT) {
296 self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
297 } else {
298 self->base.inertia.x = self->type->spawn_inertia.x;
299 }
300
301 break;
302
303 case 1:
304 case 3:
305 case 5:
306 case 7:
307 self->base.anim_frame = 1;
308 if(self->flags & JUMPNRUN_ENEMY_FACING_RIGHT) {
309 self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
310 } else {
311 self->base.inertia.x = self->type->spawn_inertia.x;
312 }
313 break;
314
315 case 8:
316 case 10:
317 self->base.anim_frame = 2;
318 self->base.inertia.x = FIXED_INT(0);
319 break;
320
321 case 9:
322 case 11:
323 self->base.anim_frame = 3;
324 self->base.inertia.x = FIXED_INT(0);
325 break;
326 }
327 }
328
329 ++self->base.tick_minor;
330 if (fixed_point_lt(self->base.inertia.x, FIXED_INT(0))) { self->flags &= ~JUMPNRUN_ENEMY_FACING_RIGHT; }
331 else if(fixed_point_ne(self->base.inertia.x, FIXED_INT(0))) { self->flags |= JUMPNRUN_ENEMY_FACING_RIGHT; }
332 }
333
334 jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = {
335 {
336 .animation_ticks_per_frame = 16,
337 .animation_length = ARRAY_SIZE(anim_cat),
338 .animation_frames = anim_cat,
339 .extent = { FIXED_INT_I(8), FIXED_INT_I(5) },
340 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(2) },
341 { FIXED_INT_I(6), FIXED_INT_I(3) } },
342 .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_INT_I(0) },
343 .collision_tiles = enemy_collision_tiles_bounce_horiz,
344 .collision_player = enemy_collision_player_jumpable,
345 .game_tick = enemy_tick_straight_ahead
346 }, {
347 .animation_ticks_per_frame = 12,
348 .animation_length = ARRAY_SIZE(anim_mushroom),
349 .animation_frames = anim_mushroom,
350 .extent = { FIXED_INT_I(7), FIXED_INT_I(7) },
351 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
352 { FIXED_INT_I(5), FIXED_INT_I(4) } },
353 .spawn_inertia = { FIXED_POINT_I(0, -50), FIXED_INT_I(0) },
354 .collision_tiles = enemy_collision_tiles_bounce_horiz,
355 .collision_player = enemy_collision_player_jumpable,
356 .game_tick = enemy_tick_straight_ahead
357 }, {
358 .animation_ticks_per_frame = 9,
359 .animation_length = ARRAY_SIZE(anim_bunny),
360 .animation_frames = anim_bunny,
361 .extent = { FIXED_INT_I(7), FIXED_INT_I(5) },
362 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(2) },
363 { FIXED_INT_I(5), FIXED_INT_I(3) } },
364 .spawn_inertia = { FIXED_POINT_I(0, -80), FIXED_POINT_I(0, -800) },
365 .collision_tiles = enemy_collision_tiles_bounce_horiz,
366 .collision_player = enemy_collision_player_jumpable,
367 .game_tick = enemy_tick_jumper
368 }, {
369 .animation_ticks_per_frame = 6,
370 .animation_length = ARRAY_SIZE(anim_snake),
371 .animation_frames = anim_snake,
372 .extent = { FIXED_INT_I(10), FIXED_INT_I(6) },
373 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(4) },
374 { FIXED_INT_I(8), FIXED_INT_I(2) } },
375 .spawn_inertia = { FIXED_POINT_I(0, -150), FIXED_INT_I(0) },
376 .collision_tiles = enemy_collision_tiles_bounce_horiz,
377 .collision_player = enemy_collision_player_jumpable,
378 .game_tick = enemy_tick_straight_ahead
379 }, {
380 .animation_ticks_per_frame = 6,
381 .animation_length = ARRAY_SIZE(anim_spiral),
382 .animation_frames = anim_spiral,
383 .extent = { FIXED_INT_I(10), FIXED_INT_I(10) },
384 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
385 { FIXED_INT_I(8), FIXED_INT_I(8) } },
386 .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, -200) },
387 .collision_tiles = enemy_collision_tiles_pass_through,
388 .collision_player = enemy_collision_player_deadly,
389 .game_tick = enemy_tick_swing_up_and_down
390 }, {
391 .animation_ticks_per_frame = 5,
392 .animation_length = ARRAY_SIZE(anim_rotor),
393 .animation_frames = anim_rotor,
394 .extent = { FIXED_INT_I(9), FIXED_INT_I(9) },
395 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
396 { FIXED_INT_I(7), FIXED_INT_I(7) } },
397 .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, 0) },
398 .collision_tiles = enemy_collision_tiles_pass_through,
399 .collision_player = enemy_collision_player_deadly,
400 .game_tick = enemy_tick_stationary
401 }, {
402 .animation_ticks_per_frame = 16,
403 .animation_length = ARRAY_SIZE(anim_dog),
404 .animation_frames = anim_dog,
405 .extent = { FIXED_INT_I(8), FIXED_INT_I(5) },
406 .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(1) },
407 { FIXED_INT_I(6), FIXED_INT_I(4) } },
408 .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_POINT_I(0, 0) },
409 .collision_tiles = enemy_collision_tiles_bounce_horiz,
410 .collision_player = enemy_collision_player_jumpable,
411 .game_tick = enemy_tick_dog
412 }
413 };
This page took 0.061066 seconds and 3 git commands to generate.