2023-03-17 10:51:17 +01:00
|
|
|
#include "game.h"
|
2023-03-17 20:51:15 +01:00
|
|
|
#include "exit.h"
|
2023-03-17 13:45:20 +01:00
|
|
|
#include "map.h"
|
2023-03-17 20:44:59 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "cfg.h"
|
2023-03-17 10:51:17 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
game_init(Game *this)
|
|
|
|
{
|
|
|
|
memset(this, 0, sizeof(*this));
|
2023-03-17 20:44:59 +01:00
|
|
|
game_restart_scene(this);
|
2023-03-17 10:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
game_deinit(Game *this)
|
|
|
|
{
|
|
|
|
(void)this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
game_update(Game *this)
|
|
|
|
{
|
2023-03-18 19:15:05 +01:00
|
|
|
if (this->queue_restart_scene) {
|
|
|
|
this->queue_restart_scene = false;
|
|
|
|
game_restart_scene(this);
|
|
|
|
}
|
2023-03-17 21:07:07 +01:00
|
|
|
if (this->queue_next_scene) {
|
|
|
|
this->queue_next_scene = false;
|
|
|
|
map_next();
|
|
|
|
game_restart_scene(this);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-17 10:51:17 +01:00
|
|
|
for (int i = 0; i < MAX_ENTITIES; i++) {
|
|
|
|
Entity *const e = &this->entities[i];
|
|
|
|
if (e->type != ET_NONE && e->update != NULL)
|
|
|
|
e->update(e, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
game_draw(Game *this)
|
|
|
|
{
|
2023-03-17 13:45:20 +01:00
|
|
|
map_draw();
|
2023-03-17 10:51:17 +01:00
|
|
|
for (int i = 0; i < MAX_ENTITIES; i++) {
|
|
|
|
Entity *const e = &this->entities[i];
|
|
|
|
if (e->type != ET_NONE && e->draw != NULL)
|
|
|
|
e->draw(e, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:44:59 +01:00
|
|
|
void
|
|
|
|
game_restart_scene(Game *this)
|
|
|
|
{
|
|
|
|
memset(this->entities, 0, sizeof(this->entities));
|
|
|
|
for (int y = 0; y < map_height(); y++)
|
|
|
|
for (int x = 0; x < map_width(); x++) {
|
|
|
|
const int dx = x * TSIZE + TSIZE / 2;
|
|
|
|
const int dy = y * TSIZE + TSIZE / 2;
|
|
|
|
switch (map_get(x, y)) {
|
|
|
|
case 2:
|
|
|
|
player_init(game_create_entity(this), dx, dy);
|
|
|
|
break;
|
2023-03-17 20:51:15 +01:00
|
|
|
case 4:
|
|
|
|
exit_init(game_create_entity(this), dx, dy);
|
|
|
|
break;
|
2023-03-17 20:44:59 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:51:17 +01:00
|
|
|
Entity *
|
|
|
|
game_create_entity(Game *this)
|
|
|
|
{
|
|
|
|
Entity *e = &this->entities[MAX_ENTITIES - 1];
|
|
|
|
for (int i = 0; i < MAX_ENTITIES; i++)
|
|
|
|
if (this->entities[i].type == ET_NONE) {
|
|
|
|
e = &this->entities[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e->type = ET_NONE;
|
|
|
|
e->uuid = this->uuid;
|
|
|
|
this->uuid += 1;
|
|
|
|
return e;
|
|
|
|
}
|