summaryrefslogtreecommitdiff
path: root/lib/world.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/world.c')
-rw-r--r--lib/world.c104
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;
+}