diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-06-25 22:44:23 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-06-25 22:44:23 +0100 |
commit | 6bd6757583510ba3edf75451309e4b8ec8c9b0f1 (patch) | |
tree | e63f401238e4118ff6d1670ea86508f0181c19ea /src/snake.c | |
parent | 2d7fbc07acf0c5766d662d2629e72600b65f744b (diff) | |
download | gold-mine-run-6bd6757583510ba3edf75451309e4b8ec8c9b0f1.tar.gz gold-mine-run-6bd6757583510ba3edf75451309e4b8ec8c9b0f1.zip |
Add entity system, add new enemy (snake)
Diffstat (limited to 'src/snake.c')
-rw-r--r-- | src/snake.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/snake.c b/src/snake.c new file mode 100644 index 0000000..e81a1c8 --- /dev/null +++ b/src/snake.c @@ -0,0 +1,69 @@ +#include <stdint.h> + +#include <vga.h> +#include "map.h" +#include "entities.h" + +#include "player.h" + +#include "snake.h" + +static const Rect frames[2 * 4] = +{ + /* right */ + { 0, 32, 144, 144 }, + { 16, 32, 144, 144 }, + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + + /* left */ + { 32, 32, 144, 144 }, + { 48, 32, 144, 144 }, + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +void snake_init(Entity *e) +{ + e->frames = (const Rect *)frames; + e->update = snake_update; +} + +void snake_update(Entity *e) +{ + if (e->delay & 1) + { + if (e->dir == DIR_RIGHT) + { + if (map_is_blocked(e->x + 16, e->y + 15) + || !map_is_blocked(e->x + 16, e->y + 16)) + e->dir = DIR_LEFT; + else + e->x++; + } + else + { + /* dir is LEFT */ + if (map_is_blocked(e->x - 1, e->y + 15) + || !map_is_blocked(e->x - 1, e->y + 16)) + e->dir = DIR_RIGHT; + else + e->x--; + } + } + + if (player_collision(e)) + { + player_hit(); + /* change direction */ + e->dir ^= 1; + } + + if (e->delay++ == WALK_DELAY + 4) + { + e->delay = 0; + e->frame ^= 1; + } +} |