feat: game world now destroys objects for which game_world_destroy has been called

This commit is contained in:
Sara 2023-11-27 17:40:11 +01:00
parent 965ae37c7e
commit 5e371a3754
2 changed files with 8 additions and 11 deletions

View file

@ -13,7 +13,7 @@ static inline
size_t _internal_find_index_for_entity(void* data, const List* list) { size_t _internal_find_index_for_entity(void* data, const List* list) {
for(size_t i = 0; i < _game_entities.len; ++i) { for(size_t i = 0; i < _game_entities.len; ++i) {
BehaviourEntity* entity = list_at_as(BehaviourEntity, &_game_entities, i); BehaviourEntity* entity = list_at_as(BehaviourEntity, &_game_entities, i);
if(entity->data == entity) { if(entity->data == data) {
return i; return i;
} }
} }
@ -23,10 +23,10 @@ size_t _internal_find_index_for_entity(void* data, const List* list) {
static inline static inline
void _internal_clear_removed() { void _internal_clear_removed() {
list_foreach(size_t*, index, &_remove_queue) { list_foreach(BehaviourEntity*, entity, &_remove_queue) {
BehaviourEntity* entity = list_at_as(BehaviourEntity, &_game_entities, *index); size_t index = _internal_find_index_for_entity(entity->data, &_game_entities);
entity->drop->drop(entity->data); entity->drop->drop(entity->data);
list_erase(&_game_entities, *index); list_erase(&_game_entities, index);
} }
list_empty(&_remove_queue); list_empty(&_remove_queue);
} }
@ -43,7 +43,7 @@ void _internal_process_new() {
void game_world_init() { void game_world_init() {
_game_entities = list_from_type(BehaviourEntity); _game_entities = list_from_type(BehaviourEntity);
_add_queue = list_from_type(BehaviourEntity); _add_queue = list_from_type(BehaviourEntity);
_remove_queue = list_from_type(size_t); _remove_queue = list_from_type(BehaviourEntity);
} }
void game_world_close() { void game_world_close() {
@ -59,11 +59,8 @@ void game_world_add_entity(BehaviourEntity entity) {
list_add(&_add_queue, &entity); list_add(&_add_queue, &entity);
} }
void game_world_remove_entity(void* entity) { void game_world_destroy_entity(BehaviourEntity entity) {
size_t index = _internal_find_index_for_entity(entity, &_game_entities); list_add(&_remove_queue, &entity);
if(index != _game_entities.len) {
list_add(&_remove_queue, &index);
}
} }
void game_world_update() { void game_world_update() {

View file

@ -7,7 +7,7 @@ extern void game_world_init();
extern void game_world_close(); extern void game_world_close();
extern void game_world_add_entity(BehaviourEntity entity); extern void game_world_add_entity(BehaviourEntity entity);
extern void game_world_remove_entity(void* entity); extern void game_world_destroy_entity(BehaviourEntity entity);
extern void game_world_update(); extern void game_world_update();
extern void game_world_draw(); extern void game_world_draw();