Add ent system base
This commit is contained in:
		| @@ -24,4 +24,4 @@ target_sources(${DUSK_TARGET_NAME} | |||||||
| # Subdirs | # Subdirs | ||||||
| add_subdirectory(assert) | add_subdirectory(assert) | ||||||
| add_subdirectory(render) | add_subdirectory(render) | ||||||
| add_subdirectory(entity) | add_subdirectory(overworld) | ||||||
| @@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME} | |||||||
|   PRIVATE |   PRIVATE | ||||||
|     entity.c |     entity.c | ||||||
|     overworld.c |     overworld.c | ||||||
|  |     player.c | ||||||
| ) | ) | ||||||
|  |  | ||||||
| # Subdirs | # Subdirs | ||||||
| @@ -8,10 +8,32 @@ | |||||||
| #include "entity.h" | #include "entity.h" | ||||||
| #include "assert/assert.h" | #include "assert/assert.h" | ||||||
|  |  | ||||||
|  | entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = { | ||||||
|  |   { NULL }, | ||||||
|  |   { playerInit, playerUpdate } | ||||||
|  | }; | ||||||
|  |  | ||||||
| entity_t ENTITY_TEST; | entity_t ENTITY_TEST; | ||||||
|  |  | ||||||
| void entityInit(entity_t *ent) { | void entityInit( | ||||||
|  |   entity_t *ent, | ||||||
|  |   const entitytype_t type | ||||||
|  | ) { | ||||||
|   assertNotNull(ent, "Entity cannot be NULL"); |   assertNotNull(ent, "Entity cannot be NULL"); | ||||||
|  |   assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type"); | ||||||
|  |  | ||||||
|  |   // Init values | ||||||
|   memset(ent, 0, sizeof(entity_t)); |   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); | ||||||
| } | } | ||||||
| @@ -6,12 +6,52 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #pragma once | #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 { | 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; | } 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); | ||||||
| @@ -7,8 +7,13 @@ | |||||||
|  |  | ||||||
| #include "overworld.h" | #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() { | void overworldSceneInit() { | ||||||
| @@ -20,6 +25,8 @@ void overworldSceneDeinit() { | |||||||
| } | } | ||||||
|  |  | ||||||
| void overworldUpdate() { | void overworldUpdate() { | ||||||
|    |   uint8_t i = 0; | ||||||
|  |   while(i < OVERWORLD.entityCount) { | ||||||
|  |     entityUpdate(OVERWORLD.entities + i++); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -6,7 +6,16 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #pragma once | #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 overworldInit(); | ||||||
| void overworldSceneInit(); | void overworldSceneInit(); | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								src/dusk/overworld/player.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/dusk/overworld/player.c
									
									
									
									
									
										Normal 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"); | ||||||
|  | } | ||||||
							
								
								
									
										29
									
								
								src/dusk/overworld/player.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/dusk/overworld/player.h
									
									
									
									
									
										Normal 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); | ||||||
		Reference in New Issue
	
	Block a user