From 81da536e8e4e121a89c809cd94aa1dac60c1e6af Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 19 Jun 2023 21:08:42 +0800 Subject: [PATCH] Add bounding box info in tiles Changelog: - Use more memory, but should help out in implementing spikes --- scenes/editor_scene.c | 3 ++- scenes/game_systems.c | 11 ++++++----- scenes/scene_impl.h | 2 ++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 152720f..5ce8ebc 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -430,7 +430,7 @@ void init_level_scene(LevelScene_t* scene) sc_array_add(&scene->scene.systems, &tile_collision_system); sc_array_add(&scene->scene.systems, &update_tilemap_system); sc_array_add(&scene->scene.systems, &hitbox_update_system); - sc_array_add(&scene->scene.systems, &player_crushing_system); + //sc_array_add(&scene->scene.systems, &player_crushing_system); sc_array_add(&scene->scene.systems, &state_transition_update_system); sc_array_add(&scene->scene.systems, &player_ground_air_transition_system); sc_array_add(&scene->scene.systems, &sprite_animation_system); @@ -462,6 +462,7 @@ void init_level_scene(LevelScene_t* scene) all_tiles[i].solid = NOT_SOLID; all_tiles[i].tile_type = EMPTY_TILE; sc_map_init_64v(&all_tiles[i].entities_set, 16, 0); + all_tiles[i].size = (Vector2){TILE_SIZE, TILE_SIZE}; } for (size_t i = 0; i < scene->data.tilemap.width; ++i) { diff --git a/scenes/game_systems.c b/scenes/game_systems.c index 7a87c17..621f045 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -5,7 +5,6 @@ #include "constants.h" #include -static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static const Vector2 GRAVITY = {0, GRAV_ACCEL}; static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1}; typedef enum AnchorPoint { @@ -60,7 +59,9 @@ static uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, boo find_AABB_overlap( (Vector2){ent->bbox.x, ent->bbox.y}, (Vector2){ent->bbox.width, ent->bbox.height}, - (Vector2){tile_x * TILE_SIZE, tile_y * TILE_SIZE}, TILE_SZ, &overlap + (Vector2){tile_x * TILE_SIZE + grid->tiles[tile_idx].offset.x, tile_y * TILE_SIZE + grid->tiles[tile_idx].offset.y}, + grid->tiles[tile_idx].size, + &overlap ); //For one-way platform, check for vectical collision, only return true for up direction @@ -704,12 +705,12 @@ void tile_collision_system(Scene_t* scene) if(tilemap.tiles[tile_idx].tile_type != EMPTY_TILE) { Vector2 other; - other.x = (tile_idx % tilemap.width) * TILE_SIZE; - other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional + other.x = (tile_idx % tilemap.width) * TILE_SIZE + tilemap.tiles[tile_idx].offset.x; + other.y = (tile_idx / tilemap.width) * TILE_SIZE + tilemap.tiles[tile_idx].offset.y; // Precision loss is intentional check_collision_and_move( &tilemap, p_ent, - &other, TILE_SZ, tilemap.tiles[tile_idx].solid + &other, tilemap.tiles[tile_idx].size, tilemap.tiles[tile_idx].solid ); } diff --git a/scenes/scene_impl.h b/scenes/scene_impl.h index 6143eaf..0f1e36e 100644 --- a/scenes/scene_impl.h +++ b/scenes/scene_impl.h @@ -31,6 +31,8 @@ typedef struct Tile { SolidType_t solid; unsigned int water_level; struct sc_map_64v entities_set; + Vector2 offset; + Vector2 size; }Tile_t; typedef struct TileGrid {