diff --git a/include/component.h b/include/component.h index f74531a..690fd89 100644 --- a/include/component.h +++ b/include/component.h @@ -30,6 +30,7 @@ struct gc_component void *new_component(const void *component, ...); void component_destroy(void *component); +gc_component *component_remove(gc_component *cmp, const char *name); #define GETCMP(x) ((struct x *)entity->get_component(entity, #x)) #define GETCOLCMP(x) ((struct x *)entity_get_component(entity_get(engine->scene, id), #x)) \ No newline at end of file diff --git a/include/entity.h b/include/entity.h index 7b1be57..27ed8c3 100644 --- a/include/entity.h +++ b/include/entity.h @@ -20,6 +20,8 @@ struct gc_entity gc_entity *(*add_component)(gc_entity *entity, void *component); void *(*get_component)(const gc_entity *entity, const char *name); bool (*has_component)(const gc_entity *entity, const char *name); + void (*remove_component)(const gc_scene *scene, const gc_entity *entity, \ +const char *name); char *(*serialize)(gc_entity *entity, int fd); void (*destroy)(gc_entity *entity); }; @@ -30,4 +32,6 @@ gc_entity *entity_get(gc_scene *scene, int id); int entity_add(gc_scene *scene, gc_entity *entity); gc_entity *entity_add_component(gc_entity *entity, void *component); void *entity_get_component(const gc_entity *entity, const char *name); +void entity_remove_component(const gc_scene *scene, const gc_entity *entity, \ +const char *name); char *entity_serialize(gc_entity *entity, int fd); \ No newline at end of file diff --git a/include/tupple.h b/include/tupple.h index 47c83e0..c300c6c 100644 --- a/include/tupple.h +++ b/include/tupple.h @@ -19,4 +19,5 @@ struct gc_tupple gc_tupple *next; }; -gc_tupple *tupple_add(gc_tupple *list, const char *name, gc_entity *entity); \ No newline at end of file +gc_tupple *tupple_add(gc_tupple *list, const char *name, gc_entity *entity); +void tup_remove(gc_tupple *tup, int id); \ No newline at end of file diff --git a/src/component.c b/src/component.c index 4cde2a3..1764083 100644 --- a/src/component.c +++ b/src/component.c @@ -45,4 +45,18 @@ void component_destroy(void *component) if (cmp->dtr) cmp->dtr(component); free(component); +} + +gc_component *component_remove(gc_component *cmp, const char *name) +{ + for (; cmp; cmp = cmp->next) { + if (my_strcmp(cmp->name, name)) + continue; + if (cmp->prev) + cmp->prev->next = cmp->next; + if (cmp->next) + cmp->next->prev = cmp->prev; + return (cmp); + } + return (NULL); } \ No newline at end of file diff --git a/src/entity/entity.c b/src/entity/entity.c index eadff69..18fac9a 100644 --- a/src/entity/entity.c +++ b/src/entity/entity.c @@ -70,6 +70,7 @@ const gc_entity entity_prefab = { add_component: &entity_add_component, get_component: &entity_get_component, has_component: &entity_has_component, + remove_component: &entity_remove_component, serialize: &entity_serialize, destroy: &destroy }; \ No newline at end of file diff --git a/src/entity/entity_factory.c b/src/entity/entity_factory.c index 7c73bbc..74ff7d2 100644 --- a/src/entity/entity_factory.c +++ b/src/entity/entity_factory.c @@ -7,6 +7,7 @@ #include "entity.h" #include "component.h" +#include "my.h" #include static unsigned int next_id = 0; @@ -66,4 +67,21 @@ gc_entity *entity_add_component(gc_entity *entity, void *component) components->next = component; } return (entity); +} + +void entity_remove_component(const gc_scene *scene, const gc_entity *entity, \ +const char *name) +{ + gc_component *cmp; + + if (!scene || !entity || !entity->components) + return; + cmp = component_remove(entity->components, name); + if (!cmp) + return; + for (gc_tupple *tup = scene->entities_by_cmp; tup; tup = tup->next) { + if (!my_strcmp(tup->name, name)) + tup_remove(tup, entity->id); + } + component_destroy(cmp); } \ No newline at end of file diff --git a/src/utility/tupple.c b/src/utility/tupple.c index 081b25f..ffdd560 100644 --- a/src/utility/tupple.c +++ b/src/utility/tupple.c @@ -41,4 +41,20 @@ gc_tupple *tupple_add(gc_tupple *list, const char *name, gc_entity *entity) list->entities = list_add(NULL, entity); list->next = NULL; return (listconst); +} + +void tup_remove(gc_tupple *tup, int id) +{ + gc_list *prev = NULL; + + for (gc_list *ent = tup->entities; ent; ent = ent->next) { + if (((gc_entity *)ent->data)->id == id) { + if (prev) + prev->next = ent->next; + else + tup->entities = ent->next; + return; + } + prev = ent; + } } \ No newline at end of file