From ad9e8a78d3804f8395b209da0e6d6e18c438ed0e Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 4 Mar 2025 10:51:22 -0600 Subject: [PATCH] Cleanup overworld. --- src/dusk/assert/assert.h | 4 +- src/dusk/overworld/CMakeLists.txt | 7 ++-- src/dusk/overworld/entity/CMakeLists.txt | 15 +++++++ src/dusk/overworld/{ => entity}/entity.c | 32 ++++----------- src/dusk/overworld/{ => entity}/entity.h | 14 +++---- src/dusk/overworld/entity/facing.c | 50 +++++++++++++++++++++++ src/dusk/overworld/entity/facing.h | 36 ++++++++++++++++ src/dusk/overworld/entity/npc.c | 17 ++++++++ src/dusk/overworld/entity/npc.h | 36 ++++++++++++++++ src/dusk/overworld/{ => entity}/player.c | 3 +- src/dusk/overworld/{ => entity}/player.h | 0 src/dusk/overworld/map/CMakeLists.txt | 12 ++++++ src/dusk/overworld/map/map.c | 14 +++++++ src/dusk/overworld/map/map.h | 18 ++++++++ src/dusk/overworld/{ => map}/tile.c | 0 src/dusk/overworld/{ => map}/tile.h | 0 src/dusk/overworld/overworld.c | 3 ++ src/dusk/overworld/overworld.h | 4 +- src/duskgl/display/shader/data/entities.c | 2 +- src/duskgl/overworld/overworld.c | 4 +- 20 files changed, 229 insertions(+), 42 deletions(-) create mode 100644 src/dusk/overworld/entity/CMakeLists.txt rename src/dusk/overworld/{ => entity}/entity.c (82%) rename src/dusk/overworld/{ => entity}/entity.h (88%) create mode 100644 src/dusk/overworld/entity/facing.c create mode 100644 src/dusk/overworld/entity/facing.h create mode 100644 src/dusk/overworld/entity/npc.c create mode 100644 src/dusk/overworld/entity/npc.h rename src/dusk/overworld/{ => entity}/player.c (90%) rename src/dusk/overworld/{ => entity}/player.h (100%) create mode 100644 src/dusk/overworld/map/CMakeLists.txt create mode 100644 src/dusk/overworld/map/map.c create mode 100644 src/dusk/overworld/map/map.h rename src/dusk/overworld/{ => map}/tile.c (100%) rename src/dusk/overworld/{ => map}/tile.h (100%) diff --git a/src/dusk/assert/assert.h b/src/dusk/assert/assert.h index b98d192..090d38f 100644 --- a/src/dusk/assert/assert.h +++ b/src/dusk/assert/assert.h @@ -126,4 +126,6 @@ void assertMemoryRangeMatchesImpl( assertTrue(strlen(str) <= len, message) #define assertStrLenMin(str, len, message) \ - assertTrue(strlen(str) >= len, message) \ No newline at end of file + assertTrue(strlen(str) >= len, message) + +// EOF \ No newline at end of file diff --git a/src/dusk/overworld/CMakeLists.txt b/src/dusk/overworld/CMakeLists.txt index 3d0218f..06f242b 100644 --- a/src/dusk/overworld/CMakeLists.txt +++ b/src/dusk/overworld/CMakeLists.txt @@ -6,10 +6,9 @@ # Sources target_sources(${DUSK_TARGET_NAME} PRIVATE - entity.c overworld.c - player.c - tile.c ) -# Subdirs \ No newline at end of file +# Subdirs +add_subdirectory(entity) +add_subdirectory(map) \ No newline at end of file diff --git a/src/dusk/overworld/entity/CMakeLists.txt b/src/dusk/overworld/entity/CMakeLists.txt new file mode 100644 index 0000000..f0dac1f --- /dev/null +++ b/src/dusk/overworld/entity/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + entity.c + facing.c + player.c + npc.c +) + +# Subdirs \ No newline at end of file diff --git a/src/dusk/overworld/entity.c b/src/dusk/overworld/entity/entity.c similarity index 82% rename from src/dusk/overworld/entity.c rename to src/dusk/overworld/entity/entity.c index c1f1595..d4b9ec3 100644 --- a/src/dusk/overworld/entity.c +++ b/src/dusk/overworld/entity/entity.c @@ -8,11 +8,12 @@ #include "entity.h" #include "assert/assert.h" #include "util/memory.h" -#include "overworld.h" +#include "overworld/overworld.h" entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = { - { NULL, NULL }, - { playerInit, playerUpdate } + { NULL, NULL, NULL }, + { playerInit, playerUpdate, NULL }, + { npcInit, npcUpdate, npcInteract } }; entity_t ENTITY_TEST; @@ -69,26 +70,11 @@ void entityMove(entity_t *ent, const facingdir_t dir) { entityTurn(ent, dir); uint8_t targetX = ent->x, targetY = ent->y; - switch(dir) { - case FACING_DIRECTION_EAST: - if(ent->x >= OVERWORLD.mapWidth - 1) return; - targetX++; - break; - case FACING_DIRECTION_WEST: - if(ent->x == 0) return; - targetX--; - break; - case FACING_DIRECTION_NORTH: - if(ent->y >= OVERWORLD.mapHeight - 1) return; - targetY++; - break; - case FACING_DIRECTION_SOUTH: - if(ent->y == 0) return; - targetY--; - break; - default: - assertUnreachable("Invalid facing direction"); - } + facingDirAdd(ent->direction, &targetX, &targetY); + + // Check oob + if(targetX < 0 || targetX >= OVERWORLD.mapWidth) return; + if(targetY < 0 || targetY >= OVERWORLD.mapHeight) return; // Check tile at target uint8_t i = 0; diff --git a/src/dusk/overworld/entity.h b/src/dusk/overworld/entity/entity.h similarity index 88% rename from src/dusk/overworld/entity.h rename to src/dusk/overworld/entity/entity.h index f62bdee..7f5ad22 100644 --- a/src/dusk/overworld/entity.h +++ b/src/dusk/overworld/entity/entity.h @@ -6,28 +6,24 @@ */ #pragma once +#include "facing.h" #include "player.h" +#include "npc.h" typedef enum { ENTITY_TYPE_NULL, ENTITY_TYPE_PLAYER, + ENTITY_TYPE_NPC } entitytype_t; -#define ENTITY_TYPE_COUNT 2 +#define ENTITY_TYPE_COUNT 3 typedef struct { void (*init)(entity_t *); void (*update)(entity_t *); + void (*interact)(entity_t *); } entitycallback_t; -typedef enum { - FACING_DIRECTION_SOUTH, - FACING_DIRECTION_EAST, - FACING_DIRECTION_NORTH, - FACING_DIRECTION_WEST -} facingdir_t; -#define FACING_DIRECTION_COUNT 4 - typedef struct _entity_t { uint8_t x, y; int8_t subX, subY; diff --git a/src/dusk/overworld/entity/facing.c b/src/dusk/overworld/entity/facing.c new file mode 100644 index 0000000..27b4354 --- /dev/null +++ b/src/dusk/overworld/entity/facing.c @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "facing.h" +#include "assert/assert.h" + +void facingDirGetRelative(const facingdir_t dir, int8_t *x, int8_t *y) { + assertNotNull(x, "X cannot be NULL"); + assertNotNull(y, "Y cannot be NULL"); + + switch(dir) { + case FACING_DIRECTION_SOUTH: + *x = 0; + *y = -1; + break; + + case FACING_DIRECTION_EAST: + *x = 1; + *y = 0; + break; + + case FACING_DIRECTION_NORTH: + *x = 0; + *y = 1; + break; + + case FACING_DIRECTION_WEST: + *x = -1; + *y = 0; + break; + + default: + assertUnreachable("Invalid facing direction."); + } +} + +void facingDirAdd(const facingdir_t dir, uint8_t *x, uint8_t *y) { + assertNotNull(x, "X cannot be NULL"); + assertNotNull(y, "Y cannot be NULL"); + + int8_t dx, dy; + facingDirGetRelative(dir, &dx, &dy); + + *x += dx; + *y += dy; +} \ No newline at end of file diff --git a/src/dusk/overworld/entity/facing.h b/src/dusk/overworld/entity/facing.h new file mode 100644 index 0000000..07a1e5b --- /dev/null +++ b/src/dusk/overworld/entity/facing.h @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" + +typedef enum { + FACING_DIRECTION_SOUTH, + FACING_DIRECTION_EAST, + FACING_DIRECTION_NORTH, + FACING_DIRECTION_WEST +} facingdir_t; + +#define FACING_DIRECTION_COUNT 4 + +/** + * Converts a facing direction to a directional vector. + * + * @param dir The direction to convert. + * @param x The x component of the vector. + * @param y The y component of the vector. + */ +void facingDirGetRelative(const facingdir_t dir, int8_t *x, int8_t *y); + +/** + * Adds a facing direction to a position. + * + * @param dir The direction to add. + * @param x The x position to add to. + * @param y The y position to add to. + */ +void facingDirAdd(const facingdir_t dir, uint8_t *x, uint8_t *y); \ No newline at end of file diff --git a/src/dusk/overworld/entity/npc.c b/src/dusk/overworld/entity/npc.c new file mode 100644 index 0000000..2f30d29 --- /dev/null +++ b/src/dusk/overworld/entity/npc.c @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "npc.h" + +void npcInit(entity_t *ent) { +} + +void npcUpdate(entity_t *ent) { +} + +void npcInteract(entity_t *ent) { +} \ No newline at end of file diff --git a/src/dusk/overworld/entity/npc.h b/src/dusk/overworld/entity/npc.h new file mode 100644 index 0000000..b29d97f --- /dev/null +++ b/src/dusk/overworld/entity/npc.h @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" + +typedef struct _entity_t entity_t; + +typedef struct { + int32_t nothing; +} npc_t; + +/** + * Handles initialization of an NPC. + * + * @param ent The entity to initialize as an NPC. + */ +void npcInit(entity_t *ent); + +/** + * Handles updating an NPC. + * + * @param ent The entity to update as an NPC. + */ +void npcUpdate(entity_t *ent); + +/** + * Handles interaction with an NPC. + * + * @param ent The entity to interact with. + */ +void npcInteract(entity_t *ent); \ No newline at end of file diff --git a/src/dusk/overworld/player.c b/src/dusk/overworld/entity/player.c similarity index 90% rename from src/dusk/overworld/player.c rename to src/dusk/overworld/entity/player.c index 76931b5..1cbd52f 100644 --- a/src/dusk/overworld/player.c +++ b/src/dusk/overworld/entity/player.c @@ -5,7 +5,7 @@ * https://opensource.org/licenses/MIT */ -#include "entity.h" +#include "overworld/overworld.h" #include "assert/assert.h" #include "input.h" @@ -26,6 +26,7 @@ void playerUpdate(entity_t *ent) { entityMove(ent, FACING_DIRECTION_NORTH); } else if(inputIsDown(INPUT_DOWN)) { entityMove(ent, FACING_DIRECTION_SOUTH); + } else if(inputWasPressed(INPUT_ACCEPT)) { } } \ No newline at end of file diff --git a/src/dusk/overworld/player.h b/src/dusk/overworld/entity/player.h similarity index 100% rename from src/dusk/overworld/player.h rename to src/dusk/overworld/entity/player.h diff --git a/src/dusk/overworld/map/CMakeLists.txt b/src/dusk/overworld/map/CMakeLists.txt new file mode 100644 index 0000000..81d07e0 --- /dev/null +++ b/src/dusk/overworld/map/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + tile.c +) + +# Subdirs \ No newline at end of file diff --git a/src/dusk/overworld/map/map.c b/src/dusk/overworld/map/map.c new file mode 100644 index 0000000..b6338d7 --- /dev/null +++ b/src/dusk/overworld/map/map.c @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "map.h" +#include "assert/assert.h" +#include "util/memory.h" + +void mapInit(map_t *map) { + memoryZero(map, sizeof(map_t)); +} \ No newline at end of file diff --git a/src/dusk/overworld/map/map.h b/src/dusk/overworld/map/map.h new file mode 100644 index 0000000..c5a922d --- /dev/null +++ b/src/dusk/overworld/map/map.h @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "tile.h" +#include "overworld/overworlddefs.h" + +typedef struct { + tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX]; + tiledata_t tileData[OVERWORLD_TILE_COUNT_MAX]; + uint8_t width, height, layerCount; +} map_t; + +void mapInit(map_t *map); \ No newline at end of file diff --git a/src/dusk/overworld/tile.c b/src/dusk/overworld/map/tile.c similarity index 100% rename from src/dusk/overworld/tile.c rename to src/dusk/overworld/map/tile.c diff --git a/src/dusk/overworld/tile.h b/src/dusk/overworld/map/tile.h similarity index 100% rename from src/dusk/overworld/tile.h rename to src/dusk/overworld/map/tile.h diff --git a/src/dusk/overworld/overworld.c b/src/dusk/overworld/overworld.c index 0539288..e658979 100644 --- a/src/dusk/overworld/overworld.c +++ b/src/dusk/overworld/overworld.c @@ -29,6 +29,9 @@ void overworldInit() { ); entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER); + entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_NPC); + OVERWORLD.entities[1].x = 2; + OVERWORLD.entities[1].y = 2; } void overworldSceneInit() { diff --git a/src/dusk/overworld/overworld.h b/src/dusk/overworld/overworld.h index 6f19894..b1cb7fa 100644 --- a/src/dusk/overworld/overworld.h +++ b/src/dusk/overworld/overworld.h @@ -7,8 +7,8 @@ #pragma once #include "overworlddefs.h" -#include "entity.h" -#include "tile.h" +#include "overworld/entity/entity.h" +#include "overworld/map/tile.h" typedef struct { entity_t entities[OVERWORLD_ENTITY_COUNT_MAX]; diff --git a/src/duskgl/display/shader/data/entities.c b/src/duskgl/display/shader/data/entities.c index cc688b0..712853e 100644 --- a/src/duskgl/display/shader/data/entities.c +++ b/src/duskgl/display/shader/data/entities.c @@ -14,7 +14,7 @@ shaderbuffer_t ENTITIES_BUFFER; entitiesdata_t ENTITIES_DATA; void entitiesInit() { - memset(&ENTITIES_DATA, 0, sizeof(entitiesdata_t)); + memoryZero(&ENTITIES_DATA, sizeof(entitiesdata_t)); shaderBufferInit(&ENTITIES_BUFFER, sizeof(overworld_t)); } diff --git a/src/duskgl/overworld/overworld.c b/src/duskgl/overworld/overworld.c index 26559e4..59fc1da 100644 --- a/src/duskgl/overworld/overworld.c +++ b/src/duskgl/overworld/overworld.c @@ -12,7 +12,9 @@ void overworldRender() { mapShaderUse(); - quadRender(OVERWORLD.mapWidth*OVERWORLD.mapHeight*OVERWORLD.mapLayerCount); + quadRender( + OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount + ); entityShaderUse(); quadRender(OVERWORLD.entityCount);