Fixed entity unloading on chunk
This commit is contained in:
@ -17,7 +17,7 @@ typedef struct {
|
|||||||
|
|
||||||
#define PLAYER_ENTITY_ID (UINT32_MAX-1)
|
#define PLAYER_ENTITY_ID (UINT32_MAX-1)
|
||||||
#define PLAYER_MOVE_SPEED FIXED248(1, 0)
|
#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.
|
* Initializes the player and all player-related entities.
|
||||||
|
@ -278,12 +278,17 @@ void chunkUnload(chunk_t *chunk) {
|
|||||||
entity++;
|
entity++;
|
||||||
} while(entity < ENTITIES + ENTITY_COUNT_MAX);
|
} 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 the entity is still within our chunk bounds, it's getting unloaded
|
||||||
if(
|
if(
|
||||||
entity->x >= chunk->x * CHUNK_WIDTH * TILE_WIDTH &&
|
fx248Flooru32(entity->x) >= chunk->x * CHUNK_WIDTH * TILE_WIDTH &&
|
||||||
entity->x < (chunk->x + 1) * CHUNK_WIDTH * TILE_WIDTH &&
|
fx248Ceilu32(entity->x) < (chunk->x + 1) * CHUNK_WIDTH * TILE_WIDTH &&
|
||||||
entity->y >= chunk->y * CHUNK_HEIGHT * TILE_HEIGHT &&
|
fx248Flooru32(entity->y) >= chunk->y * CHUNK_HEIGHT * TILE_HEIGHT &&
|
||||||
entity->y < (chunk->y + 1) * CHUNK_HEIGHT * TILE_HEIGHT
|
fx248Ceilu32(entity->y) < (chunk->y + 1) * CHUNK_HEIGHT * TILE_HEIGHT
|
||||||
) {
|
) {
|
||||||
shouldUnload = true;
|
shouldUnload = true;
|
||||||
} else {
|
} else {
|
||||||
@ -298,13 +303,7 @@ void chunkUnload(chunk_t *chunk) {
|
|||||||
// This entity is still in use, leave it loaded.
|
// This entity is still in use, leave it loaded.
|
||||||
if(!shouldUnload) continue;
|
if(!shouldUnload) continue;
|
||||||
|
|
||||||
assertTrue(
|
|
||||||
entity < ENTITIES + ENTITY_COUNT_MAX,
|
|
||||||
"Entity ID not found for unloading"
|
|
||||||
);
|
|
||||||
|
|
||||||
// NULL the entity type, effectively unloading it.
|
// NULL the entity type, effectively unloading it.
|
||||||
printf("Unloading entity ID %u\n", entity->id);
|
|
||||||
entity->type = ENTITY_TYPE_NULL;
|
entity->type = ENTITY_TYPE_NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@
|
|||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
|
|
||||||
#define CHUNK_MAP_WIDTH 4
|
#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_MAP_COUNT (CHUNK_MAP_WIDTH * CHUNK_MAP_HEIGHT)
|
||||||
|
|
||||||
#define CHUNK_WIDTH 8
|
#define CHUNK_WIDTH 8
|
||||||
|
@ -51,16 +51,21 @@ void overworldUpdate() {
|
|||||||
OVERWORLD_CAMERA_Y = fx248Flooru32(entity->y);
|
OVERWORLD_CAMERA_Y = fx248Flooru32(entity->y);
|
||||||
|
|
||||||
uint16_t x, 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;
|
x = 0;
|
||||||
} else {
|
} 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;
|
y = 0;
|
||||||
} else {
|
} else {
|
||||||
y = (OVERWORLD_CAMERA_Y -(RENDER_HEIGHT / 2)) / (CHUNK_HEIGHT*TILE_HEIGHT);
|
y = (OVERWORLD_CAMERA_Y - halfHeight) / (CHUNK_HEIGHT*TILE_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkMapSetPosition(x, y);
|
chunkMapSetPosition(x, y);
|
||||||
|
@ -105,7 +105,7 @@ void drawOverworldDraw(void) {
|
|||||||
entity_t *entity = ENTITIES;
|
entity_t *entity = ENTITIES;
|
||||||
do {
|
do {
|
||||||
drawOverworldDrawEntity(entity++);
|
drawOverworldDrawEntity(entity++);
|
||||||
} while(entity->type != ENTITY_TYPE_NULL);
|
} while(entity < ENTITIES + ENTITY_COUNT_MAX);
|
||||||
|
|
||||||
EndMode2D();
|
EndMode2D();
|
||||||
}
|
}
|
||||||
@ -113,6 +113,7 @@ void drawOverworldDraw(void) {
|
|||||||
|
|
||||||
void drawOverworldDrawEntity(const entity_t *entity) {
|
void drawOverworldDrawEntity(const entity_t *entity) {
|
||||||
assertNotNull(entity, "Entity pointer cannot be NULL");
|
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 x = fx248Tou32(entity->x);
|
||||||
uint32_t y = fx248Tou32(entity->y);
|
uint32_t y = fx248Tou32(entity->y);
|
||||||
@ -130,47 +131,4 @@ void drawOverworldDrawEntity(const entity_t *entity) {
|
|||||||
(Vector2){ x, y },
|
(Vector2){ x, y },
|
||||||
WHITE
|
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;
|
|
||||||
// }
|
|
||||||
}
|
}
|
@ -188,7 +188,7 @@ for layerIndex, layer in enumerate(tileLayers):
|
|||||||
|
|
||||||
|
|
||||||
# Pre generate entity data
|
# Pre generate entity data
|
||||||
nextEntityId = 1
|
nextEntityId = 100
|
||||||
for obIndex, ob in enumerate(objectLayer['objects']):
|
for obIndex, ob in enumerate(objectLayer['objects']):
|
||||||
if 'x' not in ob or 'y' not in ob:
|
if 'x' not in ob or 'y' not in ob:
|
||||||
print(f"Error: Object in object layer does not contain 'x' or 'y' key.")
|
print(f"Error: Object in object layer does not contain 'x' or 'y' key.")
|
||||||
|
Reference in New Issue
Block a user