| Directory: | ./ |
|---|---|
| File: | sources/Component/component_velocity.c |
| Date: | 2023-09-29 04:53:15 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 15 | 25 | 60.0% |
| Branches: | 7 | 12 | 58.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Filename: sources/Component/velocity.c | ||
| 3 | * Path: sources/Component | ||
| 4 | * Created Date: Sunday, January 15th 2023, 3:59:05 pm | ||
| 5 | * Author: osvegn | ||
| 6 | * | ||
| 7 | * Copyright (c) 2023 our_rpg | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include "components.h" | ||
| 11 | #include "json.h" | ||
| 12 | #include "world_logger.h" | ||
| 13 | #include <stdlib.h> | ||
| 14 | #include <string.h> | ||
| 15 | |||
| 16 | /// @brief Check if a component is a velocity component. | ||
| 17 | /// @param component Component to check. It must be a valid component. | ||
| 18 | /// @return Return 1 if the component is a velocity component, 0 otherwise. | ||
| 19 | static int component_is_velocity(const component_t *component) | ||
| 20 | { | ||
| 21 | 4 | if (component->type == C_VELOCITY) | |
| 22 | return 1; | ||
| 23 | ✗ | log_error("Component is not velocity."); | |
| 24 | return 0; | ||
| 25 | } | ||
| 26 | |||
| 27 | 3 | int component_velocity_constructor(component_t *component, void *data) | |
| 28 | { | ||
| 29 | int rvalue = 0; | ||
| 30 | |||
| 31 | 3 | component->type = C_VELOCITY; | |
| 32 | 3 | component->data = malloc(sizeof(ecs_vector2i_t)); | |
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!component->data) { |
| 34 | ✗ | log_fatal("Could not allocate memory for velocity component."); | |
| 35 | ✗ | return -1; | |
| 36 | } | ||
| 37 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!data) { |
| 38 | data = &(ecs_vector2i_t){0, 0}; | ||
| 39 | } | ||
| 40 | 3 | rvalue = component_velocity_set(component, data); | |
| 41 | 3 | log_info("Velocity component created."); | |
| 42 | 3 | return rvalue; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | int component_velocity_constructor_from_json(component_t *component, void *data) | |
| 46 | { | ||
| 47 | ✗ | json_object *obj = json_tokener_parse(data); | |
| 48 | ✗ | ecs_vector2i_t v = {0}; | |
| 49 | |||
| 50 | ✗ | v.x = json_object_get_int(json_object_object_get(obj, "x")); | |
| 51 | ✗ | v.y = json_object_get_int(json_object_object_get(obj, "y")); | |
| 52 | ✗ | return component_velocity_constructor(component, &v); | |
| 53 | } | ||
| 54 | |||
| 55 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | int component_velocity_set(component_t *component, void *data) |
| 56 | { | ||
| 57 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!component_is_velocity(component) || !component->data || !data) |
| 58 | ✗ | return -1; | |
| 59 | memcpy(component->data, data, sizeof(ecs_vector2i_t)); | ||
| 60 | 4 | return 0; | |
| 61 | } | ||
| 62 | |||
| 63 | 2 | void *component_velocity_get(const component_t *component) | |
| 64 | { | ||
| 65 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (component->type != C_VELOCITY) |
| 66 | return 0; | ||
| 67 | 2 | return component->data; | |
| 68 | } | ||
| 69 |