diff --git a/game.c b/game.c index db3e2a9..40e0c30 100644 --- a/game.c +++ b/game.c @@ -96,8 +96,9 @@ void process_bullet_collisions(Game *g, Entity *bullet) { } void run_frame(Game *g) { + float dt = GetFrameTime(); handle_input(g); - tick_player(g->player); + tick_player(g->player, dt); FOREACH_ENEMY { INIT_ENEMY; if (entity_expired(e)) { diff --git a/level.c b/level.c index 99ca2b9..df9152d 100644 --- a/level.c +++ b/level.c @@ -35,22 +35,18 @@ void init_level(Game *g, Level *l, char *filepath) { l->game = g; } -size_t tile_index_from_position(Level *l, Vector2 p) { +size_t level_tile_index_from_position(Level *l, Vector2 p) { size_t x_tile = p.x / 32; size_t y_tile = p.y / 32; return ((y_tile * l->width) + x_tile) % l->data_size; } -typedef struct FourIndices { - size_t index[4]; -} FourIndices; - FourIndices rect_corners(Level *l, Rectangle r) { return (FourIndices) { - tile_index_from_position(l, (Vector2) { r.x, r.y }), - tile_index_from_position(l, (Vector2) { r.x + r.width, r.y }), - tile_index_from_position(l, (Vector2) { r.x, r.y + r.height }), - tile_index_from_position(l, (Vector2) { r.x + r.width, r.y + r.height}), + level_tile_index_from_position(l, (Vector2) { r.x, r.y }), + level_tile_index_from_position(l, (Vector2) { r.x + r.width, r.y }), + level_tile_index_from_position(l, (Vector2) { r.x, r.y + r.height }), + level_tile_index_from_position(l, (Vector2) { r.x + r.width, r.y + r.height}), }; } @@ -68,7 +64,6 @@ void draw_tile(Level *l, float x, float y) { } void draw_level(Level *l) { - Camera2D *camera = l->game->camera; float y = l->game->player->position.y; y /= 32; diff --git a/level.h b/level.h index 344f0da..93f9bb5 100644 --- a/level.h +++ b/level.h @@ -2,7 +2,9 @@ #define LEVEL_H typedef struct Level Level; +typedef struct FourIndices FourIndices; +#include #include "game.h" struct Level { @@ -13,7 +15,13 @@ struct Level { Game *game; }; +struct FourIndices { + size_t index[4]; +}; + + void init_level(Game *g, Level *l, char *filepath); void draw_level(Level *l); +FourIndices rect_corners(Level *l, Rectangle r); #endif diff --git a/player.c b/player.c index a58bdbb..f25482b 100644 --- a/player.c +++ b/player.c @@ -23,6 +23,8 @@ void initialize_player(Game *g, Player *p) { p->spritesheet = LoadTextureFromImage(sprite_img); UnloadImage(sprite_img); p->game = g; + p->timer = 2.0; + p->destroyed = false; } void handle_player_input(Player *p) { @@ -36,11 +38,17 @@ void handle_player_input(Player *p) { } void draw_player(Player *p) { + if (p->destroyed) return; + + if (p->timer < 0 || ((int)(p->timer * 20.)) % 2) { DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE); + } DrawRectangleRec(p->bbox, YELLOW); } void move_player(Player *p) { + if (p->destroyed) return; + switch (p->roll) { case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break; case ROLL_NEUTRAL: p->acceleration.x = 0; break; @@ -82,14 +90,39 @@ void update_camera_player(Player *p) { } } -void check_collision_top_left(Vector2 p) { +void handle_terrain_crash(Player *p) { + printf("Boom!\n"); + p->destroyed = true; + p->timer = 3.; + p->velocity.x = 0; + p->velocity.y = 0; + p->acceleration.x = 0; + p->acceleration.y = 0; } void check_collision_player_with_terrain(Player *p) { - check_collision_top_left(p->position); + if (p->timer > 0) return; + Level *l = p->game->level; + FourIndices fi = rect_corners(l, p->bbox); + for (int i = 0; i < 4; i++) { + if (l->data[fi.index[0]] == 0) { + handle_terrain_crash(p); + break; + } + } } -void tick_player(Player *p) { +void revive_player(Player *p) { + if (!p->destroyed) return; + if (p->timer > 0.) return; + + p->destroyed = false; + p->timer = 2.; +} + +void tick_player(Player *p, float dt) { + if (p->timer > 0) p->timer -= dt; + revive_player(p); move_player(p); update_camera_player(p); check_collision_player_with_terrain(p); diff --git a/player.h b/player.h index 8c925ea..6119ac6 100644 --- a/player.h +++ b/player.h @@ -37,6 +37,7 @@ typedef enum PlayerRoll { typedef struct Player Player; #include "game.h" +#include "level.h" struct Player { Vector2 position; @@ -47,11 +48,13 @@ struct Player { PlayerRoll roll; Texture2D spritesheet; Game *game; + float timer; + bool destroyed; }; void initialize_player(Game *g, Player *p); void handle_player_input(Player *p); void draw_player(Player *p); -void tick_player(Player *p); +void tick_player(Player *p, float dt); #endif