From 81efbd3a866fe3f99768b03c341b7b9d11dda9de Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 4 Jul 2023 22:28:50 +0100 Subject: Add time monster --- src/game.c | 16 +++++++++++ src/player.c | 10 +++++++ src/player.h | 3 ++ src/tmonster.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tmonster.h | 8 ++++++ 5 files changed, 123 insertions(+) create mode 100644 src/tmonster.c create mode 100644 src/tmonster.h diff --git a/src/game.c b/src/game.c index a89ff66..dc51a21 100644 --- a/src/game.c +++ b/src/game.c @@ -10,6 +10,7 @@ #include "timer.h" #include "entities.h" +#include "tmonster.h" #include "player.h" #include "game.h" @@ -42,6 +43,7 @@ static uint8_t time; static uint8_t pickaxe; static uint8_t gameover; static uint8_t pause; +static Entity *tmonster; static void hud_render() { @@ -126,6 +128,7 @@ void run_game() stage = 0; pickaxe = 0; time = GAME_TIME_MAX; + tmonster = NULL; hud = HUD_ALL; @@ -189,6 +192,19 @@ void run_game() player_update(); entities_update(); + /* time monster */ + if (!time && !tmonster) + { + tmonster = entities_new(); + if (tmonster) + tmonster_init(tmonster); + } + if (time && tmonster) + { + tmonster->used = 0; + tmonster = NULL; + } + entities_draw(); player_draw(); diff --git a/src/player.c b/src/player.c index c07cc34..49c032c 100644 --- a/src/player.c +++ b/src/player.c @@ -335,3 +335,13 @@ void player_hit() sound_queue_efx(EFX_DEATH); } } + +uint16_t player_x() +{ + return x; +} + +uint16_t player_y() +{ + return y; +} diff --git a/src/player.h b/src/player.h index 4d32881..d0acc93 100644 --- a/src/player.h +++ b/src/player.h @@ -11,4 +11,7 @@ uint8_t player_collision(Entity *e); uint8_t player_collision_pickup(Entity *e); void player_hit(); +uint16_t player_x(); +uint16_t player_y(); + #endif /* _PLAYER_H */ diff --git a/src/tmonster.c b/src/tmonster.c new file mode 100644 index 0000000..30d5714 --- /dev/null +++ b/src/tmonster.c @@ -0,0 +1,86 @@ +#include +#include + +#include "vga.h" +#include "map.h" +#include "entities.h" +#include "game.h" + +#include "player.h" + +#include "tmonster.h" + +#define WAIT 120 +#define MOVE_TIME 90 + +static const Rect frames[2 * 4] = +{ + /* right */ + { 96, 16, 144, 144 }, + { 80, 48, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + + /* left */ + { 96, 16, 144, 144 }, + { 96, 48, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +void tmonster_init(Entity *e) +{ + e->y = 16; + e->x = 32 + (rand() % 256); + e->dir = e->x > 160 ? DIR_LEFT : DIR_RIGHT; + e->frames = (const Rect *)frames; + e->update = tmonster_wait_update; +} + +void tmonster_wait_update(Entity *e) +{ + e->delay++; + e->frame = (e->delay & 2) ? 1 : 0; + + if (e->delay == WAIT) + { + e->delay = 0; + e->frame = 1; + e->update = tmonster_update; + } +} + +void tmonster_update(Entity *e) +{ + e->delay++; + if (e->delay == WAIT * 2) + e->delay = 0; + + if (e->delay < MOVE_TIME) + { + if (e->x > player_x() && e->x > 0) + { + e->dir = DIR_LEFT; + e->x--; + } + if (e->x < player_x() && e->x < (MAP_W * MAP_TILE_W) - 16) + { + e->dir = DIR_RIGHT; + e->x++; + } + if (e->y > player_y() && e->y > 0) + e->y--; + if (e->y < player_y() && e->y < (MAP_H * MAP_TILE_H) - 16) + e->y++; + } + + if (player_collision(e)) + { + player_hit(); + reset_time(); + } +} diff --git a/src/tmonster.h b/src/tmonster.h new file mode 100644 index 0000000..5d73ad2 --- /dev/null +++ b/src/tmonster.h @@ -0,0 +1,8 @@ +#ifndef _TMONSTER_H +#define _TMONSTER_H + +void tmonster_init(Entity *e); +void tmonster_wait_update(Entity *e); +void tmonster_update(Entity *e); + +#endif /* _TMONSTER_H */ -- cgit v1.2.3