Implement tag on entities
Internal Changelog: - Use data store similar to component map - Add init and free for entities tag - Use enum instead of char for tag - Switch to use clang for compilation - Add -Wall for compilationscene_man
parent
f90ef7eee4
commit
4a926f8c53
|
@ -1,5 +1,6 @@
|
||||||
set(PROJECT_NAME HATPC_remake)
|
set(PROJECT_NAME HATPC_remake)
|
||||||
|
set(CMAKE_C_COMPILER clang)
|
||||||
|
set(CMAKE_C_FLAGS "-Wall")
|
||||||
cmake_minimum_required(VERSION 3.22.1)
|
cmake_minimum_required(VERSION 3.22.1)
|
||||||
project(${PROJECT_NAME} C)
|
project(${PROJECT_NAME} C)
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
19
entManager.c
19
entManager.c
|
@ -7,6 +7,10 @@ void init_entity_manager(EntityManager_t *p_manager)
|
||||||
{
|
{
|
||||||
sc_map_init_64v(p_manager->component_map + i, MAX_COMP_POOL_SIZE, 0);
|
sc_map_init_64v(p_manager->component_map + i, MAX_COMP_POOL_SIZE, 0);
|
||||||
}
|
}
|
||||||
|
for (size_t i=0; i<N_TAGS; ++i)
|
||||||
|
{
|
||||||
|
sc_map_init_64v(p_manager->entities_map + i, MAX_COMP_POOL_SIZE, 0);
|
||||||
|
}
|
||||||
sc_queue_init(&p_manager->to_add);
|
sc_queue_init(&p_manager->to_add);
|
||||||
sc_queue_init(&p_manager->to_remove);
|
sc_queue_init(&p_manager->to_remove);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +32,7 @@ void update_entity_manager(EntityManager_t *p_manager)
|
||||||
{
|
{
|
||||||
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx);
|
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx);
|
||||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], comp_idx);
|
sc_map_del_64v(&p_manager->component_map[comp_type_idx], comp_idx);
|
||||||
|
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
|
||||||
}
|
}
|
||||||
free_entity_to_mempool(e_idx);
|
free_entity_to_mempool(e_idx);
|
||||||
sc_map_del_64v(&p_manager->entities, e_idx);
|
sc_map_del_64v(&p_manager->entities, e_idx);
|
||||||
|
@ -35,8 +40,9 @@ void update_entity_manager(EntityManager_t *p_manager)
|
||||||
sc_queue_clear(&p_manager->to_remove);
|
sc_queue_clear(&p_manager->to_remove);
|
||||||
sc_queue_foreach (&p_manager->to_add, e_idx)
|
sc_queue_foreach (&p_manager->to_add, e_idx)
|
||||||
{
|
{
|
||||||
Entity_t *ent = get_entity_wtih_id(e_idx);
|
Entity_t *p_entity = get_entity_wtih_id(e_idx);
|
||||||
sc_map_put_64v(&p_manager->entities, e_idx, (void *)ent);
|
sc_map_put_64v(&p_manager->entities, e_idx, (void *)p_entity);
|
||||||
|
sc_map_put_64v(&p_manager->entities_map[p_entity->m_tag], e_idx, (void *)p_entity);
|
||||||
}
|
}
|
||||||
sc_queue_clear(&p_manager->to_add);
|
sc_queue_clear(&p_manager->to_add);
|
||||||
|
|
||||||
|
@ -61,14 +67,19 @@ void free_entity_manager(EntityManager_t *p_manager)
|
||||||
{
|
{
|
||||||
sc_map_term_64v(p_manager->component_map + i);
|
sc_map_term_64v(p_manager->component_map + i);
|
||||||
}
|
}
|
||||||
|
for (size_t i=0; i<N_TAGS; ++i)
|
||||||
|
{
|
||||||
|
sc_map_term_64v(p_manager->entities_map + i);
|
||||||
|
}
|
||||||
sc_queue_term(&p_manager->to_add);
|
sc_queue_term(&p_manager->to_add);
|
||||||
sc_queue_term(&p_manager->to_remove);
|
sc_queue_term(&p_manager->to_remove);
|
||||||
}
|
}
|
||||||
|
|
||||||
Entity_t *add_entity(EntityManager_t *p_manager, const char *tag)
|
Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag)
|
||||||
{
|
{
|
||||||
unsigned long e_idx = 0;
|
unsigned long e_idx = 0;
|
||||||
Entity_t * p_ent = new_entity_from_mempool(&e_idx);
|
Entity_t * p_ent = new_entity_from_mempool(&e_idx);
|
||||||
|
p_ent->m_tag = tag;
|
||||||
if (p_ent)
|
if (p_ent)
|
||||||
{
|
{
|
||||||
sc_queue_add_last(&p_manager->to_add, e_idx);
|
sc_queue_add_last(&p_manager->to_add, e_idx);
|
||||||
|
@ -78,8 +89,6 @@ Entity_t *add_entity(EntityManager_t *p_manager, const char *tag)
|
||||||
|
|
||||||
void remove_entity(EntityManager_t *p_manager, unsigned long id)
|
void remove_entity(EntityManager_t *p_manager, unsigned long id)
|
||||||
{
|
{
|
||||||
unsigned long comp_type, comp_id;
|
|
||||||
|
|
||||||
Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id);
|
Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id);
|
||||||
if(!sc_map_found(&p_manager->entities)) return;
|
if(!sc_map_found(&p_manager->entities)) return;
|
||||||
// This only marks the entity for deletion
|
// This only marks the entity for deletion
|
||||||
|
|
|
@ -7,8 +7,9 @@
|
||||||
typedef struct EntityManager
|
typedef struct EntityManager
|
||||||
{
|
{
|
||||||
// All fields are Read-Only
|
// All fields are Read-Only
|
||||||
struct sc_map_64v entities;
|
struct sc_map_64v entities; // id : entity
|
||||||
struct sc_map_64v component_map[N_COMPONENTS];
|
struct sc_map_64v entities_map[N_TAGS]; // [{id: ent}]
|
||||||
|
struct sc_map_64v component_map[N_COMPONENTS]; // [{id: comp}, ...]
|
||||||
struct sc_queue_uint to_add;
|
struct sc_queue_uint to_add;
|
||||||
struct sc_queue_uint to_remove;
|
struct sc_queue_uint to_remove;
|
||||||
}EntityManager_t;
|
}EntityManager_t;
|
||||||
|
@ -18,7 +19,7 @@ void update_entity_manager(EntityManager_t *p_manager);
|
||||||
void clear_entity_manager(EntityManager_t *p_manager);
|
void clear_entity_manager(EntityManager_t *p_manager);
|
||||||
void free_entity_manager(EntityManager_t *p_manager);
|
void free_entity_manager(EntityManager_t *p_manager);
|
||||||
|
|
||||||
Entity_t *add_entity(EntityManager_t *p_manager, const char *tag);
|
Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag);
|
||||||
void remove_entity(EntityManager_t *p_manager, unsigned long id);
|
void remove_entity(EntityManager_t *p_manager, unsigned long id);
|
||||||
|
|
||||||
void *add_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type);
|
void *add_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type);
|
||||||
|
|
12
entity.h
12
entity.h
|
@ -2,11 +2,19 @@
|
||||||
#define __ENTITY_H
|
#define __ENTITY_H
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "sc/map/sc_map.h"
|
#include "sc/map/sc_map.h"
|
||||||
#define MAX_TAG_LEN 32
|
|
||||||
|
#define N_TAGS 3
|
||||||
|
enum EntityTag
|
||||||
|
{
|
||||||
|
NO_ENT_TAG,
|
||||||
|
PLAYER_ENT_TAG,
|
||||||
|
ENEMY_ENT_TAG,
|
||||||
|
};
|
||||||
|
typedef enum EntityTag EntityTag_t;
|
||||||
typedef struct Entity
|
typedef struct Entity
|
||||||
{
|
{
|
||||||
unsigned long m_id;
|
unsigned long m_id;
|
||||||
char m_tag[MAX_TAG_LEN];
|
EntityTag_t m_tag;
|
||||||
bool m_alive;
|
bool m_alive;
|
||||||
struct sc_map_64 components;
|
struct sc_map_64 components;
|
||||||
}Entity_t;
|
}Entity_t;
|
||||||
|
|
4
main.c
4
main.c
|
@ -11,10 +11,10 @@ int main(void)
|
||||||
init_entity_manager(&manager);
|
init_entity_manager(&manager);
|
||||||
|
|
||||||
puts("Creating two entities");
|
puts("Creating two entities");
|
||||||
Entity_t *p_ent = add_entity(&manager, "player");
|
Entity_t *p_ent = add_entity(&manager, PLAYER_ENT_TAG);
|
||||||
CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
p_bbox->x = 15;
|
p_bbox->x = 15;
|
||||||
p_ent = add_entity(&manager, "enemy");
|
p_ent = add_entity(&manager, ENEMY_ENT_TAG);
|
||||||
p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
p_bbox->x = 40;
|
p_bbox->x = 40;
|
||||||
update_entity_manager(&manager);
|
update_entity_manager(&manager);
|
||||||
|
|
|
@ -72,7 +72,7 @@ Entity_t* new_entity_from_mempool(unsigned long *p_e_idx)
|
||||||
Entity_t * ent = entity_mem_pool.entity_buffer + e_idx;
|
Entity_t * ent = entity_mem_pool.entity_buffer + e_idx;
|
||||||
sc_map_clear_64(&ent->components);
|
sc_map_clear_64(&ent->components);
|
||||||
ent->m_alive = true;
|
ent->m_alive = true;
|
||||||
memset(ent->m_tag, 0, MAX_TAG_LEN);
|
ent->m_tag = NO_ENT_TAG;
|
||||||
return ent;
|
return ent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue