From b049a32d80aac0012a653c868263d2efc9a27fcc Mon Sep 17 00:00:00 2001 From: arngo <27396817+arngo@users.noreply.github.com> Date: Sat, 27 May 2023 16:19:34 -0400 Subject: add walls and player wall collision --- CMakeLists.txt | 1 + external/RodeoKit | 2 +- src/main.c | 13 +++++++++++- src/player.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/player.h | 2 ++ src/wall.c | 40 +++++++++++++++++++++++++++++++++++ src/wall.h | 18 ++++++++++++++++ 7 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 src/wall.c create mode 100644 src/wall.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 748a9f1..dad1a2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${PROJECT_NAME} "src/bullet.c" "src/enemies.c" "src/sprite.c" + "src/wall.c" ) set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99) diff --git a/external/RodeoKit b/external/RodeoKit index 68b5d45..79cd0ac 160000 --- a/external/RodeoKit +++ b/external/RodeoKit @@ -1 +1 @@ -Subproject commit 68b5d45b66d948db337f50255fa777ce15ee0871 +Subproject commit 79cd0ac07a93fefbf59218ee77d44ea2501dfdd5 diff --git a/src/main.c b/src/main.c index e1b3f50..7ce77bd 100644 --- a/src/main.c +++ b/src/main.c @@ -5,8 +5,13 @@ #include "player.h" #include "bullet.h" #include "enemies.h" +#include "wall.h" #include "rodeo/collision.h" +const uint16_t window_width = 640; +const uint16_t window_height = 480; + + cstr renderer; float time_var; @@ -192,6 +197,7 @@ main_loop(void) draw_enemies(); detect_bullet_enemy_collisions(); detect_player_enemy_collisions(); + detect_player_wall_collisions(); rodeo_debug_text_draw( 1, @@ -235,7 +241,7 @@ main(void) rodeo_logLevel_error, "Testing error log level... It seems to work!" ); - mrodeo_window_do(480, 640, cstr_lit("Rodeo Window")) + mrodeo_window_do(window_height, window_width, cstr_lit("Rodeo Window")) { renderer = rodeo_renderer_name_get(); rodeo_frame_limit_set(60); @@ -266,10 +272,15 @@ main(void) init_bullets(); init_player(); init_enemies(); + init_wall(); spawn_enemy(240, 240); //spawn_enemy(100, 100); spawn_enemy(300, 100); spawn_enemy(200, 330); + new_wall(0, -10, window_width, 10); + new_wall(0, window_height, window_width, 10); + new_wall(-10, 0, 10, window_height); + new_wall(window_width, 0, 10, window_height); rodeo_mainLoop_run( main_loop diff --git a/src/player.c b/src/player.c index d171c80..fe518d5 100644 --- a/src/player.c +++ b/src/player.c @@ -4,6 +4,7 @@ #include "enemies.h" #include "rodeo/collision.h" #include "sprite.h" +#include "wall.h" struct player_t { @@ -159,10 +160,10 @@ void player_enemy_resolver( ) { if (player.hp <= 0) { - rodeo_log( + /*rodeo_log( rodeo_logLevel_info, "player is dead" - ); + );*/ } else if (player.damage_timer > 1000.0) { rodeo_log( rodeo_logLevel_info, @@ -182,6 +183,63 @@ detect_player_enemy_collisions(void) rodeo_collision_2d_world_compare_other(&player_collision_world, get_enemies_world(), player_enemy_resolver); } +void player_wall_resolver( + rodeo_collision_2d_world_item_t *player_collision, + rodeo_collision_2d_world_item_t *wall_collision +) +{ + rodeo_collision_2d_world_item_t *p = player_collision; + rodeo_collision_2d_world_item_t *w = wall_collision; + rodeo_rectangle_t step = (rodeo_rectangle_t){ + .x = p->x + p->dx * rodeo_frame_time_get(), + .y = p->y + p->dy * rodeo_frame_time_get(), + .width = p->width, + .height = p->height + }; + rodeo_rectangle_t intersection = rodeo_collision_2d_get_collision_rect(p, w); + if (intersection.width < intersection.height) { + if (intersection.x == step.x) { + p->x = w->x + w->width; + if (p->dx < 0) { + p->dx = 0; + } + } else { + p->x = w->x - p->width; + if (p->dx > 0) { + p->dx = 0; + } + } + } + else if (intersection.height < intersection.width) { + if (intersection.y == step.y) { + p->y = w->y + w->height; + if (p->dy < 0) { + p->dy = 0; + } + } else { + p->y = w->y - p->height; + if (p->dy > 0) { + p->dy = 0; + } + } + } + else if (p->width == w->width && p->height == w->height) { + p->dx = 0; + p->dy = 0; + } + rodeo_log( + rodeo_logLevel_info, + "%d collided with %d", + p->id.id, w->id.id + ); +} + +void +detect_player_wall_collisions(void) +{ + rodeo_collision_2d_world_compare_other(&player_collision_world, get_wall_world(), player_wall_resolver); +} + cvec_collision_2d_world_item_value * get_player_position(void) { diff --git a/src/player.h b/src/player.h index 0497d3c..fa010eb 100644 --- a/src/player.h +++ b/src/player.h @@ -34,6 +34,8 @@ player_shoot(rodeo_collision_2d_world_t *bullet_collision_world); void detect_player_enemy_collisions(void); +void +detect_player_wall_collisions(void); cvec_collision_2d_world_item_value * get_player_position(void); diff --git a/src/wall.c b/src/wall.c new file mode 100644 index 0000000..d2b1319 --- /dev/null +++ b/src/wall.c @@ -0,0 +1,40 @@ +#include "wall.h" + +static rodeo_collision_2d_world_t collision_wall_world; + +void +init_wall(void) +{ + collision_wall_world = rodeo_collision_2d_world_create(); +} + +void +deinit_wall(void) +{ + rodeo_collision_2d_world_destroy(&collision_wall_world); +} + +rodeo_collision_2d_world_t * +get_wall_world(void) +{ + return &collision_wall_world; +} + +rodeo_collision_2d_world_item_t * +new_wall( + float x, + float y, + float width, + float height +) +{ + return rodeo_collision_2d_world_item_create( + &collision_wall_world, + (rodeo_collision_2d_world_item_t) + { + .x = x, + .y = y, + .width = width, + .height = height + }); +} diff --git a/src/wall.h b/src/wall.h new file mode 100644 index 0000000..7b0f9fe --- /dev/null +++ b/src/wall.h @@ -0,0 +1,18 @@ +#include "rodeo/collision.h" + +void +init_wall(void); + +void +deinit_wall(void); + +rodeo_collision_2d_world_t * +get_wall_world(void); + +rodeo_collision_2d_world_item_t * +new_wall( + float x, + float y, + float width, + float height +); -- cgit v1.2.3