diff --git a/src/rpg/rpgcamera.c b/src/rpg/rpgcamera.c index 2d8b16b..efa76e2 100644 --- a/src/rpg/rpgcamera.c +++ b/src/rpg/rpgcamera.c @@ -19,10 +19,12 @@ void rpgCameraInit(void) { void rpgCameraUpdate(void) { switch(RPG_CAMERA.mode) { case RPG_CAMERA_MODE_FREE: + chunkpos_t chunkPos; + worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos); mapPositionSet((chunkpos_t){ - .x = (RPG_CAMERA.free.x / CHUNK_WIDTH) - (MAP_CHUNK_WIDTH / 2), - .y = (RPG_CAMERA.free.y / CHUNK_HEIGHT) - (MAP_CHUNK_HEIGHT / 2), - .z = (RPG_CAMERA.free.z / CHUNK_DEPTH) - (MAP_CHUNK_DEPTH / 2) + .x = chunkPos.x - (MAP_CHUNK_WIDTH / 2), + .y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2), + .z = chunkPos.z - (MAP_CHUNK_DEPTH / 2) }); break; @@ -32,10 +34,12 @@ void rpgCameraUpdate(void) { // Update map position to match camera. By default map wants to know the // top left but we want to set the center, so we need to sub half map size + chunkpos_t chunkPos; + worldPosToChunkPos(&entity->position, &chunkPos); mapPositionSet((chunkpos_t){ - .x = (entity->position.x / CHUNK_WIDTH) - (MAP_CHUNK_WIDTH / 2), - .y = (entity->position.y / CHUNK_HEIGHT) - (MAP_CHUNK_HEIGHT / 2), - .z = (entity->position.z / CHUNK_DEPTH) - (MAP_CHUNK_DEPTH / 2) + .x = chunkPos.x - (MAP_CHUNK_WIDTH / 2), + .y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2), + .z = chunkPos.z - (MAP_CHUNK_DEPTH / 2) }); break; } diff --git a/src/rpg/world/CMakeLists.txt b/src/rpg/world/CMakeLists.txt index f38a5ec..cec1f00 100644 --- a/src/rpg/world/CMakeLists.txt +++ b/src/rpg/world/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE chunk.c map.c + worldpos.c ) \ No newline at end of file diff --git a/src/rpg/world/map.c b/src/rpg/world/map.c index a194199..d8260b5 100644 --- a/src/rpg/world/map.c +++ b/src/rpg/world/map.c @@ -146,18 +146,15 @@ chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) { }; if( - relPos.x < 0 || relPos.x >= MAP_CHUNK_WIDTH || - relPos.y < 0 || relPos.y >= MAP_CHUNK_HEIGHT || - relPos.z < 0 || relPos.z >= MAP_CHUNK_DEPTH + relPos.x < 0 || relPos.y < 0 || relPos.z < 0 || + relPos.x >= MAP_CHUNK_WIDTH || + relPos.y >= MAP_CHUNK_HEIGHT || + relPos.z >= MAP_CHUNK_DEPTH ) { return -1; } - return ( - (relPos.z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) + - (relPos.y * MAP_CHUNK_WIDTH) + - relPos.x - ); + return chunkPosToIndex(&relPos); } chunk_t* mapGetChunkByIndex(const uint8_t index) { diff --git a/src/rpg/world/worldpos.c b/src/rpg/world/worldpos.c index 86db743..0d48843 100644 --- a/src/rpg/world/worldpos.c +++ b/src/rpg/world/worldpos.c @@ -11,4 +11,18 @@ void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) { out->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH); out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT); out->z = (worldunit_t)(chunkPos->z * CHUNK_DEPTH); +} + +void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) { + out->x = (chunkunit_t)(worldPos->x / CHUNK_WIDTH); + out->y = (chunkunit_t)(worldPos->y / CHUNK_HEIGHT); + out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH); +} + +chunkindex_t chunkPosToIndex(const chunkpos_t* pos) { + return (chunkindex_t)( + (pos->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) + + (pos->y * MAP_CHUNK_WIDTH) + + pos->x + ); } \ No newline at end of file diff --git a/src/rpg/world/worldpos.h b/src/rpg/world/worldpos.h index bc2bb8f..c93d611 100644 --- a/src/rpg/world/worldpos.h +++ b/src/rpg/world/worldpos.h @@ -8,8 +8,8 @@ #pragma once #include "dusk.h" -#define CHUNK_WIDTH 4 -#define CHUNK_HEIGHT 4 +#define CHUNK_WIDTH 16 +#define CHUNK_HEIGHT 16 #define CHUNK_DEPTH 32 #define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) @@ -18,7 +18,7 @@ #define MAP_CHUNK_DEPTH 1 #define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH) -typedef int32_t worldunit_t; +typedef int16_t worldunit_t; typedef int16_t chunkunit_t; typedef int8_t chunkindex_t; @@ -39,4 +39,20 @@ typedef struct chunkpos_t { * @param worldPos The world position. * @param out The output chunk position. */ -void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out); \ No newline at end of file +void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out); + +/** + * Converts a chunk position to a world position. + * + * @param worldPos The world position. + * @param out The output chunk position. + */ +void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out); + +/** + * Converts a chunk position to a world position. + * + * @param worldPos The world position. + * @param out The output chunk position. + */ +chunkindex_t chunkPosToIndex(const chunkpos_t* pos); \ No newline at end of file