X-Git-Url: https://git.rohieb.name/hackover2013-badge-firmware.git/blobdiff_plain/5df5b3536a7e824c1340a07f3c26a7c61e78b68b..eb0884ecd703881e43b1c34ac04e0d49dbab4c38:/badge/jumpnrun/enemies.c diff --git a/badge/jumpnrun/enemies.c b/badge/jumpnrun/enemies.c index 1b35739..cc72db3 100644 --- a/badge/jumpnrun/enemies.c +++ b/badge/jumpnrun/enemies.c @@ -27,10 +27,10 @@ static badge_sprite const anim_bunny[] = { }; static badge_sprite const anim_snake[] = { - { 10, 6, (uint8_t const *) "\x10\x86\x83\x30\x04\x83\xa2\x0f" }, - { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x0c\xc1\xa2\x0f" }, - { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x82\xe0\x0f" }, - { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x86\xe1\x0f" } + { 10, 6, (uint8_t const *) "\x10\x86\x83\x30\x04\x83\xa2\x07" }, + { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x0c\xc1\xa2\x07" }, + { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x82\xe0\x07" }, + { 10, 6, (uint8_t const *) "\x10\x86\x83\x20\x08\x86\xe1\x07" } }; static badge_sprite const anim_spiral[] = { @@ -58,6 +58,24 @@ static badge_sprite const anim_dog[] = { { 8, 5, (uint8_t const *) "\xc1\xf8\xc6\xb8\x20" } }; +static badge_sprite const anim_giraffe[] = { + { 8, 10, (uint8_t const *) "\x00\x10\x60\x80\xff\xc1\x00\x03\x3c\x08" }, + { 8, 10, (uint8_t const *) "\x00\x10\x60\xa0\x7f\xc1\x00\x03\x1c\x88" }, + { 9, 10, (uint8_t const *) "\x00\x10\x68\x90\x3f\xc1\x00\x03\x0c\x48\x00\x02" }, + { 8, 10, (uint8_t const *) "\x00\x10\x60\xb0\x3f\xc1\x00\x03\x0c\xc8" }, + { 8, 10, (uint8_t const *) "\x08\x30\xc0\x81\xfc\xc0\x00\x03\x3c\x08" }, + { 8, 10, (uint8_t const *) "\x00\x10\x60\x80\xff\xc1\x00\x03\x3c\x08" } +}; + +static badge_sprite const anim_bird[] = { + { 9, 7, (uint8_t const *) "\x10\x0c\x05\xf7\x4a\xa9\x58\x10" }, + { 9, 7, (uint8_t const *) "\x10\x0c\x05\x87\x47\xa2\x70\x10" } +/* + { 10, 8, (uint8_t const *) "\x10\x10\x18\x14\x38\x2e\x29\x29\x2e\x10" }, + { 10, 8, (uint8_t const *) "\x10\x10\x18\x14\x38\x78\xa8\xa8\x78\x10" } +*/ +}; + static void enemy_animation_advance(jumpnrun_enemy *enemy) { ++enemy->base.tick_minor; if(enemy->base.tick_minor == enemy->type->animation_ticks_per_frame) { @@ -84,7 +102,8 @@ void jumpnrun_process_enemy(jumpnrun_enemy *self, fixed_point_gt(rectangle_top (enemy_box(self)), FIXED_POINT(BADGE_DISPLAY_HEIGHT , 0))) { self->flags &= ~JUMPNRUN_ENEMY_SPAWNED; } else { - self->type->game_tick(self, state, lv, visible_tiles, player_inertia_mod); + self->type->move_tick(self, state, lv, visible_tiles, player_inertia_mod); + self->type->collision_shots(self, state); if(fb) { badge_framebuffer_blt(fb, @@ -104,7 +123,7 @@ void jumpnrun_process_enemy(jumpnrun_enemy *self, (fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 0)) && fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH, 0)))) { // enemy unspawned, available and in spawn zone. - self->flags |= JUMPNRUN_ENEMY_SPAWNED | JUMPNRUN_ENEMY_UNAVAILABLE; + self->flags = JUMPNRUN_ENEMY_SPAWNED | JUMPNRUN_ENEMY_UNAVAILABLE; self->base.current_box = rectangle_new(self->spawn_pos, self->type->extent); self->base.inertia = self->type->spawn_inertia; self->base.anim_frame = 0; @@ -120,11 +139,15 @@ void enemy_collision_tiles_bounce_horiz(jumpnrun_enemy *self, jumpnrun_tile_range const *visible_tiles) { vec2d inertia_mod = self->base.inertia; - collisions_tiles_displace(desired_position, - &self->base, - lv, - visible_tiles, - &inertia_mod); + bool killed = collisions_tiles_displace(desired_position, + &self->base, + lv, + visible_tiles, + &inertia_mod); + + if(killed) { + self->flags &= ~JUMPNRUN_ENEMY_SPAWNED; + } if(fixed_point_ne(inertia_mod.x, self->base.inertia.x)) { self->base.inertia.x = fixed_point_neg(self->base.inertia.x); @@ -150,7 +173,9 @@ void enemy_collision_player_jumpable(jumpnrun_enemy *self, rectangle rect_self = enemy_hitbox(self); if(rectangle_intersect(&rect_self, &state->player.current_box)) { - if(fixed_point_gt(state->player.inertia.y, FIXED_POINT(0, 0))) { + if(fixed_point_lt(rectangle_top(&state->player.current_box), rectangle_top(&rect_self)) && + fixed_point_gt(state->player.inertia.y, FIXED_POINT(0, 0))) + { self->flags &= ~JUMPNRUN_ENEMY_SPAWNED; player_inertia_mod->y = FIXED_POINT(0, -250); state->player.jumpable_frames = 12; @@ -171,6 +196,49 @@ void enemy_collision_tiles_pass_through(struct jumpnrun_enemy *self, return; } +void enemy_collision_shots_die(struct jumpnrun_enemy *self, + struct jumpnrun_game_state *state) { + rectangle rect_self = enemy_hitbox(self); + + for(uint8_t i = 0; i < JUMPNRUN_MAX_SHOTS; ++i) { + jumpnrun_shot *shot = &state->shots[i]; + + if(jumpnrun_shot_spawned(shot)) { + if(rectangle_intersect(&rect_self, &shot->current_box)) { + self->flags &= ~JUMPNRUN_ENEMY_SPAWNED; + jumpnrun_shot_despawn(shot); + } + } + } +} + +void enemy_collision_shots_bounce(struct jumpnrun_enemy *self, + struct jumpnrun_game_state *state) { + rectangle rect_self = enemy_hitbox(self); + + for(uint8_t i = 0; i < JUMPNRUN_MAX_SHOTS; ++i) { + jumpnrun_shot *shot = &state->shots[i]; + + if(jumpnrun_shot_spawned(shot)) { + if(rectangle_intersect(&rect_self, &shot->current_box)) { + if(fixed_point_gt(shot->inertia.x, FIXED_INT(0))) { + rectangle_move_to_x(&shot->current_box, fixed_point_sub(rectangle_left(&rect_self), rectangle_width(&shot->current_box))); + } else { + rectangle_move_to_x(&shot->current_box, rectangle_right(&rect_self)); + } + + shot->inertia.x = fixed_point_neg(shot->inertia.x); + } + } + } +} + +void enemy_collision_shots_dontcare(struct jumpnrun_enemy *self, + struct jumpnrun_game_state *state) { + (void) self; + (void) state; +} + void enemy_tick_straight_ahead(jumpnrun_enemy *self, jumpnrun_game_state *state, jumpnrun_level *lv, @@ -193,6 +261,35 @@ void enemy_tick_straight_ahead(jumpnrun_enemy *self, enemy_animation_advance(self); } +void enemy_tick_straight_follow(jumpnrun_enemy *self, + jumpnrun_game_state *state, + jumpnrun_level *lv, + jumpnrun_tile_range const *visible_tiles, + vec2d *player_inertia_mod) { + int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box)); + + if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left || + screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) { + return; + } + + jumpnrun_passive_movement(&self->base.inertia); + + vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia); + self->type->collision_tiles(self, &new_pos, lv, visible_tiles); + self->type->collision_player(self, state, player_inertia_mod); + rectangle_move_to(&self->base.current_box, new_pos); + + if(fixed_point_le(rectangle_mid_x(&state->player.current_box), rectangle_mid_x(enemy_box(self)))) { + self->base.inertia.x = self->type->spawn_inertia.x; + } else { + self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x); + } + + + enemy_animation_advance(self); +} + void enemy_tick_swing_up_and_down(struct jumpnrun_enemy *self, struct jumpnrun_game_state *state, struct jumpnrun_level *lv, @@ -331,9 +428,114 @@ void enemy_tick_dog(jumpnrun_enemy *self, else if(fixed_point_ne(self->base.inertia.x, FIXED_INT(0))) { self->flags |= JUMPNRUN_ENEMY_FACING_RIGHT; } } +void enemy_tick_giraffe(jumpnrun_enemy *self, + jumpnrun_game_state *state, + jumpnrun_level *lv, + jumpnrun_tile_range const *visible_tiles, + vec2d *player_inertia_mod) { + int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box)); + + if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left || + screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) { + return; + } + + bool was_on_ground = self->base.touching_ground; + + jumpnrun_passive_movement(&self->base.inertia); + + vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia); + self->type->collision_tiles(self, &new_pos, lv, visible_tiles); + self->type->collision_player(self, state, player_inertia_mod); + rectangle_move_to(&self->base.current_box, new_pos); + + if(self->base.touching_ground) { + if(was_on_ground) { + enemy_animation_advance(self); + if(self->base.anim_frame == 0) { + self->base.inertia = self->type->spawn_inertia; + + if(fixed_point_gt(rectangle_mid_x(&state->player.current_box), rectangle_mid_x(enemy_box(self)))) { + self->base.inertia.x = fixed_point_neg(self->base.inertia.x); + } + } + } else { + self->base.tick_minor = 0; + self->base.anim_frame = 3; + self->base.inertia.x = FIXED_INT(0); + } + } else if(was_on_ground) { + self->base.tick_minor = 0; + self->base.anim_frame = 1; + } else if(self->base.anim_frame == 1) { + enemy_animation_advance(self); + } + + ++self->base.tick_minor; + if (fixed_point_lt(self->base.inertia.x, FIXED_INT(0))) { self->flags &= ~JUMPNRUN_ENEMY_FACING_RIGHT; } + else if(fixed_point_ne(self->base.inertia.x, FIXED_INT(0))) { self->flags |= JUMPNRUN_ENEMY_FACING_RIGHT; } +} + +void enemy_tick_fly_straight(struct jumpnrun_enemy *self, + struct jumpnrun_game_state *state, + struct jumpnrun_level *lv, + struct jumpnrun_tile_range const *visible_tiles, + vec2d *player_inertia_mod) { + int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box)); + + if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left || + screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) { + return; + } + + vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia); + self->type->collision_tiles(self, &new_pos, lv, visible_tiles); + self->type->collision_player(self, state, player_inertia_mod); + rectangle_move_to(&self->base.current_box, new_pos); + + enemy_animation_advance(self); +} + +void enemy_tick_fly_straight_and_dip(struct jumpnrun_enemy *self, + struct jumpnrun_game_state *state, + struct jumpnrun_level *lv, + struct jumpnrun_tile_range const *visible_tiles, + vec2d *player_inertia_mod) { + int screenpos = fixed_point_cast_int(rectangle_left(&self->base.current_box)); + + if(screenpos + JUMPNRUN_MAX_SPAWN_MARGIN < state->left || + screenpos >= state->left + BADGE_DISPLAY_WIDTH + JUMPNRUN_MAX_SPAWN_MARGIN) { + return; + } + + if(fixed_point_lt(fixed_point_abs(fixed_point_sub(enemy_position(self).x, + state->player.current_box.pos.x)), + FIXED_INT(20))) { + self->flags |= JUMPNRUN_ENEMY_EVENT_TRIGGER1; + } + + if(self->flags & JUMPNRUN_ENEMY_EVENT_TRIGGER1) { + self->base.inertia.y = + fixed_point_add(fixed_point_add(self->base.inertia.y, + fixed_point_div(self->type->spawn_inertia.y, FIXED_INT(3))), + fixed_point_mul(FIXED_POINT(0, 5), + fixed_point_sub(self->spawn_pos.y, + enemy_position(self).y))); + } else { + self->base.inertia.y = FIXED_INT(0); + } + + vec2d new_pos = vec2d_add(enemy_position(self), self->base.inertia);; + self->type->collision_tiles(self, &new_pos, lv, visible_tiles); + self->type->collision_player(self, state, player_inertia_mod); + rectangle_move_to(&self->base.current_box, new_pos); + + enemy_animation_advance(self); +} + jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = { { - .animation_ticks_per_frame = 16, + .animation_ticks_per_frame = 18, .animation_length = ARRAY_SIZE(anim_cat), .animation_frames = anim_cat, .extent = { FIXED_INT_I(8), FIXED_INT_I(5) }, @@ -342,7 +544,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_INT_I(0) }, .collision_tiles = enemy_collision_tiles_bounce_horiz, .collision_player = enemy_collision_player_jumpable, - .game_tick = enemy_tick_straight_ahead + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_straight_ahead }, { .animation_ticks_per_frame = 12, .animation_length = ARRAY_SIZE(anim_mushroom), @@ -353,7 +556,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_POINT_I(0, -50), FIXED_INT_I(0) }, .collision_tiles = enemy_collision_tiles_bounce_horiz, .collision_player = enemy_collision_player_jumpable, - .game_tick = enemy_tick_straight_ahead + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_straight_follow }, { .animation_ticks_per_frame = 9, .animation_length = ARRAY_SIZE(anim_bunny), @@ -364,7 +568,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_POINT_I(0, -80), FIXED_POINT_I(0, -800) }, .collision_tiles = enemy_collision_tiles_bounce_horiz, .collision_player = enemy_collision_player_jumpable, - .game_tick = enemy_tick_jumper + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_jumper }, { .animation_ticks_per_frame = 6, .animation_length = ARRAY_SIZE(anim_snake), @@ -375,7 +580,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_POINT_I(0, -150), FIXED_INT_I(0) }, .collision_tiles = enemy_collision_tiles_bounce_horiz, .collision_player = enemy_collision_player_jumpable, - .game_tick = enemy_tick_straight_ahead + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_straight_ahead }, { .animation_ticks_per_frame = 6, .animation_length = ARRAY_SIZE(anim_spiral), @@ -386,7 +592,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, -200) }, .collision_tiles = enemy_collision_tiles_pass_through, .collision_player = enemy_collision_player_deadly, - .game_tick = enemy_tick_swing_up_and_down + .collision_shots = enemy_collision_shots_dontcare, + .move_tick = enemy_tick_swing_up_and_down }, { .animation_ticks_per_frame = 5, .animation_length = ARRAY_SIZE(anim_rotor), @@ -397,9 +604,10 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_INT_I(0), FIXED_POINT_I(0, 0) }, .collision_tiles = enemy_collision_tiles_pass_through, .collision_player = enemy_collision_player_deadly, - .game_tick = enemy_tick_stationary + .collision_shots = enemy_collision_shots_dontcare, + .move_tick = enemy_tick_stationary }, { - .animation_ticks_per_frame = 16, + .animation_ticks_per_frame = 18, .animation_length = ARRAY_SIZE(anim_dog), .animation_frames = anim_dog, .extent = { FIXED_INT_I(8), FIXED_INT_I(5) }, @@ -408,6 +616,55 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] = .spawn_inertia = { FIXED_POINT_I(0, -200), FIXED_POINT_I(0, 0) }, .collision_tiles = enemy_collision_tiles_bounce_horiz, .collision_player = enemy_collision_player_jumpable, - .game_tick = enemy_tick_dog + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_dog + }, { + .animation_ticks_per_frame = 36, + .animation_length = ARRAY_SIZE(anim_giraffe), + .animation_frames = anim_giraffe, + .extent = { FIXED_INT_I(7), FIXED_INT_I(10) }, + .hitbox = { { FIXED_INT_I(2), FIXED_INT_I(1) }, + { FIXED_INT_I(4), FIXED_INT_I(9) } }, + .spawn_inertia = { FIXED_POINT_I(0, -150), FIXED_POINT_I(-1, -200) }, + .collision_tiles = enemy_collision_tiles_bounce_horiz, + .collision_player = enemy_collision_player_jumpable, + .collision_shots = enemy_collision_shots_bounce, + .move_tick = enemy_tick_giraffe + }, { + .animation_ticks_per_frame = 24, + .animation_length = ARRAY_SIZE(anim_bird), + .animation_frames = anim_bird, + .extent = { FIXED_INT_I(9), FIXED_INT_I(7) }, + .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) }, + { FIXED_INT_I(7), FIXED_INT_I(3) } }, + .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_POINT_I(0, -150) }, + .collision_tiles = enemy_collision_tiles_bounce_horiz, + .collision_player = enemy_collision_player_jumpable, + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_swing_up_and_down + }, { + .animation_ticks_per_frame = 24, + .animation_length = ARRAY_SIZE(anim_bird), + .animation_frames = anim_bird, + .extent = { FIXED_INT_I(9), FIXED_INT_I(7) }, + .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) }, + { FIXED_INT_I(7), FIXED_INT_I(3) } }, + .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_INT_I(0) }, + .collision_tiles = enemy_collision_tiles_bounce_horiz, + .collision_player = enemy_collision_player_jumpable, + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_fly_straight + }, { + .animation_ticks_per_frame = 24, + .animation_length = ARRAY_SIZE(anim_bird), + .animation_frames = anim_bird, + .extent = { FIXED_INT_I(9), FIXED_INT_I(7) }, + .hitbox = { { FIXED_INT_I(1), FIXED_INT_I(3) }, + { FIXED_INT_I(7), FIXED_INT_I(3) } }, + .spawn_inertia = { FIXED_POINT_I(0, -400), FIXED_POINT_I(0, 200) }, + .collision_tiles = enemy_collision_tiles_bounce_horiz, + .collision_player = enemy_collision_player_jumpable, + .collision_shots = enemy_collision_shots_die, + .move_tick = enemy_tick_fly_straight_and_dip } };