From 53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c Mon Sep 17 00:00:00 2001 From: Joel Stålnacke Date: Fri, 11 Oct 2024 13:31:44 +0300 Subject: Add Godot client --- lib/world.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lib/world.c (limited to 'lib/world.c') 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 +#include + +#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; +} -- cgit v1.2.3