From 6814077feb9d4929afdbe224b4947a2e3245cb46 Mon Sep 17 00:00:00 2001 From: Wintermate Date: Fri, 18 Oct 2013 22:14:51 +0200 Subject: [PATCH] Spikes. --- badge/jumpnrun/collision.c | 53 +++++++++++++++++++++++------------ badge/jumpnrun/collision.h | 18 ++++++------ badge/jumpnrun/enemies.c | 14 +++++---- badge/jumpnrun/jumpnrun.c | 4 +-- badge/jumpnrun/levels.txt | 3 +- badge/jumpnrun/smb.lv | 14 +++++---- badge/jumpnrun/tiles.c | 16 +++++++---- badge/jumpnrun/tiles.h | 16 +++++++---- badge/jumpnrun/wrongturn.lv | 4 +-- mock/tools/level-converter.cc | 4 +++ 10 files changed, 92 insertions(+), 54 deletions(-) diff --git a/badge/jumpnrun/collision.c b/badge/jumpnrun/collision.c index 2b946f2..0f254e0 100644 --- a/badge/jumpnrun/collision.c +++ b/badge/jumpnrun/collision.c @@ -1,33 +1,39 @@ #include "collision.h" -void collision_displace(vec2d *desired_pos, - jumpnrun_moveable *current, - rectangle const *obstacle, - vec2d *inertia_mod) { +uint8_t collision_displace(vec2d *desired_pos, + jumpnrun_moveable *current, + rectangle const *obstacle, + vec2d *inertia_mod) { rectangle desired = current->current_box; rectangle_move_to(&desired, *desired_pos); if(!rectangle_intersect(obstacle, &desired)) { - return; + return 0; } fixed_point x = FIXED_INT(1000), y = FIXED_INT(1000); fixed_point dx = desired_pos->x, dy = desired_pos->y; bool bottom_collision = false; + uint8_t coll_x = 0; + uint8_t coll_y = 0; + uint8_t coll = 0; + if(fixed_point_le(rectangle_top ( obstacle), rectangle_top(&desired)) && fixed_point_gt(rectangle_bottom( obstacle), rectangle_top(&desired)) && fixed_point_lt(rectangle_top (&desired ), rectangle_top(¤t->current_box))) { - y = fixed_point_sub(rectangle_bottom(obstacle), rectangle_top(&desired)); - dy = rectangle_bottom(obstacle); + coll_y = JUMPNRUN_COLLISION_BOTTOM; + y = fixed_point_sub(rectangle_bottom(obstacle), rectangle_top(&desired)); + dy = rectangle_bottom(obstacle); } else if(fixed_point_gt(rectangle_bottom( obstacle), rectangle_bottom(&desired)) && fixed_point_le(rectangle_top ( obstacle), rectangle_bottom(&desired)) && fixed_point_gt(rectangle_top (&desired ), rectangle_top (¤t->current_box))) { - y = fixed_point_sub(rectangle_bottom(&desired ), rectangle_top ( obstacle)); - dy = fixed_point_sub(rectangle_top ( obstacle), rectangle_height(&desired )); + coll_y = JUMPNRUN_COLLISION_TOP; + y = fixed_point_sub(rectangle_bottom(&desired ), rectangle_top ( obstacle)); + dy = fixed_point_sub(rectangle_top ( obstacle), rectangle_height(&desired )); bottom_collision = true; } @@ -36,35 +42,39 @@ void collision_displace(vec2d *desired_pos, fixed_point_gt(rectangle_right( obstacle), rectangle_left(&desired)) && fixed_point_lt(rectangle_left (&desired ), rectangle_left(¤t->current_box))) { - x = fixed_point_sub(rectangle_right(obstacle), rectangle_left(&desired)); - dx = rectangle_right(obstacle); + coll_x = JUMPNRUN_COLLISION_RIGHT; + x = fixed_point_sub(rectangle_right(obstacle), rectangle_left(&desired)); + dx = rectangle_right(obstacle); } else if(fixed_point_gt(rectangle_right( obstacle), rectangle_right(&desired)) && fixed_point_le(rectangle_left ( obstacle), rectangle_right(&desired)) && fixed_point_gt(rectangle_left (&desired ), rectangle_left (¤t->current_box))) { - x = fixed_point_sub(rectangle_right(&desired ), rectangle_left ( obstacle)); - dx = fixed_point_sub(rectangle_left ( obstacle), rectangle_width(&desired )); + coll_x = JUMPNRUN_COLLISION_LEFT; + x = fixed_point_sub(rectangle_right(&desired ), rectangle_left ( obstacle)); + dx = fixed_point_sub(rectangle_left ( obstacle), rectangle_width(&desired )); } if(fixed_point_eq(x, y)) { desired_pos->x = dx; desired_pos->y = dy; + coll = coll_x | coll_y; } else if(fixed_point_gt(x, y)) { desired_pos->y = dy; inertia_mod->y = FIXED_INT(0); - current->touching_ground = bottom_collision; + coll = coll_y; } else { desired_pos->x = dx; inertia_mod->x = FIXED_INT(0); + coll = coll_x; } - return; + return coll; } -void collisions_tiles_displace(vec2d *desired_position, +bool collisions_tiles_displace(vec2d *desired_position, jumpnrun_moveable *thing, jumpnrun_level const *lv, jumpnrun_tile_range const *visible_tiles, @@ -123,6 +133,7 @@ void collisions_tiles_displace(vec2d *desired_position, } /* collision: sort by priority (top/bottom, left/right, then diagonal) */ + bool lethal = false; thing->touching_ground = false; for(size_t collision_index = 0; collision_index < ARRAY_SIZE(collision_order); ++collision_index) { @@ -130,10 +141,16 @@ void collisions_tiles_displace(vec2d *desired_position, continue; } - rectangle tile_rect = rect_from_tile(&lv->tiles[collision_tile[collision_order[collision_index]]]); + jumpnrun_tile *tile_obj = &lv->tiles[collision_tile[collision_order[collision_index]]]; + rectangle tile_rect = rect_from_tile(tile_obj); - collision_displace(desired_position, thing, &tile_rect, inertia_mod); + uint8_t coll = collision_displace(desired_position, thing, &tile_rect, inertia_mod); + if(coll & tile_type(tile_obj)->lethal_sides) { + lethal = true; + } } rectangle_move_to(&thing->current_box, *desired_position); + + return lethal; } diff --git a/badge/jumpnrun/collision.h b/badge/jumpnrun/collision.h index 65016e9..405a4f4 100644 --- a/badge/jumpnrun/collision.h +++ b/badge/jumpnrun/collision.h @@ -6,15 +6,15 @@ #include "../util/rectangle.h" #include -void collision_displace(vec2d *desired_pos, - jumpnrun_moveable *current, - rectangle const *obstacle, - vec2d *inertia_mod); +uint8_t collision_displace(vec2d *desired_pos, + jumpnrun_moveable *current, + rectangle const *obstacle, + vec2d *inertia_mod); -void collisions_tiles_displace(vec2d *desired_position, - jumpnrun_moveable *thing, - jumpnrun_level const *level, - jumpnrun_tile_range const *visible_tiles, - vec2d *inertia_mod); +bool collisions_tiles_displace(vec2d *desired_position, + jumpnrun_moveable *thing, + jumpnrun_level const *level, + jumpnrun_tile_range const *visible_tiles, + vec2d *inertia_mod); #endif diff --git a/badge/jumpnrun/enemies.c b/badge/jumpnrun/enemies.c index 09a8764..cc72db3 100644 --- a/badge/jumpnrun/enemies.c +++ b/badge/jumpnrun/enemies.c @@ -139,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); diff --git a/badge/jumpnrun/jumpnrun.c b/badge/jumpnrun/jumpnrun.c index ae0b80f..5fc5eb7 100644 --- a/badge/jumpnrun/jumpnrun.c +++ b/badge/jumpnrun/jumpnrun.c @@ -191,10 +191,10 @@ static void jumpnrun_apply_movement(jumpnrun_level const *lv, } *inertia_mod = state->player.inertia; - collisions_tiles_displace(&new_pos, &state->player, lv, tilerange, inertia_mod); + bool killed = collisions_tiles_displace(&new_pos, &state->player, lv, tilerange, inertia_mod); state->player.inertia = *inertia_mod; - if(fixed_point_gt(state->player.current_box.pos.y, FIXED_INT(BADGE_DISPLAY_HEIGHT))) { + if(killed || fixed_point_gt(state->player.current_box.pos.y, FIXED_INT(BADGE_DISPLAY_HEIGHT))) { state->status = JUMPNRUN_DEAD; } } diff --git a/badge/jumpnrun/levels.txt b/badge/jumpnrun/levels.txt index 035b5d7..b4b5113 100644 --- a/badge/jumpnrun/levels.txt +++ b/badge/jumpnrun/levels.txt @@ -1,5 +1,6 @@ smb -foo wrongturn +smb +foo gnobbel mean diff --git a/badge/jumpnrun/smb.lv b/badge/jumpnrun/smb.lv index ce34f8e..bc96897 100644 --- a/badge/jumpnrun/smb.lv +++ b/badge/jumpnrun/smb.lv @@ -5,12 +5,12 @@ ? X ######## ###? W ? ### #??# ## # ### # #### # # - G ##### # ### - ? #?#?# 01 01 #?# # ## ? ? ? # ## # # ## # ##?# ###### # ### - 01 V 23 23 @ R R ## ## ### ## ####### # ##### - P 01 23 23 23 M ### ### #### ### 01 01 ######## D # ##### + vvv G ##### # ### +> < ? #?#?# 01 01 #?# # ## ? ? ? # ## # # ## # ##?# ###### # ### +> < 01 V 23 23 @ R R ## ## ### ## ####### # ##### +> P < 01 23 23 23 M ### ### #### ### 01 01 ######## D # ##### S 23 23B 23 H H 23 C C C C C C H H #### #### ##### #### 23 C C 23######### # ##### -#################################################################### ############### ################################################################ ##################################################### +########^^^######################################################### ############### ################################################################ ##################################################### [tiles] 0 tube_top_left @@ -19,6 +19,10 @@ 3 tube_right # brick ? square +^ spike_up +v spike_down +> spike_right +< spike_left [items] D doc diff --git a/badge/jumpnrun/tiles.c b/badge/jumpnrun/tiles.c index a707950..8fb10ce 100644 --- a/badge/jumpnrun/tiles.c +++ b/badge/jumpnrun/tiles.c @@ -1,10 +1,14 @@ #include "tiles.h" jumpnrun_tile_type const jumpnrun_tile_type_data[JUMPNRUN_TILE_TYPE_COUNT] = { - { 0, { 5, 5, (uint8_t const *) "\x3f\xf7\xfe\x01" } }, - { 0, { 5, 5, (uint8_t const *) "\x3f\xc6\xf8\x01" } }, - { 0, { 5, 5, (uint8_t const *) "\x2f\x87\x10\x00" } }, - { 0, { 5, 5, (uint8_t const *) "\x21\x84\xfc\x00" } }, - { 0, { 5, 5, (uint8_t const *) "\xe0\x03\x00\x00" } }, - { 0, { 5, 5, (uint8_t const *) "\x00\x80\x0f\x00" } } + { 0, { 5, 5, (uint8_t const *) "\x3f\xf7\xfe\x01" } }, + { 0, { 5, 5, (uint8_t const *) "\x3f\xc6\xf8\x01" } }, + { 0, { 5, 5, (uint8_t const *) "\x2f\x87\x10\x00" } }, + { 0, { 5, 5, (uint8_t const *) "\x21\x84\xfc\x00" } }, + { 0, { 5, 5, (uint8_t const *) "\xe0\x03\x00\x00" } }, + { 0, { 5, 5, (uint8_t const *) "\x00\x80\x0f\x00" } }, + { JUMPNRUN_COLLISION_TOP , { 5, 5, (uint8_t const *) "\x98\x4e\x8a\x01" } }, + { JUMPNRUN_COLLISION_RIGHT , { 5, 5, (uint8_t const *) "\x3f\x2a\x42\x00" } }, + { JUMPNRUN_COLLISION_BOTTOM, { 5, 5, (uint8_t const *) "\xa3\xe4\x32\x00" } }, + { JUMPNRUN_COLLISION_LEFT , { 5, 5, (uint8_t const *) "\x84\xa8\xf8\x01" } } }; diff --git a/badge/jumpnrun/tiles.h b/badge/jumpnrun/tiles.h index f0cde4b..a6a5f70 100644 --- a/badge/jumpnrun/tiles.h +++ b/badge/jumpnrun/tiles.h @@ -7,12 +7,12 @@ #include -typedef enum { - JUMPNRUN_TILE_TOP = 1, - JUMPNRUN_TILE_BOTTOM = 2, - JUMPNRUN_TILE_LEFT = 4, - JUMPNRUN_TILE_RIGHT = 8 -} jumpnrun_tile_sides; +enum { + JUMPNRUN_COLLISION_TOP = 1, + JUMPNRUN_COLLISION_BOTTOM = 2, + JUMPNRUN_COLLISION_LEFT = 4, + JUMPNRUN_COLLISION_RIGHT = 8 +}; enum { JUMPNRUN_TILE_PIXEL_WIDTH = 5, @@ -53,6 +53,10 @@ enum { JUMPNRUN_TILE_TYPE_TUBE_TOP_RIGHT, JUMPNRUN_TILE_TYPE_TUBE_LEFT, JUMPNRUN_TILE_TYPE_TUBE_RIGHT, + JUMPNRUN_TILE_TYPE_SPIKE_UP, + JUMPNRUN_TILE_TYPE_SPIKE_RIGHT, + JUMPNRUN_TILE_TYPE_SPIKE_DOWN, + JUMPNRUN_TILE_TYPE_SPIKE_LEFT, JUMPNRUN_TILE_TYPE_COUNT }; diff --git a/badge/jumpnrun/wrongturn.lv b/badge/jumpnrun/wrongturn.lv index af86928..50bfea0 100644 --- a/badge/jumpnrun/wrongturn.lv +++ b/badge/jumpnrun/wrongturn.lv @@ -2,10 +2,10 @@ 01 # 01 23 01 23 # 23 K# 23 23 23 01 23 # ##### ########## ##### ### 23 23 23 23 01 01 # - P ? 01 ## # 01 01 ## # 23 M01 ###### 23 ##################### # + ? 01 ## # 01 01 P ## # 23 M01 ###### 23 ##################### # 23 M ## # ## ## ### # 23 23 MM # # 23 23 # # 01 ? 23 ####### ## # ## ## #### # 230123###### # 23 CMKCMKCMKCMK D 23 # - 23 01 23 ## # # # # ##2323 # # 23 CMKCMKCMKCMKCMKCMK 23 # + 23 01 23 ## # # # ##2323 # # 23 CMKCMKCMKCMKCMKCMK 23 # 23 ? ? 23 CC 23 ## M # # # 2323 ## # C K M C K M C K M ######################### M## # 23 ############# ######### # # #### ## ##################### ###### # M 23 # # # ? ? ### # diff --git a/mock/tools/level-converter.cc b/mock/tools/level-converter.cc index 7f67732..e5bfa98 100644 --- a/mock/tools/level-converter.cc +++ b/mock/tools/level-converter.cc @@ -33,6 +33,10 @@ namespace jnrcpp { ("tube_right" , JUMPNRUN_TILE_TYPE_TUBE_RIGHT ) ("brick" , JUMPNRUN_TILE_TYPE_BRICK ) ("square" , JUMPNRUN_TILE_TYPE_SQUARE ) + ("spike_up" , JUMPNRUN_TILE_TYPE_SPIKE_UP ) + ("spike_right" , JUMPNRUN_TILE_TYPE_SPIKE_RIGHT ) + ("spike_down" , JUMPNRUN_TILE_TYPE_SPIKE_DOWN ) + ("spike_left" , JUMPNRUN_TILE_TYPE_SPIKE_LEFT ) ; items.add -- 2.20.1