#include "game.h" #include "cfg.h" #include "entity.h" #include "map.h" #include #include 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; this->queue_previous_scene = 0; map_next(); game_restart_scene(this); return; } } if (this->queue_previous_scene) { if (--this->queue_next_scene == 0) { this->queue_restart_scene = 0; this->queue_next_scene = 0; map_previous(); 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_begin != NULL) e->update_begin(e, 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); } for (__auto_type i = 0; i < MAX_ENTITIES; i++) { const __auto_type e = &this->entities[i]; if (e->type != 0 && e->update_end != NULL) e->update_end(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_begin != NULL) e->draw_begin(e, this); } 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); } for (int i = 0; i < MAX_ENTITIES; i++) { const __auto_type e = &this->entities[i]; if (e->type != 0 && e->draw_end != NULL) e->draw_end(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 = (object->type && object->type[0]) ? entity_type(object->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, object->name, x, y, object->width, object->height); } } 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; }