entity pos sets chunk now

This commit is contained in:
2026-07-04 18:43:09 -05:00
parent 589e4224f3
commit 85bf455731
5 changed files with 48 additions and 22 deletions
@@ -8,7 +8,6 @@
#include "rpg/cutscene/item/cutsceneitem.h" #include "rpg/cutscene/item/cutsceneitem.h"
#include "rpg/cutscene/cutscenesystem.h" #include "rpg/cutscene/cutscenesystem.h"
#include "rpg/entity/entity.h" #include "rpg/entity/entity.h"
#include "rpg/overworld/map.h"
#include "assert/assert.h" #include "assert/assert.h"
void cutsceneEntityAddStart( void cutsceneEntityAddStart(
@@ -20,12 +19,7 @@ void cutsceneEntityAddStart(
entity_t *entity = &ENTITIES[entIndex]; entity_t *entity = &ENTITIES[entIndex];
entityInit(entity, item->entityAdd.entityType); entityInit(entity, item->entityAdd.entityType);
entityPositionSet(entity, item->entityAdd.position); entityPositionSet(entity, item->entityAdd.position);// Also assigns chunk.
chunkpos_t cp;
worldPosToChunkPos(&entity->position, &cp);
chunkindex_t ci = mapGetChunkIndexAt(cp);
if(ci != -1) entitySetChunk(entity, (uint8_t)ci);
CUTSCENE_SYSTEM.entityLastCreated = entity; CUTSCENE_SYSTEM.entityLastCreated = entity;
CUTSCENE_SYSTEM.entityLastRef = entity; CUTSCENE_SYSTEM.entityLastRef = entity;
+22
View File
@@ -223,6 +223,7 @@ 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
entityUpdateChunk(entity);
} }
void entityRun(entity_t *entity, const entitydir_t direction) { void entityRun(entity_t *entity, const entitydir_t direction) {
@@ -245,6 +246,17 @@ entity_t * entityGetAt(const worldpos_t position) {
return NULL; return NULL;
} }
entity_t * entityGetByGlobalId(const entityglobalid_t globalId) {
entity_t *ent = ENTITIES;
do {
if(ent->type == ENTITY_TYPE_NULL) continue;
if(ent->globalId != globalId) continue;
return ent;
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
return NULL;
}
uint8_t entityGetAvailable() { uint8_t entityGetAvailable() {
entity_t *ent = ENTITIES; entity_t *ent = ENTITIES;
do { do {
@@ -261,6 +273,7 @@ void entityPositionSet(entity_t *entity, const worldpos_t pos) {
entity->animation = ENTITY_ANIM_IDLE; entity->animation = ENTITY_ANIM_IDLE;
entity->animTime = 0; entity->animTime = 0;
entity->walkEndCooldown = 0; entity->walkEndCooldown = 0;
entityUpdateChunk(entity);
} }
void entitySetChunk(entity_t *entity, const uint8_t chunkIndex) { void entitySetChunk(entity_t *entity, const uint8_t chunkIndex) {
@@ -290,3 +303,12 @@ void entitySetChunk(entity_t *entity, const uint8_t chunkIndex) {
} }
} }
} }
void entityUpdateChunk(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
chunkpos_t cp;
worldPosToChunkPos(&entity->position, &cp);
chunkindex_t ci = mapGetChunkIndexAt(cp);
if(ci != -1) entitySetChunk(entity, (uint8_t)ci);
}
+17
View File
@@ -125,6 +125,14 @@ void entityRun(entity_t *entity, const entitydir_t direction);
*/ */
entity_t *entityGetAt(const worldpos_t pos); entity_t *entityGetAt(const worldpos_t pos);
/**
* Gets the entity with the given global ID, if one is currently loaded.
*
* @param globalId The global ID to search for.
* @return Pointer to the matching entity, or NULL if none is loaded.
*/
entity_t *entityGetByGlobalId(const entityglobalid_t globalId);
/** /**
* Gets an available entity index. * Gets an available entity index.
* *
@@ -141,6 +149,15 @@ uint8_t entityGetAvailable();
*/ */
void entitySetChunk(entity_t *entity, const uint8_t chunkIndex); void entitySetChunk(entity_t *entity, const uint8_t chunkIndex);
/**
* Resolves the chunk that an entity's current position falls into and
* assigns the entity to it via entitySetChunk. Leaves the entity's chunk
* unchanged if its position doesn't fall within any loaded chunk.
*
* @param entity Pointer to the entity to update.
*/
void entityUpdateChunk(entity_t *entity);
/** /**
* Instantly moves an entity to a world position, resetting movement state. * Instantly moves an entity to a world position, resetting movement state.
* *
+6 -7
View File
@@ -281,6 +281,11 @@ entity_t * mapSpawnEntity(
"Global ID is out of range for entity global init callbacks" "Global ID is out of range for entity global init callbacks"
); );
// Already spawned? Reuse the existing entity instead of making a
// duplicate - two entities must never share a global ID.
entity_t *existing = entityGetByGlobalId(globalId);
if(existing != NULL) return existing;
// See if there is a callback for this entity first. // See if there is a callback for this entity first.
const entityglobaldef_t *def = &ENTITY_GLOBAL_LIST[globalId]; const entityglobaldef_t *def = &ENTITY_GLOBAL_LIST[globalId];
assertNotNull(def, "No global entity definition for this ID"); assertNotNull(def, "No global entity definition for this ID");
@@ -294,13 +299,7 @@ entity_t * mapSpawnEntity(
entity_t *entity = &ENTITIES[index]; entity_t *entity = &ENTITIES[index];
entityInit(entity, def->type); entityInit(entity, def->type);
entity->globalId = globalId; entity->globalId = globalId;
entityPositionSet(entity, position); entityPositionSet(entity, position);// Also assigns the entity's chunk.
// Assign to the local chunk.
chunkpos_t cp;
worldPosToChunkPos(&entity->position, &cp);
chunkindex_t ci = mapGetChunkIndexAt(cp);
if(ci != -1) entitySetChunk(entity, (uint8_t)ci);
// Invoke the callback to initialize the entity. // Invoke the callback to initialize the entity.
entityglobalcreate_t create = { entityglobalcreate_t create = {
+1 -7
View File
@@ -35,15 +35,9 @@ errorret_t rpgInit(void) {
assertTrue(entIndex != 0xFF, "No available entity slots!."); assertTrue(entIndex != 0xFF, "No available entity slots!.");
entity_t *ent = &ENTITIES[entIndex]; entity_t *ent = &ENTITIES[entIndex];
entityInit(ent, ENTITY_TYPE_PLAYER); entityInit(ent, ENTITY_TYPE_PLAYER);
entityPositionSet(ent, (worldpos_t){ 10, 2, 0 }); entityPositionSet(ent, (worldpos_t){ 10, 2, 0 });// Also assigns the chunk.
RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY; RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY;
RPG_CAMERA.followEntity.followEntityId = ent->id; RPG_CAMERA.followEntity.followEntityId = ent->id;
{
chunkpos_t cp;
worldPosToChunkPos(&ent->position, &cp);
chunkindex_t ci = mapGetChunkIndexAt(cp);
if(ci != -1) entitySetChunk(ent, (uint8_t)ci);
}
mapSpawnEntity(3, (worldpos_t){ 8, 8, 1 }); mapSpawnEntity(3, (worldpos_t){ 8, 8, 1 });