Fixed entity unloading on chunk

This commit is contained in:
2025-06-18 21:20:46 -05:00
parent ee22aadcc7
commit 3f22665e21
6 changed files with 23 additions and 61 deletions

View File

@ -17,7 +17,7 @@ typedef struct {
#define PLAYER_ENTITY_ID (UINT32_MAX-1)
#define PLAYER_MOVE_SPEED FIXED248(1, 0)
#define PLAYER_MOVE_SPEED_XY FIXED248(0, 70)
#define PLAYER_MOVE_SPEED_XY FIXED248(0, 80)
/**
* Initializes the player and all player-related entities.

View File

@ -278,12 +278,17 @@ void chunkUnload(chunk_t *chunk) {
entity++;
} while(entity < ENTITIES + ENTITY_COUNT_MAX);
assertTrue(
entity < ENTITIES + ENTITY_COUNT_MAX,
"Entity ID not found in ENTITIES array, cannot unload"
);
// If the entity is still within our chunk bounds, it's getting unloaded
if(
entity->x >= chunk->x * CHUNK_WIDTH * TILE_WIDTH &&
entity->x < (chunk->x + 1) * CHUNK_WIDTH * TILE_WIDTH &&
entity->y >= chunk->y * CHUNK_HEIGHT * TILE_HEIGHT &&
entity->y < (chunk->y + 1) * CHUNK_HEIGHT * TILE_HEIGHT
fx248Flooru32(entity->x) >= chunk->x * CHUNK_WIDTH * TILE_WIDTH &&
fx248Ceilu32(entity->x) < (chunk->x + 1) * CHUNK_WIDTH * TILE_WIDTH &&
fx248Flooru32(entity->y) >= chunk->y * CHUNK_HEIGHT * TILE_HEIGHT &&
fx248Ceilu32(entity->y) < (chunk->y + 1) * CHUNK_HEIGHT * TILE_HEIGHT
) {
shouldUnload = true;
} else {
@ -298,13 +303,7 @@ void chunkUnload(chunk_t *chunk) {
// This entity is still in use, leave it loaded.
if(!shouldUnload) continue;
assertTrue(
entity < ENTITIES + ENTITY_COUNT_MAX,
"Entity ID not found for unloading"
);
// NULL the entity type, effectively unloading it.
printf("Unloading entity ID %u\n", entity->id);
entity->type = ENTITY_TYPE_NULL;
}
}

View File

@ -9,7 +9,7 @@
#include "tile.h"
#define CHUNK_MAP_WIDTH 4
#define CHUNK_MAP_HEIGHT 4
#define CHUNK_MAP_HEIGHT 3
#define CHUNK_MAP_COUNT (CHUNK_MAP_WIDTH * CHUNK_MAP_HEIGHT)
#define CHUNK_WIDTH 8

View File

@ -51,16 +51,21 @@ void overworldUpdate() {
OVERWORLD_CAMERA_Y = fx248Flooru32(entity->y);
uint16_t x, y;
if(OVERWORLD_CAMERA_X < RENDER_WIDTH / 2) {
uint16_t halfWidth, halfHeight;
halfWidth = ((CHUNK_MAP_WIDTH - 1) * CHUNK_WIDTH * TILE_WIDTH) / 2;
halfHeight = ((CHUNK_MAP_HEIGHT - 1) * CHUNK_HEIGHT * TILE_HEIGHT) / 2;
// Calculate the chunk map position based on the camera position.
if(OVERWORLD_CAMERA_X < halfWidth) {
x = 0;
} else {
x = (OVERWORLD_CAMERA_X - (RENDER_WIDTH / 2)) / (CHUNK_WIDTH*TILE_WIDTH);
x = (OVERWORLD_CAMERA_X - halfWidth) / (CHUNK_WIDTH*TILE_WIDTH);
}
if(OVERWORLD_CAMERA_Y < RENDER_HEIGHT / 2) {
if(OVERWORLD_CAMERA_Y < halfHeight) {
y = 0;
} else {
y = (OVERWORLD_CAMERA_Y -(RENDER_HEIGHT / 2)) / (CHUNK_HEIGHT*TILE_HEIGHT);
y = (OVERWORLD_CAMERA_Y - halfHeight) / (CHUNK_HEIGHT*TILE_HEIGHT);
}
chunkMapSetPosition(x, y);

View File

@ -105,7 +105,7 @@ void drawOverworldDraw(void) {
entity_t *entity = ENTITIES;
do {
drawOverworldDrawEntity(entity++);
} while(entity->type != ENTITY_TYPE_NULL);
} while(entity < ENTITIES + ENTITY_COUNT_MAX);
EndMode2D();
}
@ -113,6 +113,7 @@ void drawOverworldDraw(void) {
void drawOverworldDrawEntity(const entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
if(entity->type == ENTITY_TYPE_NULL) return; // Skip null entities
uint32_t x = fx248Tou32(entity->x);
uint32_t y = fx248Tou32(entity->y);
@ -130,47 +131,4 @@ void drawOverworldDrawEntity(const entity_t *entity) {
(Vector2){ x, y },
WHITE
);
// DrawRectangle(
// x,
// y,
// TILE_WIDTH,
// TILE_HEIGHT,
// (entity->type == ENTITY_TYPE_PLAYER) ? BLUE : YELLOW
// );
// switch(entity->dir) {
// case ENTITY_DIR_NORTH:
// DrawTriangle(
// (Vector2){ x + TILE_WIDTH / 2, y },
// (Vector2){ x, y + TILE_HEIGHT },
// (Vector2){ x + TILE_WIDTH, y + TILE_HEIGHT },
// WHITE
// );
// break;
// case ENTITY_DIR_SOUTH:
// DrawTriangle(
// (Vector2){ x + TILE_WIDTH / 2, y + TILE_HEIGHT },
// (Vector2){ x + TILE_WIDTH, y },
// (Vector2){ x, y },
// WHITE
// );
// break;
// case ENTITY_DIR_WEST:
// DrawTriangle(
// (Vector2){ x, y + TILE_HEIGHT / 2 },
// (Vector2){ x + TILE_WIDTH, y + TILE_HEIGHT },
// (Vector2){ x + TILE_WIDTH, y },
// WHITE
// );
// break;
// case ENTITY_DIR_EAST:
// DrawTriangle(
// (Vector2){ x + TILE_WIDTH, y + TILE_HEIGHT / 2 },
// (Vector2){ x, y },
// (Vector2){ x, y + TILE_HEIGHT },
// WHITE
// );
// break;
// }
}

View File

@ -188,7 +188,7 @@ for layerIndex, layer in enumerate(tileLayers):
# Pre generate entity data
nextEntityId = 1
nextEntityId = 100
for obIndex, ob in enumerate(objectLayer['objects']):
if 'x' not in ob or 'y' not in ob:
print(f"Error: Object in object layer does not contain 'x' or 'y' key.")