This commit is contained in:
2025-11-11 07:50:20 -06:00
parent c07d0b32a9
commit 26bfb912f1
6 changed files with 66 additions and 24 deletions

View File

@@ -76,17 +76,19 @@ void entityWalk(entity_t *entity, const entitydir_t direction) {
} }
// Get tile under foot // Get tile under foot
chunkpos_t chunkPos; tile_t tile = mapGetTile(newPos);
worldPosToChunkPos(&newPos, &chunkPos); if(tile == TILE_NULL && newPos.z > 0) {
chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos); // Check one level down for walkable tile (stairs down)
tile_t tile = TILE_NULL; worldpos_t belowPos = newPos;
if(chunkIndex != -1) { belowPos.z -= 1;
chunk_t *chunk = mapGetChunk(chunkIndex); tile = mapGetTile(belowPos);
assertNotNull(chunk, "Chunk pointer cannot be NULL");
chunktileindex_t tileIndex = woprldPosToChunkTileIndex(&newPos); if(tile != TILE_NULL) newPos.z -= 1;
tile = chunk->tiles[tileIndex];
} }
// Tile walkable?
if(!tileIsWalkable(tile)) return;
// Entity in way? // Entity in way?
entity_t *other = ENTITIES; entity_t *other = ENTITIES;
do { do {
@@ -99,6 +101,8 @@ void entityWalk(entity_t *entity, const entitydir_t direction) {
entity->position = newPos; entity->position = newPos;
entity->animation = ENTITY_ANIM_WALK; entity->animation = ENTITY_ANIM_WALK;
entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking
// We are comitting, we can run effects here.
} }
entity_t * entityGetAt(const worldpos_t position) { entity_t * entityGetAt(const worldpos_t position) {

View File

@@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME}
chunk.c chunk.c
map.c map.c
worldpos.c worldpos.c
tile.c
) )

View File

@@ -7,6 +7,7 @@
#include "map.h" #include "map.h"
#include "util/memory.h" #include "util/memory.h"
#include "assert/assert.h"
map_t MAP; map_t MAP;
@@ -126,16 +127,13 @@ void mapChunkLoad(chunk_t* chunk) {
); );
memoryZero(chunk->tiles, sizeof(tile_t) * CHUNK_TILE_COUNT); memoryZero(chunk->tiles, sizeof(tile_t) * CHUNK_TILE_COUNT);
uint8_t x, y, z; // 3x3 test walkable area
x = 1; for(int y = 0; y <= 3; y++) {
y = 2; for(int x = 0; x <= 3; x++) {
z = 0; chunktileindex_t tileIndex = (y * CHUNK_WIDTH) + x;
chunk->tiles[tileIndex] = TILE_WALKABLE;
chunk->tiles[ }
(z * CHUNK_WIDTH * CHUNK_HEIGHT) + }
(y * CHUNK_WIDTH) +
x
] = (tile_t){ .id = 1 };
} }
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) { chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
@@ -161,3 +159,15 @@ chunk_t* mapGetChunk(const uint8_t index) {
if(index >= MAP_CHUNK_COUNT) return NULL; if(index >= MAP_CHUNK_COUNT) return NULL;
return &MAP.chunks[index]; return &MAP.chunks[index];
} }
tile_t mapGetTile(const worldpos_t position) {
chunkpos_t chunkPos;
worldPosToChunkPos(&position, &chunkPos);
chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos);
if(chunkIndex == -1) return TILE_NULL;
chunk_t *chunk = mapGetChunk(chunkIndex);
assertNotNull(chunk, "Chunk pointer cannot be NULL");
chunktileindex_t tileIndex = woprldPosToChunkTileIndex(&position);
return chunk->tiles[tileIndex];
}

View File

@@ -62,3 +62,11 @@ chunkindex_t mapGetChunkIndexAt(const chunkpos_t position);
* @return A pointer to the chunk. * @return A pointer to the chunk.
*/ */
chunk_t * mapGetChunk(const uint8_t chunkIndex); chunk_t * mapGetChunk(const uint8_t chunkIndex);
/**
* Gets the tile at the given world position.
*
* @param position The world position.
* @return The tile at that position, or TILE_NULL if the chunk is unloaded.
*/
tile_t mapGetTile(const worldpos_t position);

12
src/rpg/world/tile.c Normal file
View File

@@ -0,0 +1,12 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tile.h"
bool_t tileIsWalkable(const tile_t tile) {
return tile == TILE_WALKABLE;
}

View File

@@ -9,8 +9,15 @@
#include "dusk.h" #include "dusk.h"
typedef struct tile_s { typedef uint8_t tile_t;
uint8_t id;
} tile_t;
#define TILE_NULL ((tile_t){ .id = 0 }) #define TILE_NULL 0
#define TILE_WALKABLE 1
/**
* Returns whether or not the given tile is walkable.
*
* @param tile The tile to check.
* @return bool_t True if walkable, false if not.
*/
bool_t tileIsWalkable(const tile_t tile);