| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* Filename: /workspaces/our_rpg/lib/ECS/source/world_entity.c |
| 3 |
|
|
* Path: /workspaces/our_rpg/lib/ECS/source |
| 4 |
|
|
* Created Date: Sunday, January 15th 2023, 3:59:16 pm |
| 5 |
|
|
* Author: osvegn |
| 6 |
|
|
* |
| 7 |
|
|
* Copyright (c) 2023 our_rpg |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include "world.h" |
| 11 |
|
|
#include "component.h" |
| 12 |
|
|
#include <stdarg.h> |
| 13 |
|
|
|
| 14 |
|
|
/// @brief It returns the index of the entity in the entity list if it exists, or the size of the entity list |
| 15 |
|
|
/// if it doesn't |
| 16 |
|
|
/// @param entity_list The list of entities to search through. |
| 17 |
|
|
/// @param entity The entity to find. |
| 18 |
|
|
/// @return The index of the entity in the entity list. |
| 19 |
|
✗ |
static unsigned int find_entity(vector_t *entity_list, entity_t *entity) |
| 20 |
|
|
{ |
| 21 |
|
✗ |
for (unsigned int i = 0; i < entity_list->size(entity_list); i++) { |
| 22 |
|
✗ |
if (entity->id == (*(entity_t *)(entity_list->at(entity_list, i))).id) |
| 23 |
|
✗ |
return i; |
| 24 |
|
|
} |
| 25 |
|
✗ |
return entity_list->size(entity_list); |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
✗ |
static unsigned int find_entity_by_id(vector_t *entity_list, unsigned int id) |
| 29 |
|
|
{ |
| 30 |
|
✗ |
for (unsigned int i = 0; i < entity_list->size(entity_list); i++) { |
| 31 |
|
✗ |
if (id == (*(entity_t *)(entity_list->at(entity_list, i))).id) |
| 32 |
|
✗ |
return i; |
| 33 |
|
|
} |
| 34 |
|
✗ |
return entity_list->size(entity_list); |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
✗ |
int world_add_entity(world_t *world, entity_t *entity) |
| 38 |
|
|
{ |
| 39 |
|
✗ |
unsigned int index = find_entity(&world->entity_list, entity); |
| 40 |
|
|
|
| 41 |
|
✗ |
if (index < world->entity_list.size(&world->entity_list)) { |
| 42 |
|
|
return -1; |
| 43 |
|
|
} |
| 44 |
|
✗ |
world->entity_list.emplace_back(&world->entity_list, entity); |
| 45 |
|
✗ |
return 0; |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
✗ |
int world_remove_entity(world_t *world, entity_t *entity) |
| 49 |
|
|
{ |
| 50 |
|
✗ |
unsigned int index = find_entity(&world->entity_list, entity); |
| 51 |
|
|
|
| 52 |
|
✗ |
if (index < world->entity_list.size(&world->entity_list)) { |
| 53 |
|
✗ |
world->entity_list.erase(&world->entity_list, index); |
| 54 |
|
✗ |
return 0; |
| 55 |
|
|
} |
| 56 |
|
|
return -1; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
✗ |
int world_remove_entity_by_id(world_t *world, unsigned int id) |
| 60 |
|
|
{ |
| 61 |
|
✗ |
unsigned int index = find_entity_by_id(&world->entity_list, id); |
| 62 |
|
|
|
| 63 |
|
✗ |
if (index < world->entity_list.size(&world->entity_list)) { |
| 64 |
|
✗ |
world->entity_list.erase(&world->entity_list, index); |
| 65 |
|
✗ |
return 0; |
| 66 |
|
|
} |
| 67 |
|
|
return -1; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
✗ |
bool world_contains_entity(world_t *world, entity_t *entity) |
| 71 |
|
|
{ |
| 72 |
|
✗ |
unsigned int index = find_entity(&world->entity_list, entity); |
| 73 |
|
|
|
| 74 |
|
✗ |
if (index < world->entity_list.size(&world->entity_list)) |
| 75 |
|
✗ |
return true; |
| 76 |
|
|
return false; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
✗ |
bool world_contains_entity_by_id(world_t *world, unsigned int id) |
| 80 |
|
|
{ |
| 81 |
|
✗ |
unsigned int index = find_entity_by_id(&world->entity_list, id); |
| 82 |
|
|
|
| 83 |
|
✗ |
if (index < world->entity_list.size(&world->entity_list)) |
| 84 |
|
✗ |
return true; |
| 85 |
|
|
return false; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
✗ |
entity_t *world_get_entity_by_id(world_t *world, unsigned int id) |
| 89 |
|
|
{ |
| 90 |
|
|
unsigned int index = 0; |
| 91 |
|
|
|
| 92 |
|
✗ |
index = find_entity_by_id(&world->entity_list, id); |
| 93 |
|
✗ |
return world->entity_list.at(&world->entity_list, index); |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
✗ |
int world_join_entities(world_t *world, vector_t *entities, unsigned int type, ...) |
| 97 |
|
|
{ |
| 98 |
|
|
va_list argptr; |
| 99 |
|
|
int component; |
| 100 |
|
|
bool first_element = true; |
| 101 |
|
|
void *ptr; |
| 102 |
|
|
|
| 103 |
|
✗ |
vector_constructor(entities, sizeof(entity_t *), 0); |
| 104 |
|
✗ |
va_start(argptr, type); |
| 105 |
|
✗ |
for (unsigned index = 0; index < type; index++) { |
| 106 |
|
✗ |
component = va_arg(argptr, int); |
| 107 |
|
✗ |
if (first_element) { |
| 108 |
|
✗ |
for (unsigned int i = 0; i < world->entity_list.size(&world->entity_list); i++) { |
| 109 |
|
✗ |
if (entity_contains_component_by_type(world->entity_list.at(&world->entity_list, i), component)) { |
| 110 |
|
✗ |
ptr = world->entity_list.at(&world->entity_list, i); |
| 111 |
|
✗ |
entities->emplace_back(entities, &ptr); |
| 112 |
|
|
} |
| 113 |
|
|
} |
| 114 |
|
|
first_element = false; |
| 115 |
|
|
} else { |
| 116 |
|
✗ |
for (unsigned int i = 0; i < entities->size(entities); i++) { |
| 117 |
|
✗ |
if (!entity_contains_component_by_type(*(entity_t **)entities->at(entities, i), component)) { |
| 118 |
|
✗ |
entities->erase(entities, i); |
| 119 |
|
✗ |
i--; |
| 120 |
|
|
} |
| 121 |
|
|
} |
| 122 |
|
✗ |
entities->shrink_to_fit(entities); |
| 123 |
|
|
} |
| 124 |
|
|
} |
| 125 |
|
✗ |
va_end(argptr); |
| 126 |
|
✗ |
if (first_element) |
| 127 |
|
|
return -1; |
| 128 |
|
✗ |
return entities->size(entities); |
| 129 |
|
|
} |
| 130 |
|
|
|