hyperultra/src/game.c

121 lines
2.4 KiB
C
Raw Normal View History

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-19 01:37:14 +01:00
#include "spike.h"
2023-03-17 10:51:17 +01:00
#include <string.h>
void
game_init(Game *this)
{
memset(this, 0, sizeof(*this));
2023-03-21 21:29:20 +01:00
this->player_dir = 1;
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-21 21:31:26 +01:00
extern double tick;
this->spike_angle = tick / 16;
2023-03-18 23:40:10 +01:00
if (this->queue_restart_scene > 0) {
if (--this->queue_restart_scene == 0)
game_restart_scene(this);
2023-03-18 19:15:05 +01:00
}
2023-03-17 21:07:07 +01:00
if (this->queue_next_scene) {
this->queue_next_scene = false;
2023-03-21 21:29:20 +01:00
this->player_dir = game_get_entity(this, ET_exit)->exit.dir;
2023-03-17 21:07:07 +01:00
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);
}
2023-03-21 22:19:31 +01:00
this->show_ui -= (this->show_ui > 0);
2023-03-17 10:51:17 +01:00
}
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-21 22:19:31 +01:00
if (this->show_ui)
map_draw_ui();
2023-03-17 10:51:17 +01:00
}
2023-03-17 20:44:59 +01:00
void
game_restart_scene(Game *this)
{
memset(this->entities, 0, sizeof(this->entities));
2023-03-21 22:19:31 +01:00
this->show_ui = 30;
2023-03-17 20:44:59 +01:00
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;
2023-03-21 21:17:54 +01:00
Entity *e;
2023-03-17 20:44:59 +01:00
switch (map_get(x, y)) {
case 2:
2023-03-21 21:29:20 +01:00
e = player_init(game_create_entity(this), dx, dy);
e->player.dirx = this->player_dir;
2023-03-17 20:44:59 +01:00
break;
2023-03-21 21:17:54 +01:00
case 3:
2023-03-17 20:51:15 +01:00
case 4:
2023-03-21 21:17:54 +01:00
e = exit_init(game_create_entity(this), dx, dy);
e->exit.dir = (map_get(x, y) == 3) ? -1 : 1;
2023-03-17 20:51:15 +01:00
break;
2023-03-19 01:37:14 +01:00
case 5:
spike_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;
}
2023-03-19 05:49:14 +01:00
int
game_entity_count(Game *this, unsigned int type)
{
int count = 0;
for (int i = 0; i < MAX_ENTITIES; i++)
count += (this->entities[i].type == type);
return count;
}
2023-03-21 21:29:20 +01:00
Entity *
game_get_entity(Game *this, unsigned int type)
{
for (int i = 0; i < MAX_ENTITIES; i++)
if (this->entities[i].type == type)
return &this->entities[i];
return NULL;
}