| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* Filename: source/entity_component.c |
| 3 |
|
|
* Path: 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 "entity.h" |
| 11 |
|
|
#include "world_logger.h" |
| 12 |
|
|
|
| 13 |
|
|
/// @brief It returns the index of the component in the component list if it |
| 14 |
|
|
/// exists, or the size of the component list |
| 15 |
|
|
/// @param entity The entity to search through. |
| 16 |
|
|
/// @param component The component to find. |
| 17 |
|
|
/// @return The index of the component in the component list, or the size of the |
| 18 |
|
|
/// component list if it doesn't exist. |
| 19 |
|
✗ |
static unsigned int find_component(entity_t *entity, component_t *component) |
| 20 |
|
|
{ |
| 21 |
|
✗ |
for (unsigned int i = 0; i < entity->components.size(&entity->components); i++) { |
| 22 |
|
✗ |
if (component->type == ((component_t *)entity->components.at(&entity->components, i))->type) { |
| 23 |
|
✗ |
return i; |
| 24 |
|
|
} |
| 25 |
|
|
} |
| 26 |
|
✗ |
return entity->components.size(&entity->components); |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
✗ |
bool entity_contains_component(entity_t *entity, component_t *component) |
| 30 |
|
|
{ |
| 31 |
|
✗ |
unsigned int index = find_component(entity, component); |
| 32 |
|
|
|
| 33 |
|
✗ |
if (index < entity->components.size(&entity->components)) { |
| 34 |
|
✗ |
return true; |
| 35 |
|
|
} |
| 36 |
|
|
return false; |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
✗ |
bool entity_contains_component_by_type(entity_t *entity, unsigned int type) |
| 40 |
|
|
{ |
| 41 |
|
✗ |
component_t component = {type, 0}; |
| 42 |
|
✗ |
unsigned int index = find_component(entity, &component); |
| 43 |
|
|
|
| 44 |
|
✗ |
if (index < entity->components.size(&entity->components)) { |
| 45 |
|
✗ |
return true; |
| 46 |
|
|
} |
| 47 |
|
|
return false; |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
|
✗ |
int entity_add_component(entity_t *entity, component_t *component) |
| 51 |
|
|
{ |
| 52 |
|
✗ |
unsigned int index = find_component(entity, component); |
| 53 |
|
|
|
| 54 |
|
✗ |
if (index < entity->components.size(&entity->components)) { |
| 55 |
|
✗ |
log_error("Entity already contains component of type %d", component->type); |
| 56 |
|
✗ |
return -1; |
| 57 |
|
|
} |
| 58 |
|
✗ |
entity->components.emplace_back(&entity->components, component); |
| 59 |
|
✗ |
return 0; |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
✗ |
int entity_remove_component(entity_t *entity, component_t *component) |
| 63 |
|
|
{ |
| 64 |
|
✗ |
unsigned int index = find_component(entity, component); |
| 65 |
|
|
|
| 66 |
|
✗ |
if (index < entity->components.size(&entity->components)) { |
| 67 |
|
✗ |
entity->components.erase(&entity->components, index); |
| 68 |
|
✗ |
return 0; |
| 69 |
|
|
} |
| 70 |
|
✗ |
log_error("Entity does not contain component of type %d", component->type); |
| 71 |
|
✗ |
return -1; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
✗ |
component_t *entity_get_component(entity_t *entity, unsigned int type) |
| 75 |
|
|
{ |
| 76 |
|
✗ |
if (entity->components.size(&entity->components) <= 0) { |
| 77 |
|
✗ |
log_error("Entity does not contain any components"); |
| 78 |
|
✗ |
return 0; |
| 79 |
|
|
} |
| 80 |
|
✗ |
for (unsigned int index = 0; index < entity->components.size(&entity->components); index++) { |
| 81 |
|
✗ |
if (((component_t *)entity->components.at(&entity->components, index))->type == type) { |
| 82 |
|
✗ |
return entity->components.at(&entity->components, index); |
| 83 |
|
|
} |
| 84 |
|
|
} |
| 85 |
|
✗ |
log_error("Entity does not contain component of type %d", type); |
| 86 |
|
✗ |
return 0; |
| 87 |
|
|
} |
| 88 |
|
|
|
| 89 |
|
✗ |
int entity_constructor(entity_t *entity) |
| 90 |
|
|
{ |
| 91 |
|
|
static int id = 0; |
| 92 |
|
|
|
| 93 |
|
✗ |
entity->id = id; |
| 94 |
|
✗ |
id++; |
| 95 |
|
✗ |
log_info("Entity created (type: %d)", entity->id); |
| 96 |
|
✗ |
return vector_constructor(&entity->components, sizeof(component_t), 0); |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
✗ |
void entity_destructor(entity_t *entity) |
| 100 |
|
|
{ |
| 101 |
|
✗ |
entity->components.destructor(&entity->components); |
| 102 |
|
✗ |
log_info("Entity destroyed (type: %d)", entity->id); |
| 103 |
|
|
} |
| 104 |
|
|
|