Add ent system base

This commit is contained in:
2025-02-24 22:22:23 -06:00
parent 2a0af3c256
commit 7844eb5d51
8 changed files with 138 additions and 11 deletions

View File

@ -24,4 +24,4 @@ target_sources(${DUSK_TARGET_NAME}
# Subdirs
add_subdirectory(assert)
add_subdirectory(render)
add_subdirectory(entity)
add_subdirectory(overworld)

View File

@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
entity.c
overworld.c
player.c
)
# Subdirs

View File

@ -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);
}

View File

@ -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);
/**
* Updates an entity.
*
* @param entity The entity to update.
*/
void entityUpdate(entity_t *entity);

View File

@ -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++);
}
}

View File

@ -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();

View File

@ -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");
}

View File

@ -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);