aboutsummaryrefslogtreecommitdiff
path: root/src/bat.c
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-06-27 21:49:22 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-06-27 21:49:22 +0100
commit0c820ace5a4688b68849fb149811fb7331e53f78 (patch)
tree26ede93402f21de51f8e915d7317ec0882b5181f /src/bat.c
parent4df5f6b0ff382076e26a7463ede191107c259d5d (diff)
downloadgold-mine-run-0c820ace5a4688b68849fb149811fb7331e53f78.tar.gz
gold-mine-run-0c820ace5a4688b68849fb149811fb7331e53f78.zip
Add Bat enemy
Diffstat (limited to 'src/bat.c')
-rw-r--r--src/bat.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/bat.c b/src/bat.c
new file mode 100644
index 0000000..73ea4b2
--- /dev/null
+++ b/src/bat.c
@@ -0,0 +1,94 @@
+#include <stdint.h>
+
+#include <vga.h>
+#include "map.h"
+#include "entities.h"
+
+#include "player.h"
+
+#include "bat.h"
+
+#define DIR_UP 0
+#define DIR_DOWN 1
+
+/* limit of the animation cycle */
+#define MAX_FRAME 4
+
+static const Rect frames[2 * 4] =
+{
+ /* right */
+ { 64, 32, 144, 144 },
+ { 80, 32, 144, 144 },
+ { 96, 32, 144, 144 },
+ { 80, 32, 144, 144 },
+
+ /* left */
+ { 64, 32, 144, 144 },
+ { 80, 32, 144, 144 },
+ { 96, 32, 144, 144 },
+ { 80, 32, 144, 144 },
+};
+
+void bat_init(Entity *e)
+{
+ e->frames = (const Rect *)frames;
+ e->update = bat_update;
+ /* will control vertical movement */
+ e->flags = DIR_UP;
+}
+
+void bat_update(Entity *e)
+{
+ if (e->delay & 1)
+ {
+ /* horizontal */
+ if (e->dir == DIR_RIGHT)
+ {
+ if (map_is_blocked(e->x + 16, e->y + 7))
+ e->dir = DIR_LEFT;
+ else
+ e->x++;
+ }
+ else
+ {
+ /* dir is LEFT */
+ if (map_is_blocked(e->x - 1, e->y + 7))
+ e->dir = DIR_RIGHT;
+ else
+ e->x--;
+ }
+
+ /* vertical */
+ if (e->flags == DIR_DOWN)
+ {
+ if (map_is_blocked(e->x + 7, e->y + 16))
+ e->flags = DIR_UP;
+ else
+ e->y++;
+ }
+ else
+ {
+ /* dir is UP */
+ if (map_is_blocked(e->x + 7, e->y - 1))
+ e->flags = DIR_DOWN;
+ else
+ e->y--;
+ }
+ }
+
+ if (player_collision(e))
+ {
+ player_hit();
+ /* change direction */
+ e->dir ^= 1;
+ e->flags ^= 1;
+ }
+
+ if (e->delay++ == WALK_DELAY - 2)
+ {
+ e->delay = 0;
+ e->frame++;
+ if (e->frame == MAX_FRAME)
+ e->frame = 0;
+ }
+}