From 7844eb5d515989358fa7213349d6a0663b12d4cf Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 24 Feb 2025 22:22:23 -0600 Subject: [PATCH] Add ent system base --- src/dusk/CMakeLists.txt | 2 +- src/dusk/overworld/CMakeLists.txt | 1 + src/dusk/overworld/entity.c | 24 +++++++++++++++- src/dusk/overworld/entity.h | 48 ++++++++++++++++++++++++++++--- src/dusk/overworld/overworld.c | 15 +++++++--- src/dusk/overworld/overworld.h | 11 ++++++- src/dusk/overworld/player.c | 19 ++++++++++++ src/dusk/overworld/player.h | 29 +++++++++++++++++++ 8 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 src/dusk/overworld/player.c create mode 100644 src/dusk/overworld/player.h diff --git a/src/dusk/CMakeLists.txt b/src/dusk/CMakeLists.txt index b07c043..d246405 100644 --- a/src/dusk/CMakeLists.txt +++ b/src/dusk/CMakeLists.txt @@ -24,4 +24,4 @@ target_sources(${DUSK_TARGET_NAME} # Subdirs add_subdirectory(assert) add_subdirectory(render) -add_subdirectory(entity) \ No newline at end of file +add_subdirectory(overworld) \ No newline at end of file diff --git a/src/dusk/overworld/CMakeLists.txt b/src/dusk/overworld/CMakeLists.txt index ee7265d..2a7b70f 100644 --- a/src/dusk/overworld/CMakeLists.txt +++ b/src/dusk/overworld/CMakeLists.txt @@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE entity.c overworld.c + player.c ) # Subdirs \ No newline at end of file diff --git a/src/dusk/overworld/entity.c b/src/dusk/overworld/entity.c index de25aba..d6fb26a 100644 --- a/src/dusk/overworld/entity.c +++ b/src/dusk/overworld/entity.c @@ -8,10 +8,32 @@ #include "entity.h" #include "assert/assert.h" +entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = { + { NULL }, + { playerInit, playerUpdate } +}; + entity_t ENTITY_TEST; -void entityInit(entity_t *ent) { +void entityInit( + entity_t *ent, + const entitytype_t type +) { assertNotNull(ent, "Entity cannot be NULL"); + assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type"); + // Init values memset(ent, 0, sizeof(entity_t)); + ent->type = type; + + assertNotNull(ENTITY_CALLBACKS[type].init, "Entity type init callback err."); + ENTITY_CALLBACKS[type].init(ent); +} + +void entityUpdate(entity_t *ent) { + assertNotNull(ent, "Entity cannot be NULL"); + assertTrue(ent->type < ENTITY_TYPE_COUNT, "Invalid entity type"); + + assertNotNull(ENTITY_CALLBACKS[ent->type].update, "Entity type update err."); + ENTITY_CALLBACKS[ent->type].update(ent); } \ No newline at end of file diff --git a/src/dusk/overworld/entity.h b/src/dusk/overworld/entity.h index 8e44f58..5c55a66 100644 --- a/src/dusk/overworld/entity.h +++ b/src/dusk/overworld/entity.h @@ -6,12 +6,52 @@ */ #pragma once -#include "dusk.h" +#include "player.h" + +typedef enum { + ENTITY_TYPE_NULL, + ENTITY_TYPE_PLAYER, +} entitytype_t; + +#define ENTITY_TYPE_COUNT 2 typedef struct { - vec3 position; + void (*init)(entity_t *); + void (*update)(entity_t *); +} entitycallback_t; + +typedef enum { + FACING_DIRECTION_SOUTH, + FACING_DIRECTION_EAST, + FACING_DIRECTION_NORTH, + FACING_DIRECTION_WEST +} facingdir_t; + +typedef struct _entity_t { + uint8_t x, y; + uint8_t subX, subY; + facingdir_t direction; + entitytype_t type; + + union { + player_t player; + }; } entity_t; -extern entity_t ENTITY_TEST; +/** + * Initializes an entity. + * + * @param entity The entity to initialize. + * @param type The type of entity to initialize. + */ +void entityInit( + entity_t *entity, + const entitytype_t type +); -void entityInit(entity_t *entity); \ No newline at end of file +/** + * Updates an entity. + * + * @param entity The entity to update. + */ +void entityUpdate(entity_t *entity); \ No newline at end of file diff --git a/src/dusk/overworld/overworld.c b/src/dusk/overworld/overworld.c index c33dd2c..b64a409 100644 --- a/src/dusk/overworld/overworld.c +++ b/src/dusk/overworld/overworld.c @@ -7,8 +7,13 @@ #include "overworld.h" -void overworldInit() { +overworld_t OVERWORLD; +void overworldInit() { + memset(&OVERWORLD, 0, sizeof(overworld_t)); + + // Test + entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER); } void overworldSceneInit() { @@ -20,6 +25,8 @@ void overworldSceneDeinit() { } void overworldUpdate() { - -} - + uint8_t i = 0; + while(i < OVERWORLD.entityCount) { + entityUpdate(OVERWORLD.entities + i++); + } +} \ No newline at end of file diff --git a/src/dusk/overworld/overworld.h b/src/dusk/overworld/overworld.h index cffe780..1b73533 100644 --- a/src/dusk/overworld/overworld.h +++ b/src/dusk/overworld/overworld.h @@ -6,7 +6,16 @@ */ #pragma once -#include "dusk.h" +#include "entity.h" + +#define OVERWORLD_ENTITY_COUNT_MAX 32 + +typedef struct { + entity_t entities[OVERWORLD_ENTITY_COUNT_MAX]; + uint8_t entityCount; +} overworld_t; + +extern overworld_t overworld; void overworldInit(); void overworldSceneInit(); diff --git a/src/dusk/overworld/player.c b/src/dusk/overworld/player.c new file mode 100644 index 0000000..6edae3f --- /dev/null +++ b/src/dusk/overworld/player.c @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "entity.h" +#include "assert/assert.h" + +void playerInit(entity_t *ent) { + assertNotNull(ent, "Entity cannot be NULL"); + + printf("Initializing player entity\n"); +} + +void playerUpdate(entity_t *ent) { + assertNotNull(ent, "Entity cannot be NULL"); +} \ No newline at end of file diff --git a/src/dusk/overworld/player.h b/src/dusk/overworld/player.h new file mode 100644 index 0000000..2082f57 --- /dev/null +++ b/src/dusk/overworld/player.h @@ -0,0 +1,29 @@ +/** + * 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; +} player_t; + +/** + * Handles initialization of the player. + * + * @param ent The entity to initialize as a player. + */ +void playerInit(entity_t *ent); + +/** + * Handles updating the player. + * + * @param ent The entity to update as a player. + */ +void playerUpdate(entity_t *ent); \ No newline at end of file