From 562da971e9850512285ed0b96f85be5c31099398 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 10 Nov 2025 11:01:41 -0600 Subject: [PATCH] Tile under foot --- src/input/input.c | 2 ++ src/rpg/entity/entity.c | 12 ++++++------ src/rpg/world/map.c | 2 +- src/rpg/world/tile.h | 5 ++++- src/rpg/world/worldpos.c | 12 ++++++++++++ src/rpg/world/worldpos.h | 12 +++++++++++- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/input/input.c b/src/input/input.c index 2cc69d1..73c4ff4 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -46,6 +46,8 @@ void inputInit(void) { #if INPUT_GAMEPAD == 1 #if PSP + INPUT.deadzone = 0.2890625f;// Taken from the PSP firmware + inputBind(inputButtonGetByName("up"), INPUT_ACTION_UP); inputBind(inputButtonGetByName("down"), INPUT_ACTION_DOWN); inputBind(inputButtonGetByName("left"), INPUT_ACTION_LEFT); diff --git a/src/rpg/entity/entity.c b/src/rpg/entity/entity.c index b42fff0..bbb34c2 100644 --- a/src/rpg/entity/entity.c +++ b/src/rpg/entity/entity.c @@ -75,16 +75,16 @@ void entityWalk(entity_t *entity, const entitydir_t direction) { newPos.y += relY; } - // TODO: Tile in way? + // Get tile under foot chunkpos_t chunkPos; worldPosToChunkPos(&newPos, &chunkPos); chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos); + tile_t tile = TILE_NULL; if(chunkIndex != -1) { - chunk_t *chunk = mapGetChunkByIndex((uint8_t)chunkIndex); - assertNotbNull(chunk, "Chunk pointer cannot be NULL"); - chunkGetTile - } else { - return;// Out of bounds + chunk_t *chunk = mapGetChunk(chunkIndex); + assertNotNull(chunk, "Chunk pointer cannot be NULL"); + chunktileindex_t tileIndex = woprldPosToChunkTileIndex(&newPos); + tile = chunk->tiles[tileIndex]; } // Entity in way? diff --git a/src/rpg/world/map.c b/src/rpg/world/map.c index d8260b5..2883c89 100644 --- a/src/rpg/world/map.c +++ b/src/rpg/world/map.c @@ -157,7 +157,7 @@ chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) { return chunkPosToIndex(&relPos); } -chunk_t* mapGetChunkByIndex(const uint8_t index) { +chunk_t* mapGetChunk(const uint8_t index) { if(index >= MAP_CHUNK_COUNT) return NULL; return &MAP.chunks[index]; } \ No newline at end of file diff --git a/src/rpg/world/tile.h b/src/rpg/world/tile.h index d601fa1..dbc923a 100644 --- a/src/rpg/world/tile.h +++ b/src/rpg/world/tile.h @@ -8,6 +8,9 @@ #pragma once #include "dusk.h" + typedef struct tile_s { uint8_t id; -} tile_t; \ No newline at end of file +} tile_t; + +#define TILE_NULL ((tile_t){ .id = 0 }) \ No newline at end of file diff --git a/src/rpg/world/worldpos.c b/src/rpg/world/worldpos.c index 329eff0..71715db 100644 --- a/src/rpg/world/worldpos.c +++ b/src/rpg/world/worldpos.c @@ -23,6 +23,18 @@ void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) { out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH); } +chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos) { + uint8_t localX = (uint8_t)(worldPos->x % CHUNK_WIDTH); + uint8_t localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT); + uint8_t localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH); + + return (chunktileindex_t)( + (localZ * CHUNK_WIDTH * CHUNK_HEIGHT) + + (localY * CHUNK_WIDTH) + + localX + ); +} + chunkindex_t chunkPosToIndex(const chunkpos_t* pos) { return (chunkindex_t)( (pos->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) + diff --git a/src/rpg/world/worldpos.h b/src/rpg/world/worldpos.h index 878c0d8..401f92d 100644 --- a/src/rpg/world/worldpos.h +++ b/src/rpg/world/worldpos.h @@ -20,7 +20,8 @@ typedef int16_t worldunit_t; typedef int16_t chunkunit_t; -typedef int8_t chunkindex_t; +typedef int16_t chunkindex_t; +typedef uint8_t chunktileindex_t; typedef int32_t worldunits_t; typedef int32_t chunkunits_t; @@ -58,6 +59,15 @@ void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out); */ void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out); +/** + * Converts a position in world-space to an index inside a chunk that the tile + * resides in. + * + * @param worldPos The world position. + * @return The tile index within the chunk. + */ +chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos); + /** * Converts a chunk position to a world position. *