Combine Component, Entity + Manager headers

It has reached a point where it is much easier to combine these three
than to separate them.
scene_man
En Yi 2023-05-23 20:37:55 +08:00
parent e1a7774bfb
commit 4478a512a3
11 changed files with 55 additions and 64 deletions

View File

@ -1,5 +1,4 @@
#include "mempool.h"
#include "entManager.h"
#include <stdio.h>
int main(void)

1
main.c
View File

@ -1,5 +1,6 @@
#include "raylib.h"
#include "scene_impl.h"
#include "mempool.h"
#include <stdio.h>
#define N_SCENES 3

View File

@ -1,9 +1,16 @@
#ifndef __COMPONENTS_H
#define __COMPONENTS_H
#include "raylib.h"
#ifndef __ENTITY_H
#define __ENTITY_H
#include <stdint.h>
#include "entity.h"
// TODO: Look at sc to use macros to auto generate functions
#include <stdbool.h>
#include "raylib.h"
#include "sc/map/sc_map.h"
#include "sc/queue/sc_queue.h"
#define N_TAGS 4
#define N_COMPONENTS 10
#define MAX_COMP_POOL_SIZE 1024
typedef struct EntityManager EntityManager_t;
typedef struct Entity Entity_t;
typedef enum ComponentEnum {
CBBOX_COMP_T,
@ -134,4 +141,41 @@ static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
p_bbox->half_size.x = (unsigned int)(x / 2);
p_bbox->half_size.y = (unsigned int)(y / 2);
}
#endif // __COMPONENTS_H
typedef enum EntityTag {
NO_ENT_TAG,
PLAYER_ENT_TAG,
ENEMY_ENT_TAG,
CRATES_ENT_TAG,
} EntityTag_t;
struct Entity {
unsigned long m_id;
EntityTag_t m_tag;
bool m_alive;
unsigned long components[N_COMPONENTS];
};
struct EntityManager {
// All fields are Read-Only
struct sc_map_64v entities; // ent id : entity
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}]
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...]
struct sc_queue_uint to_add;
struct sc_queue_uint to_remove;
};
void init_entity_manager(EntityManager_t* p_manager);
void update_entity_manager(EntityManager_t* p_manager);
void clear_entity_manager(EntityManager_t* p_manager);
void free_entity_manager(EntityManager_t* p_manager);
Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
void remove_entity(EntityManager_t* p_manager, unsigned long id);
Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id);
void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type);
#endif // __ENTITY_H

View File

@ -1,4 +1,4 @@
#include "entManager.h"
#include "mempool.h"
void init_entity_manager(EntityManager_t* p_manager)
{

View File

@ -1,29 +0,0 @@
#ifndef __ENTITY_MANAGER_H
#define __ENTITY_MANAGER_H
#include "sc/queue/sc_queue.h"
#include "sc/map/sc_map.h"
#include "mempool.h" // includes entity and components
typedef struct EntityManager {
// All fields are Read-Only
struct sc_map_64v entities; // ent id : entity
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}]
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...]
struct sc_queue_uint to_add;
struct sc_queue_uint to_remove;
} EntityManager_t;
void init_entity_manager(EntityManager_t* p_manager);
void update_entity_manager(EntityManager_t* p_manager);
void clear_entity_manager(EntityManager_t* p_manager);
void free_entity_manager(EntityManager_t* p_manager);
Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
void remove_entity(EntityManager_t* p_manager, unsigned long id);
Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id);
void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type);
#endif // __ENTITY_MANAGER_H

View File

@ -1,21 +0,0 @@
#ifndef __ENTITY_H
#define __ENTITY_H
#include <stdbool.h>
#define N_TAGS 4
#define N_COMPONENTS 10
typedef enum EntityTag {
NO_ENT_TAG,
PLAYER_ENT_TAG,
ENEMY_ENT_TAG,
CRATES_ENT_TAG,
} EntityTag_t;
typedef struct Entity {
unsigned long m_id;
EntityTag_t m_tag;
bool m_alive;
unsigned long components[N_COMPONENTS];
} Entity_t;
#endif // __ENTITY_H

View File

@ -1,8 +1,6 @@
#ifndef __MEMPOOL_H
#define __MEMPOOL_H
#include "entity.h"
#include "components.h"
#define MAX_COMP_POOL_SIZE 1024
#include "EC.h"
void init_memory_pools(void);
void free_memory_pools(void);

View File

@ -1,7 +1,7 @@
#ifndef __ASSETS_H
#define __ASSETS_H
#include "sc/map/sc_map.h"
#include "components.h"
#include "EC.h"
#include "raylib.h"
typedef struct Assets

View File

@ -1,6 +1,5 @@
#ifndef __ENGINE_H
#define __ENGINE_H
#include "entManager.h"
#include "actions.h"
#include "sc/array/sc_array.h"
#include "assets.h"

View File

@ -1,6 +1,5 @@
#ifndef __ENT_IMPL_H
#define __ENT_IMPL_H
#include "entManager.h"
#include "assets.h"
bool init_player_creation(const char* info_file, Assets_t* assets);

View File

@ -1,5 +1,6 @@
#include "game_systems.h"
#include "AABB.h"
#include "EC.h"
#include "constants.h"
#include <stdio.h>