aboutsummaryrefslogtreecommitdiff
path: root/src/tmonster.c
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-07-04 22:28:50 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-07-04 22:28:50 +0100
commit81efbd3a866fe3f99768b03c341b7b9d11dda9de (patch)
tree4dca4944c40c8800c198ab29d292ed3409b4a12b /src/tmonster.c
parentb0100ff9fc763c9d3e5a72155cfa8331a1c6ac3c (diff)
downloadgold-mine-run-81efbd3a866fe3f99768b03c341b7b9d11dda9de.tar.gz
gold-mine-run-81efbd3a866fe3f99768b03c341b7b9d11dda9de.zip
Add time monster
Diffstat (limited to 'src/tmonster.c')
-rw-r--r--src/tmonster.c86
1 files changed, 86 insertions, 0 deletions
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 <stdint.h>
+#include <stdlib.h>
+
+#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();
+ }
+}