Gegnerspawning an Checkpoints richtig.
[hackover2013-badge-firmware.git] / badge / jumpnrun / enemies.c
index 0767c80..b518b51 100644 (file)
@@ -118,12 +118,10 @@ void jumpnrun_process_enemy(jumpnrun_enemy                   *self,
        state->left + BADGE_DISPLAY_WIDTH + spawn_margin < fixed_point_cast_int(self->spawn_pos.x)) {
       self->flags &= ~JUMPNRUN_ENEMY_UNAVAILABLE;
     }
-  } else if((fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left                       - spawn_margin, 0)) &&
-             fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left - spawn_margin / 2, 0))) ||
-            (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)))) {
+  } else if(fixed_point_gt(self->spawn_pos.x, FIXED_POINT(state->left                       - spawn_margin, 0)) &&
+            fixed_point_lt(self->spawn_pos.x, FIXED_POINT(state->left + BADGE_DISPLAY_WIDTH + spawn_margin, 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;
@@ -139,11 +137,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);
@@ -257,6 +259,34 @@ 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_lt(rectangle_right(&state->player.current_box), rectangle_left(enemy_box(self)))) {
+    self->base.inertia.x = self->type->spawn_inertia.x;
+  } else if(fixed_point_gt(rectangle_left(&state->player.current_box), rectangle_right(enemy_box(self)))) {
+    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,
@@ -420,11 +450,10 @@ void enemy_tick_giraffe(jumpnrun_enemy            *self,
     if(was_on_ground) {
       enemy_animation_advance(self);
       if(self->base.anim_frame == 0) {
-        if(self->flags & JUMPNRUN_ENEMY_FACING_RIGHT) {
-          self->base.inertia.x = fixed_point_neg(self->type->spawn_inertia.x);
-          self->base.inertia.y = self->type->spawn_inertia.y;
-        } else {
-          self->base.inertia = self->type->spawn_inertia;
+        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 {
@@ -444,6 +473,63 @@ void enemy_tick_giraffe(jumpnrun_enemy            *self,
   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 = 18,
@@ -464,11 +550,11 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] =
     .extent                    = { FIXED_INT_I(7), FIXED_INT_I(7) },
     .hitbox                    = { { FIXED_INT_I(1), FIXED_INT_I(1) },
                                    { FIXED_INT_I(5), FIXED_INT_I(4) } },
-    .spawn_inertia             = { FIXED_POINT_I(0, -50), FIXED_INT_I(0) },
+    .spawn_inertia             = { FIXED_POINT_I(0, -80), 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_straight_ahead
+    .move_tick                 = enemy_tick_straight_follow
   }, {
     .animation_ticks_per_frame = 9,
     .animation_length          = ARRAY_SIZE(anim_bunny),
@@ -553,5 +639,29 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] =
     .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
   }
 };
This page took 0.0347 seconds and 4 git commands to generate.