Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Filename: system_movement.c |
3 |
|
|
* Path: sources/System |
4 |
|
|
* Created Date: Sunday, September 17th 2023, 7:25:32 am |
5 |
|
|
* Author: osvegn |
6 |
|
|
* |
7 |
|
|
* Copyright (c) 2023 our_rpg |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "systems.h" |
11 |
|
|
#include "vector.h" |
12 |
|
|
#include "world_entity.h" |
13 |
|
|
#include "world_resource.h" |
14 |
|
|
#include "components.h" |
15 |
|
|
#include "resources.h" |
16 |
|
|
#ifdef __linux__ |
17 |
|
|
#include <sys/time.h> |
18 |
|
|
#endif |
19 |
|
|
|
20 |
|
✗ |
int system_movement_constructor(system_t *system) |
21 |
|
|
{ |
22 |
|
✗ |
system->type = S_MOVEMENT; |
23 |
|
✗ |
system->run = &system_movement; |
24 |
|
✗ |
system->active = true; |
25 |
|
✗ |
return 0; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
✗ |
static int system_movement(void *world) |
29 |
|
|
{ |
30 |
|
✗ |
vector_t entities = {0}; |
31 |
|
✗ |
int rvalue = world_join_entities(world, &entities, 2, C_POSITION, C_VELOCITY); |
32 |
|
✗ |
ecs_vector2f_t position = {0}; |
33 |
|
|
entity_t *e = 0; |
34 |
|
|
component_t *c_position = 0; |
35 |
|
|
component_t *c_velocity = 0; |
36 |
|
✗ |
resource_t *r = world_get_resource_by_type(world, R_GAME_CLOCK); |
37 |
|
|
#ifdef __linux__ |
38 |
|
|
struct timeval now, start; |
39 |
|
✗ |
start = *(struct timeval *)resource_game_clock_get(r); |
40 |
|
✗ |
gettimeofday(&now, NULL); |
41 |
|
✗ |
double time = ((now.tv_sec - start.tv_sec) * 1000.0f + (now.tv_usec - start.tv_usec) / 1000.0f); |
42 |
|
|
#else |
43 |
|
|
double time = 1.0f; |
44 |
|
|
#endif |
45 |
|
|
|
46 |
|
✗ |
if (rvalue <= 0) |
47 |
|
|
return 0; |
48 |
|
✗ |
for (unsigned int i = 0; i < entities.size(&entities); i++) { |
49 |
|
✗ |
e = *(entity_t **)entities.at(&entities, i); |
50 |
|
✗ |
c_position = entity_get_component(e, C_POSITION); |
51 |
|
✗ |
c_velocity = entity_get_component(e, C_VELOCITY); |
52 |
|
✗ |
position.x = (ecs_vector2f_t *){c_position->data}->x + (ecs_vector2i_t *){c_velocity->data}->x * time; |
53 |
|
✗ |
position.y = (ecs_vector2f_t *){c_position->data}->y + (ecs_vector2i_t *){c_velocity->data}->y * time; |
54 |
|
✗ |
component_position_set(c_position, &position); |
55 |
|
|
} |
56 |
|
|
return 0; |
57 |
|
|
} |
58 |
|
|
|