diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| m--------- | external/RodeoKit | 0 | ||||
| -rw-r--r-- | src/main.c | 13 | ||||
| -rw-r--r-- | src/player.c | 62 | ||||
| -rw-r--r-- | src/player.h | 2 | ||||
| -rw-r--r-- | src/wall.c | 40 | ||||
| -rw-r--r-- | src/wall.h | 18 |
7 files changed, 133 insertions, 3 deletions
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 -Subproject 68b5d45b66d948db337f50255fa777ce15ee087 +Subproject 79cd0ac07a93fefbf59218ee77d44ea2501dfdd @@ -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; @@ -194,6 +199,7 @@ main_loop(void) draw_enemies(); detect_bullet_enemy_collisions(); detect_player_enemy_collisions(); + detect_player_wall_collisions(); rodeo_debug_text_draw( 1, @@ -237,7 +243,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); @@ -268,10 +274,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 99a9c8a..55c6d38 100644 --- a/src/player.c +++ b/src/player.c @@ -5,6 +5,7 @@ #include "rodeo/collision.h" #include "bullet.h" #include "sprite.h" +#include "wall.h" struct player_t { @@ -157,10 +158,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, @@ -180,6 +181,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 +); |
