diff options
| author | Joel Stålnacke <joel@saker.fi> | 2024-10-11 13:31:44 +0300 |
|---|---|---|
| committer | Joel Stålnacke <joel@saker.fi> | 2024-10-13 13:34:46 +0300 |
| commit | 53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c (patch) | |
| tree | be8c2894226a2b7e1a47f7583f2041df75f795b3 /lib/world.c | |
| parent | 4bac6ae2e725a1997674fd3369bf4ea032235d8b (diff) | |
Add Godot client
Diffstat (limited to 'lib/world.c')
| -rw-r--r-- | lib/world.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/world.c b/lib/world.c new file mode 100644 index 0000000..954aa94 --- /dev/null +++ b/lib/world.c @@ -0,0 +1,104 @@ +#include <stdlib.h> +#include <string.h> + +#include "types.h" +#include "world.h" + +struct world { + struct entity entities[WORLD_MAX_ENTITIES]; +}; + +struct world * +dp_world_create() +{ + struct world *w; + + if (!(w = malloc(sizeof(*w)))) + return NULL; + memset(w, 0, sizeof(*w)); + + return w; +} + +void +dp_free_world(struct world *w) +{ + if (w) + free(w); +} + +int +dp_world_create_entity(struct world *w, int kind) +{ + int i; + struct entity *e; + + if (!w) + return -1; + if (kind == ENTITY_NONE) + return -1; + + for (i = 0; i < WORLD_MAX_ENTITIES; i++) { + e = &w->entities[i]; + if (e->kind != ENTITY_NONE) + continue; + e->kind = kind; + return i; + } + + return -1; +} + +struct entity * +dp_world_find_entity(struct world *w, int entity_id) +{ + struct entity *e; + + if (!w || entity_id < 0) + return NULL; + if (entity_id >= WORLD_MAX_ENTITIES) + return NULL; + + e = &w->entities[entity_id]; + + if (e->kind == ENTITY_NONE) + return NULL; + + return e; +} + +int +dp_world_remove_entity(struct world *w, int entity_id) +{ + struct entity *e; + + if (!(e = dp_world_find_entity(w, entity_id))) + return -1; + + memset(e, 0, sizeof(*e)); + e->kind = ENTITY_NONE; + return 0; +} + +int +dp_world_tick(struct world *w, double delta) +{ + int i; + struct entity *e; + struct entity_ball *ball; + + for (i = 0; i < WORLD_MAX_ENTITIES; i++) + { + e = &w->entities[i]; + switch (e->kind) { + case ENTITY_BALL: + ball = &e->e.ball; + /*ball->pos = dp_vec2_new(500.0, 500.0);*/ + ball->pos = dp_vec2_add(ball->pos, dp_vec2_mul(ball->vel, delta)); + break; + default: + break; + } + } + return 0; +} |
