From 752b8c4b4703a5bc43d32b815aa713fab12e653c Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 6 Jul 2023 22:06:28 +0100 Subject: Add free enemy (Old Miner) --- src/map.c | 2 + src/old.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/old.h | 7 +++ src/player.c | 7 --- src/player.h | 9 ++++ 5 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 src/old.c create mode 100644 src/old.h (limited to 'src') diff --git a/src/map.c b/src/map.c index d34a5a9..bc7b283 100644 --- a/src/map.c +++ b/src/map.c @@ -10,6 +10,7 @@ #include "player.h" #include "snake.h" #include "bat.h" +#include "old.h" #include "pickup.h" #include "map.h" @@ -23,6 +24,7 @@ static void (* const init[])(Entity *) = { snake_init, bat_init, + old_init, pickup_time_init, pickup_bonus_init, pickup_pickaxe_init, diff --git a/src/old.c b/src/old.c new file mode 100644 index 0000000..3451118 --- /dev/null +++ b/src/old.c @@ -0,0 +1,158 @@ +#include +#include + +#include "vga.h" +#include "map.h" +#include "entities.h" + +#include "player.h" + +#include "old.h" + +#define FRAME_STANDING 0 +#define FRAME_JUMPING 1 +#define WALK_CYCLE_FRAMES 4 + +#define JUMP_DELAY 2 + +static const Rect frames[2 * 4] = +{ + /* right */ + { 0, 80, 144, 144 }, + { 16, 80, 144, 144 }, + { 0, 80, 144, 144 }, + { 32, 80, 144, 144 }, + + /* left */ + { 48, 80, 144, 144 }, + { 64, 80, 144, 144 }, + { 48, 80, 144, 144 }, + { 80, 80, 144, 144 }, +}; + +void old_init(Entity *e) +{ + e->frames = (const Rect *)frames; + e->update = old_update; +} + +static uint8_t is_bottom_deadly(uint16_t x, uint16_t y) +{ + while (1) + { + if (map_is_deadly(x, y)) + return 1; + + if (map_is_blocked(x, y)) + return 0; + + y += 8; + } +} + +void old_update(Entity *e) +{ + if (e->gravity == GRAVITY_OFF + && !map_is_blocked(e->x + 4, e->y + 16) + && !map_is_blocked(e->x + 11, e->y + 16)) + { + e->gravity = GRAVITY_DOWN; + e->frame = FRAME_JUMPING; + } + + if (e->gravity != GRAVITY_OFF) + { + uint8_t steps = gravity_seq[e->gravity - 1]; + + if (e->gravity > GRAVITY_DOWN) + { + /* going down! */ + for (uint8_t i = 0; i < steps; i++) + { + /* hit the floor */ + if ((map_is_blocked(e->x + 11, e->y + 16) + || map_is_blocked(e->x + 4, e->y + 16)) + && !map_is_blocked(e->x + 4, e->y + 15) + && !map_is_blocked(e->x + 11, e->y + 15)) + { + e->gravity = GRAVITY_OFF; + e->frame = FRAME_STANDING; + e->flags = rand() % (JUMP_DELAY * 2); + break; + } + e->y++; + } + } + else + { + /* going up! */ + if (e->y < steps) + e->y = 0; + else + e->y -= steps; + } + + if (e->gravity != GRAVITY_OFF && e->gravity != GRAVITY_SEQ_LEN) + e->gravity++; + } + else + { + if (e->delay & 1) + { + if (e->dir == DIR_RIGHT) + { + if (map_is_blocked(e->x + 16, e->y + 15) + || is_bottom_deadly(e->x + 16, e->y + 16)) + { + e->dir = DIR_LEFT; + e->flags++; + } + else + e->x++; + + if (e->flags > JUMP_DELAY + && map_is_blocked(e->x + 7, e->y - 1) + && !map_is_blocked(e->x + 7, e->y - 9)) + { + e->gravity = GRAVITY_UP; + e->frame = FRAME_JUMPING; + } + } + else + { + /* dir is LEFT */ + if (map_is_blocked(e->x - 1, e->y + 15) + || is_bottom_deadly(e->x - 1, e->y + 16)) + { + e->dir = DIR_RIGHT; + e->flags++; + } + else + e->x--; + + if (e->flags > JUMP_DELAY + && map_is_blocked(e->x + 7, e->y - 1) + && !map_is_blocked(e->x + 7, e->y - 9)) + { + e->gravity = GRAVITY_UP; + e->frame = FRAME_JUMPING; + } + } + } + + if (e->delay++ == WALK_DELAY) + { + e->delay = 0; + e->frame++; + if (e->frame == WALK_CYCLE_FRAMES) + e->frame = FRAME_STANDING; + } + } + + if (player_collision(e)) + { + player_hit(); + /* change direction */ + e->dir ^= 1; + } +} diff --git a/src/old.h b/src/old.h new file mode 100644 index 0000000..de70158 --- /dev/null +++ b/src/old.h @@ -0,0 +1,7 @@ +#ifndef _OLD_H +#define _OLD_H + +void old_init(Entity *e); +void old_update(Entity *e); + +#endif /* _OLD_H */ diff --git a/src/player.c b/src/player.c index 49c032c..7394857 100644 --- a/src/player.c +++ b/src/player.c @@ -16,13 +16,6 @@ #define WALK_CYCLE_FRAMES 4 -#define GRAVITY_OFF 0 -/* XXX: subtract 1 to get the value from gravity_seq */ -#define GRAVITY_DOWN 14 -#define GRAVITY_UP 1 - -#define GRAVITY_SEQ_LEN 24 - /* used for "coyote time" */ #define MAX_MOMENTUM 8 diff --git a/src/player.h b/src/player.h index d0acc93..95bc14a 100644 --- a/src/player.h +++ b/src/player.h @@ -14,4 +14,13 @@ void player_hit(); uint16_t player_x(); uint16_t player_y(); +#define GRAVITY_OFF 0 +/* XXX: substract 1 to get the value from gravity_seq */ +#define GRAVITY_DOWN 14 +#define GRAVITY_UP 1 + +#define GRAVITY_SEQ_LEN 24 + +extern const uint8_t gravity_seq[GRAVITY_SEQ_LEN]; + #endif /* _PLAYER_H */ -- cgit v1.2.3