diff --git a/src/rpg/entity/entity.c b/src/rpg/entity/entity.c index ba3c32d..b42fff0 100644 --- a/src/rpg/entity/entity.c +++ b/src/rpg/entity/entity.c @@ -67,34 +67,36 @@ void entityWalk(entity_t *entity, const entitydir_t direction) { entity->direction = direction; // Where are we moving? - worldunit_t newX, newY; - newX = entity->position.x; - newY = entity->position.y; + worldpos_t newPos = entity->position; { worldunits_t relX, relY; entityDirGetRelative(direction, &relX, &relY); - newX += relX; - newY += relY; + newPos.x += relX; + newPos.y += relY; } // TODO: Tile in way? - // TODO: Map bounds in way? + chunkpos_t chunkPos; + worldPosToChunkPos(&newPos, &chunkPos); + chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos); + if(chunkIndex != -1) { + chunk_t *chunk = mapGetChunkByIndex((uint8_t)chunkIndex); + assertNotbNull(chunk, "Chunk pointer cannot be NULL"); + chunkGetTile + } else { + return;// Out of bounds + } // Entity in way? entity_t *other = ENTITIES; do { if(other == entity) continue; if(other->type == ENTITY_TYPE_NULL) continue; - if(other->position.x != newX) continue; - if(other->position.y != newY) continue; - if(other->position.z != entity->position.z) continue; + if(!worldPosIsEqual(other->position, newPos)) continue; return;// Blocked } while(++other, other < &ENTITIES[ENTITY_COUNT]); - // Move. - entity->position.x = newX; - entity->position.y = newY; - + entity->position = newPos; entity->animation = ENTITY_ANIM_WALK; entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking } diff --git a/src/rpg/rpgcamera.c b/src/rpg/rpgcamera.c index efa76e2..18de59a 100644 --- a/src/rpg/rpgcamera.c +++ b/src/rpg/rpgcamera.c @@ -9,6 +9,7 @@ #include "util/memory.h" #include "rpg/entity/entity.h" #include "rpg/world/map.h" +#include "assert/assert.h" rpgcamera_t RPG_CAMERA; @@ -17,15 +18,11 @@ void rpgCameraInit(void) { } void rpgCameraUpdate(void) { + chunkpos_t chunkPos; + switch(RPG_CAMERA.mode) { case RPG_CAMERA_MODE_FREE: - chunkpos_t chunkPos; worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos); - mapPositionSet((chunkpos_t){ - .x = chunkPos.x - (MAP_CHUNK_WIDTH / 2), - .y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2), - .z = chunkPos.z - (MAP_CHUNK_DEPTH / 2) - }); break; case RPG_CAMERA_MODE_FOLLOW_ENTITY: { @@ -34,18 +31,17 @@ 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 = chunkPos.x - (MAP_CHUNK_WIDTH / 2), - .y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2), - .z = chunkPos.z - (MAP_CHUNK_DEPTH / 2) - }); break; } default: - // Unknown camera mode; do nothing. - break; + assertUnreachable("Invalid RPG camera mode"); } + + mapPositionSet((chunkpos_t){ + .x = chunkPos.x - (MAP_CHUNK_WIDTH / 2), + .y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2), + .z = chunkPos.z - (MAP_CHUNK_DEPTH / 2) + }); } \ No newline at end of file diff --git a/src/rpg/world/worldpos.c b/src/rpg/world/worldpos.c index 0d48843..329eff0 100644 --- a/src/rpg/world/worldpos.c +++ b/src/rpg/world/worldpos.c @@ -7,6 +7,10 @@ #include "worldpos.h" +bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) { + return a.x == b.x && a.y == b.y && a.z == b.z; +} + 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); diff --git a/src/rpg/world/worldpos.h b/src/rpg/world/worldpos.h index c93d611..878c0d8 100644 --- a/src/rpg/world/worldpos.h +++ b/src/rpg/world/worldpos.h @@ -33,6 +33,15 @@ typedef struct chunkpos_t { chunkunit_t x, y, z; } chunkpos_t; +/** + * Compares two world positions for equality. + * + * @param a The first world position. + * @param b The second world position. + * @return true if equal, false otherwise. + */ +bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b); + /** * Converts a world position to a chunk position. *