Gegner reagieren auf Schüsse.
authorWintermute <wintermute@hannover.ccc.de>
Thu, 17 Oct 2013 19:50:10 +0000 (21:50 +0200)
committerWintermute <wintermute@hannover.ccc.de>
Thu, 17 Oct 2013 19:50:10 +0000 (21:50 +0200)
badge/jumpnrun/enemies.c
badge/jumpnrun/enemies.h
badge/jumpnrun/jumpnrun.c
badge/jumpnrun/jumpnrun.h
badge/jumpnrun/levels.txt

index edc1f82..0767c80 100644 (file)
@@ -102,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,
@@ -191,6 +192,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,
@@ -411,7 +455,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),
@@ -422,7 +467,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_ahead
   }, {
     .animation_ticks_per_frame = 9,
     .animation_length          = ARRAY_SIZE(anim_bunny),
@@ -433,7 +479,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),
@@ -444,7 +491,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),
@@ -455,7 +503,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),
@@ -466,7 +515,8 @@ 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 = 18,
     .animation_length          = ARRAY_SIZE(anim_dog),
@@ -477,7 +527,8 @@ 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),
@@ -488,7 +539,8 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] =
     .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,
-    .game_tick                 = enemy_tick_giraffe
+    .collision_shots           = enemy_collision_shots_bounce,
+    .move_tick                 = enemy_tick_giraffe
   }, {
     .animation_ticks_per_frame = 24,
     .animation_length          = ARRAY_SIZE(anim_bird),
@@ -499,6 +551,7 @@ jumpnrun_enemy_type const jumpnrun_enemy_type_data[JUMPNRUN_ENEMY_TYPE_COUNT] =
     .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,
-    .game_tick                 = enemy_tick_swing_up_and_down
+    .collision_shots           = enemy_collision_shots_die,
+    .move_tick                 = enemy_tick_swing_up_and_down
   }
 };
index 9c08f68..7c1fc5a 100644 (file)
@@ -29,7 +29,10 @@ typedef struct jumpnrun_enemy_type {
   void (*collision_player)(struct jumpnrun_enemy      *self,
                            struct jumpnrun_game_state *state,
                            vec2d                      *player_inertia_mod);
-  void (*game_tick)(struct jumpnrun_enemy            *self,
+  void (*collision_shots)(struct jumpnrun_enemy      *self,
+                          struct jumpnrun_game_state *state);
+
+  void (*move_tick)(struct jumpnrun_enemy            *self,
                     struct jumpnrun_game_state       *state,
                     struct jumpnrun_level            *lv,
                     struct jumpnrun_tile_range const *visible_tiles,
index 9f6053a..2aea579 100644 (file)
@@ -55,14 +55,10 @@ static badge_sprite const anim_sickle[] = {
 };
 
 enum {
-  JUMPNRUN_SHOT_EXTENT          =  3,
+  JUMPNRUN_SHOT_EXTENT = 3,
   JUMPNRUN_SHOT_TICKS_PER_FRAME = 24
 };
 
-static void jumpnrun_shot_despawn(jumpnrun_shot *shot) {
-  shot->inertia.x = FIXED_INT(0);
-}
-
 static void jumpnrun_shot_spawn(jumpnrun_shot *shot, jumpnrun_game_state const *state) {
   shot->tick        = 0;
   shot->inertia     = shot_spawn_inertia;
index fc386c7..5fcc893 100644 (file)
@@ -31,6 +31,7 @@ typedef struct jumpnrun_shot {
 } jumpnrun_shot;
 
 static inline bool jumpnrun_shot_spawned(jumpnrun_shot const *shot) { return fixed_point_ne(shot->inertia.x, FIXED_INT(0)); }
+static inline void jumpnrun_shot_despawn(jumpnrun_shot       *shot) { shot->inertia.x = FIXED_INT(0); }
 
 enum {
   JUMPNRUN_MAX_SHOTS = 2
index 88ed79c..cc57bcc 100644 (file)
@@ -1,5 +1,5 @@
-wrongturn
 smb
+wrongturn
 foo
 gnobbel
 mean
This page took 0.028407 seconds and 4 git commands to generate.