Find where the player is on the map

This commit is contained in:
Bill Rossi 2025-03-09 10:17:30 -04:00
parent 499f7871c7
commit 863c2d881f
4 changed files with 47 additions and 2 deletions

2
game.c
View File

@ -97,7 +97,7 @@ void process_bullet_collisions(Game *g, Entity *bullet) {
void run_frame(Game *g) { void run_frame(Game *g) {
handle_input(g); handle_input(g);
move_player(g->player); tick_player(g->player);
FOREACH_ENEMY { FOREACH_ENEMY {
INIT_ENEMY; INIT_ENEMY;
if (entity_expired(e)) { if (entity_expired(e)) {

24
level.c
View File

@ -35,11 +35,35 @@ 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 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}),
};
}
Color COLORS[4] = {BLACK, DARKGRAY, GRAY, LIGHTGRAY}; Color COLORS[4] = {BLACK, DARKGRAY, GRAY, LIGHTGRAY};
void draw_tile(Level *l, float x, float y) { void draw_tile(Level *l, float x, float y) {
size_t index = (size_t)(x + (y * l->width)) % l->data_size; size_t index = (size_t)(x + (y * l->width)) % l->data_size;
size_t data = l->data[index]; size_t data = l->data[index];
Color c = COLORS[data]; Color c = COLORS[data];
FourIndices fi = rect_corners(l, l->game->player->bbox);
if (fi.index[0] == index) c = RED;
if (fi.index[1] == index) c = RED;
if (fi.index[2] == index) c = RED;
if (fi.index[3] == index) c = RED;
DrawRectangle(x * 32, y * 32, 32, 32, c); DrawRectangle(x * 32, y * 32, 32, 32, c);
} }

View File

@ -16,6 +16,7 @@ void initialize_player(Game *g, Player *p) {
p->position = (Vector2) { 400, 300 }; p->position = (Vector2) { 400, 300 };
p->velocity = (Vector2) { 0, 0 }; p->velocity = (Vector2) { 0, 0 };
p->acceleration = (Vector2) { 0, 0 }; p->acceleration = (Vector2) { 0, 0 };
p->bbox = (Rectangle) { 390, 320, 20, 60 };
p->pitch = PITCH_NEUTRAL; p->pitch = PITCH_NEUTRAL;
p->roll = ROLL_NEUTRAL; p->roll = ROLL_NEUTRAL;
Image sprite_img = LoadImage("img/player.png"); Image sprite_img = LoadImage("img/player.png");
@ -36,6 +37,7 @@ void handle_player_input(Player *p) {
void draw_player(Player *p) { void draw_player(Player *p) {
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);
} }
void move_player(Player *p) { void move_player(Player *p) {
@ -61,6 +63,11 @@ void move_player(Player *p) {
p->position.x += p->velocity.x; p->position.x += p->velocity.x;
p->position.y += p->velocity.y; p->position.y += p->velocity.y;
p->bbox.x = p->position.x - (p->bbox.width / 2);
p->bbox.y = p->position.y + 20;
}
void update_camera_player(Player *p) {
p->game->camera->target = p->position; p->game->camera->target = p->position;
p->game->camera->offset.y = 400; p->game->camera->offset.y = 400;
int level_width_in_pixels = p->game->level->width * 32; int level_width_in_pixels = p->game->level->width * 32;
@ -74,3 +81,16 @@ void move_player(Player *p) {
p->game->camera->offset.x = 400; p->game->camera->offset.x = 400;
} }
} }
void check_collision_top_left(Vector2 p) {
}
void check_collision_player_with_terrain(Player *p) {
check_collision_top_left(p->position);
}
void tick_player(Player *p) {
move_player(p);
update_camera_player(p);
check_collision_player_with_terrain(p);
}

View File

@ -42,6 +42,7 @@ struct Player {
Vector2 position; Vector2 position;
Vector2 velocity; Vector2 velocity;
Vector2 acceleration; Vector2 acceleration;
Rectangle bbox;
PlayerPitch pitch; PlayerPitch pitch;
PlayerRoll roll; PlayerRoll roll;
Texture2D spritesheet; Texture2D spritesheet;
@ -51,6 +52,6 @@ struct Player {
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 move_player(Player *p); void tick_player(Player *p);
#endif #endif