diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-06-27 21:49:22 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-06-27 21:49:22 +0100 |
commit | 0c820ace5a4688b68849fb149811fb7331e53f78 (patch) | |
tree | 26ede93402f21de51f8e915d7317ec0882b5181f | |
parent | 4df5f6b0ff382076e26a7463ede191107c259d5d (diff) | |
download | gold-mine-run-0c820ace5a4688b68849fb149811fb7331e53f78.tar.gz gold-mine-run-0c820ace5a4688b68849fb149811fb7331e53f78.zip |
Add Bat enemy
-rw-r--r-- | TODO.md | 1 | ||||
-rw-r--r-- | data/sprites.png | bin | 21073 -> 21055 bytes | |||
-rw-r--r-- | data/stage.json | 18 | ||||
-rw-r--r-- | src/bat.c | 94 | ||||
-rw-r--r-- | src/bat.h | 7 | ||||
-rw-r--r-- | src/entities.h | 2 | ||||
-rw-r--r-- | src/map.c | 2 | ||||
-rwxr-xr-x | tools/map.py | 2 |
8 files changed, 114 insertions, 12 deletions
@@ -7,7 +7,6 @@ - key / doors - end of stage - enemies - - flying (bat) - free - tracker - time monster diff --git a/data/sprites.png b/data/sprites.png Binary files differindex c224d49..69116e6 100644 --- a/data/sprites.png +++ b/data/sprites.png diff --git a/data/stage.json b/data/stage.json index 9095400..f118222 100644 --- a/data/stage.json +++ b/data/stage.json @@ -45,13 +45,13 @@ { "height":16, "id":2, - "name":"Snake", + "name":"Bat", "rotation":0, "type":"", "visible":true, "width":16, - "x":216, - "y":128 + "x":240, + "y":32 }, { "height":16, @@ -73,13 +73,13 @@ { "height":16, "id":4, - "name":"Snake", + "name":"Bat", "rotation":0, "type":"", "visible":true, "width":16, - "x":184, - "y":152 + "x":160, + "y":96 }, { "height":16, @@ -112,7 +112,7 @@ { "height":16, "id":7, - "name":"Snake", + "name":"Bat", "properties":[ { "name":"dir", @@ -123,8 +123,8 @@ "type":"", "visible":true, "width":16, - "x":112, - "y":152 + "x":32, + "y":40 }, { "height":16, 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; + } +} diff --git a/src/bat.h b/src/bat.h new file mode 100644 index 0000000..cf21e8a --- /dev/null +++ b/src/bat.h @@ -0,0 +1,7 @@ +#ifndef _BAT_H +#define _BAT_H + +void bat_init(Entity *e); +void bat_update(Entity *e); + +#endif /* _BAT_H */ diff --git a/src/entities.h b/src/entities.h index e553390..016dcd2 100644 --- a/src/entities.h +++ b/src/entities.h @@ -15,7 +15,7 @@ typedef struct entity_s uint8_t frame; uint8_t delay; uint8_t gravity; - uint8_t flag; + uint8_t flags; uint8_t bg[16 * 16]; /* expected to be 2 directions per 4 frames max; 8 Rect */ const Rect *frames; @@ -9,6 +9,7 @@ #include "entities.h" #include "player.h" #include "snake.h" +#include "bat.h" #include "map.h" @@ -26,6 +27,7 @@ static uint8_t total_gold; static void (* const init[])(Entity *) = { snake_init, + bat_init, }; void map_init(const uint8_t map[]) diff --git a/tools/map.py b/tools/map.py index 8464c28..c2f05f4 100755 --- a/tools/map.py +++ b/tools/map.py @@ -10,7 +10,7 @@ __version__ = "1.0" ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld") strip = os.environ.get("STRIP", "i586-pc-msdosdjgpp-strip") -entity_types = ("Player", "Snake") +entity_types = ("Player", "Snake", "Bat") def get_layer(data, name): |