GCC Code Coverage Report


Directory: source/
File: source/entity_component.c
Date: 2023-12-18 09:27:49
Exec Total Coverage
Lines: 49 49 100.0%
Branches: 14 14 100.0%

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