diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-07-06 22:06:28 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-07-06 22:06:28 +0100 |
commit | 752b8c4b4703a5bc43d32b815aa713fab12e653c (patch) | |
tree | e61431d523e7fe7217b08fff72ef5ba6c472294b /src/old.c | |
parent | 9adf0d53c1ba24fa5c5733423b2e34c4c8798f4b (diff) | |
download | gold-mine-run-752b8c4b4703a5bc43d32b815aa713fab12e653c.tar.gz gold-mine-run-752b8c4b4703a5bc43d32b815aa713fab12e653c.zip |
Add free enemy (Old Miner)
Diffstat (limited to 'src/old.c')
-rw-r--r-- | src/old.c | 158 |
1 files changed, 158 insertions, 0 deletions
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 <stdint.h> +#include <stdlib.h> + +#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; + } +} |