#include #include #include "types.h" #include "world.h" #include "log.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; }