| Directory: | source/ |
|---|---|
| File: | source/world_entity.c |
| Date: | 2023-12-18 09:27:49 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 61 | 64 | 95.3% |
| Branches: | 21 | 24 | 87.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Filename: world_entity.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 "world.h" | ||
| 11 | #include "world_logger.h" | ||
| 12 | #include <stdarg.h> | ||
| 13 | |||
| 14 | 27 | static unsigned int find_entity_by_id(vector_t *entity_list, unsigned int id) | |
| 15 | { | ||
| 16 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 21 times.
|
35 | for (unsigned int i = 0; i < entity_list->size(entity_list); i++) { |
| 17 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8 times.
|
14 | if (id == (*(entity_t *)(entity_list->at(entity_list, i))).id) |
| 18 | 6 | return i; | |
| 19 | } | ||
| 20 | 21 | return entity_list->size(entity_list); | |
| 21 | } | ||
| 22 | |||
| 23 | /// @brief It returns the index of the entity in the entity list if it exists, or the size of the entity list | ||
| 24 | /// if it doesn't | ||
| 25 | /// @param entity_list The list of entities to search through. | ||
| 26 | /// @param entity The entity to find. | ||
| 27 | /// @return The index of the entity in the entity list. | ||
| 28 | 20 | static unsigned int find_entity(vector_t *entity_list, entity_t *entity) | |
| 29 | { | ||
| 30 | 20 | return find_entity_by_id(entity_list, entity->id); | |
| 31 | } | ||
| 32 | |||
| 33 | 20 | int world_add_entity(world_t *world, entity_t *entity) | |
| 34 | { | ||
| 35 | 20 | unsigned int index = find_entity(&world->entity_list, entity); | |
| 36 | |||
| 37 | 20 | log_info("Adding entity (id: %d) to world", entity->id); | |
| 38 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 19 times.
|
20 | if (index < world->entity_list.size(&world->entity_list)) { |
| 39 | 1 | return -1; | |
| 40 | } | ||
| 41 | 19 | world->entity_list.emplace_back(&world->entity_list, entity); | |
| 42 | 19 | return 0; | |
| 43 | } | ||
| 44 | |||
| 45 | 3 | int world_remove_entity_by_id(world_t *world, unsigned int id) | |
| 46 | { | ||
| 47 | 3 | unsigned int index = find_entity_by_id(&world->entity_list, id); | |
| 48 | |||
| 49 | 3 | log_info("Removing entity (id: %d) from world", id); | |
| 50 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (index < world->entity_list.size(&world->entity_list)) { |
| 51 | 2 | return (world->entity_list.erase(&world->entity_list, index)); | |
| 52 | } | ||
| 53 | 1 | return -1; | |
| 54 | } | ||
| 55 | |||
| 56 | 3 | int world_remove_entity(world_t *world, entity_t *entity) | |
| 57 | { | ||
| 58 | 3 | return world_remove_entity_by_id(world, entity->id); | |
| 59 | } | ||
| 60 | |||
| 61 | 2 | bool world_contains_entity_by_id(world_t *world, unsigned int id) | |
| 62 | { | ||
| 63 | 2 | unsigned int index = find_entity_by_id(&world->entity_list, id); | |
| 64 | 2 | unsigned int size = world->entity_list.size(&world->entity_list); | |
| 65 | |||
| 66 | 2 | return (index < size); | |
| 67 | } | ||
| 68 | |||
| 69 | 2 | bool world_contains_entity(world_t *world, entity_t *entity) | |
| 70 | { | ||
| 71 | 2 | return world_contains_entity_by_id(world, entity->id); | |
| 72 | } | ||
| 73 | |||
| 74 | 2 | entity_t *world_get_entity_by_id(world_t *world, unsigned int id) | |
| 75 | { | ||
| 76 | 2 | unsigned int index = find_entity_by_id(&world->entity_list, id); | |
| 77 | |||
| 78 | 2 | return world->entity_list.at(&world->entity_list, index); | |
| 79 | } | ||
| 80 | |||
| 81 | 1 | entity_t *world_get_entity(world_t *world, entity_t *entity) | |
| 82 | { | ||
| 83 | 1 | return world_get_entity_by_id(world, entity->id); | |
| 84 | } | ||
| 85 | |||
| 86 | 8 | int world_join_entities_from_vector(world_t *world, vector_t *entities, vector_t *args) | |
| 87 | { | ||
| 88 | 8 | int c_type = 0; | |
| 89 | 8 | int count = 0; | |
| 90 | 8 | entity_t *e = 0; | |
| 91 | |||
| 92 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (vector_constructor(entities, sizeof(vector_t *), 0) < 0) |
| 93 | ✗ | return -1; | |
| 94 | 8 | log_info("Joining entities"); | |
| 95 | 8 | log_info("Number of arguments: %d", args->size(args)); | |
| 96 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 8 times.
|
17 | for (unsigned int index = 0; index < world->entity_list.size(&world->entity_list); index++) { |
| 97 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 9 times.
|
21 | for (unsigned int i = 0; i < args->size(args); i++) { |
| 98 | 12 | e = world->entity_list.at(&world->entity_list, index); | |
| 99 |
2/2✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
|
12 | if (entity_contains_component_by_type(e, CAST(unsigned int, args->at(args, i)))) { |
| 100 | 10 | count++; | |
| 101 | } | ||
| 102 | } | ||
| 103 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
|
9 | if (count == args->size(args)) |
| 104 | 7 | entities->emplace_back(entities, &e); | |
| 105 | 9 | count = 0; | |
| 106 | } | ||
| 107 | 8 | return entities->size(entities); | |
| 108 | } | ||
| 109 | |||
| 110 | 8 | int world_join_entities(world_t *world, vector_t *entities, unsigned int count, ...) | |
| 111 | { | ||
| 112 | va_list arg_ptr; | ||
| 113 | 8 | int c_type = 0; | |
| 114 | 8 | int r_value = 0; | |
| 115 | vector_t args; | ||
| 116 | |||
| 117 | |||
| 118 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (vector_constructor(&args, sizeof(int), count) < 0) |
| 119 | ✗ | return -1; | |
| 120 | 8 | va_start(arg_ptr, count); | |
| 121 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
|
17 | for (unsigned int i = 0; i < count; i++) { |
| 122 | 9 | c_type = va_arg(arg_ptr, int); | |
| 123 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (args.emplace_back(&args, &c_type) < 0) |
| 124 | ✗ | return -1; | |
| 125 | } | ||
| 126 | 8 | va_end(arg_ptr); | |
| 127 | 8 | r_value = world_join_entities_from_vector(world, entities, &args); | |
| 128 | 8 | args.destructor(&args); | |
| 129 | 8 | return r_value; | |
| 130 | } | ||
| 131 |