summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..4cd9728
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,94 @@
+#include "game.h"
+#include "cfg.h"
+#include "entity.h"
+#include "map.h"
+#include <stdio.h>
+#include <string.h>
+
+void
+game_init(Game *this)
+{
+ memset(this, 0, sizeof(*this));
+ game_restart_scene(this);
+}
+
+void
+game_update(Game *this)
+{
+ if (this->queue_next_scene) {
+ if (--this->queue_next_scene == 0) {
+ this->queue_restart_scene = 0;
+ map_next();
+ game_restart_scene(this);
+ return;
+ }
+ }
+ if (this->queue_restart_scene) {
+ if (--this->queue_restart_scene == 0)
+ game_restart_scene(this);
+ }
+ for (__auto_type i = 0; i < MAX_ENTITIES; i++) {
+ const __auto_type e = &this->entities[i];
+ if (e->type != 0 && e->update != NULL)
+ e->update(e, this);
+ }
+}
+
+void
+game_draw(Game *this)
+{
+ map_draw();
+ for (int i = 0; i < MAX_ENTITIES; i++) {
+ const __auto_type e = &this->entities[i];
+ if (e->type != 0 && e->draw != NULL)
+ e->draw(e, this);
+ }
+}
+
+void
+game_restart_scene(Game *this)
+{
+ memset(this->entities, 0, sizeof(this->entities));
+ unsigned int size;
+ const __auto_type objects = map_objects(&size);
+ for (__auto_type i = 0u; i < size; i++) {
+ const __auto_type object = &objects[i];
+ const __auto_type type = entity_type(object->name);
+ if (type == 0)
+ continue;
+ const __auto_type x = object->x + object->width / 2;
+ const __auto_type y = object->y + object->height / 2;
+ entity_init(game_create_entity(this), type, x, y);
+ }
+}
+
+Entity *
+game_create_entity(Game *this)
+{
+ __auto_type e = &this->entities[MAX_ENTITIES - 1];
+ for (__auto_type i = 0; i < MAX_ENTITIES; i++)
+ if (this->entities[i].type == 0) {
+ e = &this->entities[i];
+ break;
+ }
+ e->uuid = this->uuid++;
+ return e;
+}
+
+int
+game_entity_count(Game *this, unsigned int type)
+{
+ int count = 0;
+ for (__auto_type i = 0; i < MAX_ENTITIES; i++)
+ count += (this->entities[i].type == type);
+ return count;
+}
+
+Entity *
+game_get_entity(Game *this, unsigned int type)
+{
+ for (__auto_type i = 0; i < MAX_ENTITIES; i++)
+ if (this->entities[i].type == type)
+ return &this->entities[i];
+ return NULL;
+}