This commit is contained in:
2025-11-09 22:17:26 -06:00
parent 8977d50992
commit 3eb24da475
4 changed files with 38 additions and 27 deletions

View File

@@ -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
}

View File

@@ -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)
});
}

View File

@@ -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);

View File

@@ -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.
*