Ability to crash the plane

This commit is contained in:
Bill Rossi 2025-03-09 10:49:50 -04:00
parent 863c2d881f
commit 21efc3d6e9
5 changed files with 55 additions and 15 deletions

3
game.c
View File

@ -96,8 +96,9 @@ void process_bullet_collisions(Game *g, Entity *bullet) {
} }
void run_frame(Game *g) { void run_frame(Game *g) {
float dt = GetFrameTime();
handle_input(g); handle_input(g);
tick_player(g->player); tick_player(g->player, dt);
FOREACH_ENEMY { FOREACH_ENEMY {
INIT_ENEMY; INIT_ENEMY;
if (entity_expired(e)) { if (entity_expired(e)) {

15
level.c
View File

@ -35,22 +35,18 @@ void init_level(Game *g, Level *l, char *filepath) {
l->game = g; 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 x_tile = p.x / 32;
size_t y_tile = p.y / 32; size_t y_tile = p.y / 32;
return ((y_tile * l->width) + x_tile) % l->data_size; 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) { FourIndices rect_corners(Level *l, Rectangle r) {
return (FourIndices) { return (FourIndices) {
tile_index_from_position(l, (Vector2) { r.x, r.y }), level_tile_index_from_position(l, (Vector2) { r.x, r.y }),
tile_index_from_position(l, (Vector2) { r.x + r.width, r.y }), level_tile_index_from_position(l, (Vector2) { r.x + r.width, r.y }),
tile_index_from_position(l, (Vector2) { r.x, r.y + r.height }), level_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.width, r.y + r.height}),
}; };
} }
@ -68,7 +64,6 @@ void draw_tile(Level *l, float x, float y) {
} }
void draw_level(Level *l) { void draw_level(Level *l) {
Camera2D *camera = l->game->camera;
float y = l->game->player->position.y; float y = l->game->player->position.y;
y /= 32; y /= 32;

View File

@ -2,7 +2,9 @@
#define LEVEL_H #define LEVEL_H
typedef struct Level Level; typedef struct Level Level;
typedef struct FourIndices FourIndices;
#include <raylib.h>
#include "game.h" #include "game.h"
struct Level { struct Level {
@ -13,7 +15,13 @@ struct Level {
Game *game; Game *game;
}; };
struct FourIndices {
size_t index[4];
};
void init_level(Game *g, Level *l, char *filepath); void init_level(Game *g, Level *l, char *filepath);
void draw_level(Level *l); void draw_level(Level *l);
FourIndices rect_corners(Level *l, Rectangle r);
#endif #endif

View File

@ -23,6 +23,8 @@ void initialize_player(Game *g, Player *p) {
p->spritesheet = LoadTextureFromImage(sprite_img); p->spritesheet = LoadTextureFromImage(sprite_img);
UnloadImage(sprite_img); UnloadImage(sprite_img);
p->game = g; p->game = g;
p->timer = 2.0;
p->destroyed = false;
} }
void handle_player_input(Player *p) { void handle_player_input(Player *p) {
@ -36,11 +38,17 @@ void handle_player_input(Player *p) {
} }
void draw_player(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); DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE);
}
DrawRectangleRec(p->bbox, YELLOW); DrawRectangleRec(p->bbox, YELLOW);
} }
void move_player(Player *p) { void move_player(Player *p) {
if (p->destroyed) return;
switch (p->roll) { switch (p->roll) {
case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break; case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break;
case ROLL_NEUTRAL: p->acceleration.x = 0; 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) { 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); move_player(p);
update_camera_player(p); update_camera_player(p);
check_collision_player_with_terrain(p); check_collision_player_with_terrain(p);

View File

@ -37,6 +37,7 @@ typedef enum PlayerRoll {
typedef struct Player Player; typedef struct Player Player;
#include "game.h" #include "game.h"
#include "level.h"
struct Player { struct Player {
Vector2 position; Vector2 position;
@ -47,11 +48,13 @@ struct Player {
PlayerRoll roll; PlayerRoll roll;
Texture2D spritesheet; Texture2D spritesheet;
Game *game; Game *game;
float timer;
bool destroyed;
}; };
void initialize_player(Game *g, Player *p); void initialize_player(Game *g, Player *p);
void handle_player_input(Player *p); void handle_player_input(Player *p);
void draw_player(Player *p); void draw_player(Player *p);
void tick_player(Player *p); void tick_player(Player *p, float dt);
#endif #endif