From 399e039d799186aeacebee60e1c2009a46677c0d Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 27 Jun 2023 23:18:22 +0100 Subject: Add pikcups WIP: only time and bonuses for now. --- data/sprites.png | Bin 21055 -> 21201 bytes data/stage.json | 41 ++++++++++-- src/effect.c | 41 ++++++++++++ src/effect.h | 7 ++ src/entities.h | 1 + src/game.c | 7 ++ src/game.h | 1 + src/map.c | 3 + src/pickup.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pickup.h | 11 ++++ tools/map.py | 2 +- 11 files changed, 304 insertions(+), 5 deletions(-) create mode 100644 src/effect.c create mode 100644 src/effect.h create mode 100644 src/pickup.c create mode 100644 src/pickup.h diff --git a/data/sprites.png b/data/sprites.png index 69116e6..57f5fb5 100644 Binary files a/data/sprites.png and b/data/sprites.png differ diff --git a/data/stage.json b/data/stage.json index f118222..bb04ee9 100644 --- a/data/stage.json +++ b/data/stage.json @@ -84,13 +84,46 @@ { "height":16, "id":5, - "name":"Snake", + "name":"Time", "rotation":0, "type":"", "visible":true, "width":16, - "x":32, - "y":152 + "x":80, + "y":128 + }, + { + "height":16, + "id":10, + "name":"Bonus", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":56, + "y":104 + }, + { + "height":16, + "id":11, + "name":"Bonus", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":80, + "y":104 + }, + { + "height":16, + "id":12, + "name":"Bonus", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":80, + "y":80 }, { "height":16, @@ -161,7 +194,7 @@ "y":0 }], "nextlayerid":4, - "nextobjectid":10, + "nextobjectid":13, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.7.2", diff --git a/src/effect.c b/src/effect.c new file mode 100644 index 0000000..030f223 --- /dev/null +++ b/src/effect.c @@ -0,0 +1,41 @@ +#include + +#include "vga.h" +#include "entities.h" + +#include "effect.h" + +#define MAX_FRAME 3 + +static const Rect frames[2 * 4] = +{ + { 96, 16, 144, 144 }, + { 112, 16, 144, 144 }, + { 128, 16, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +void effect_out_init(Entity *e) +{ + e->frames = (const Rect *)frames; + e->frame = 1; + e->delay = 0; + e->update = effect_out_update; +} + +void effect_out_update(Entity *e) +{ + if (e->delay++ == WALK_DELAY - 2) + { + e->delay = 0; + e->frame++; + if (e->frame == MAX_FRAME) + e->used = 0; + } +} diff --git a/src/effect.h b/src/effect.h new file mode 100644 index 0000000..9c9d654 --- /dev/null +++ b/src/effect.h @@ -0,0 +1,7 @@ +#ifndef _EFFECT_H +#define _EFFECT_H + +void effect_out_init(Entity *e); +void effect_out_update(Entity *e); + +#endif /* _EFFECT_H */ diff --git a/src/entities.h b/src/entities.h index 016dcd2..d7a6a83 100644 --- a/src/entities.h +++ b/src/entities.h @@ -16,6 +16,7 @@ typedef struct entity_s uint8_t delay; uint8_t gravity; uint8_t flags; + uint16_t counter; uint8_t bg[16 * 16]; /* expected to be 2 directions per 4 frames max; 8 Rect */ const Rect *frames; diff --git a/src/game.c b/src/game.c index 001ce21..70b1e74 100644 --- a/src/game.c +++ b/src/game.c @@ -184,3 +184,10 @@ uint8_t dec_lives() hud |= HUD_LIVES; return lives; } + +void reset_time() +{ + time = GAME_TIME_MAX; + hud |= HUD_TIME; + timer_start(GAME_TIME_MAX, &clock_updated); +} diff --git a/src/game.h b/src/game.h index 3058018..5a13927 100644 --- a/src/game.h +++ b/src/game.h @@ -6,5 +6,6 @@ void add_score(uint8_t v); uint32_t get_hiscore(); uint8_t dec_lives(); +void reset_time(); #endif /* _GAME_H */ diff --git a/src/map.c b/src/map.c index f7c271b..cf25426 100644 --- a/src/map.c +++ b/src/map.c @@ -10,6 +10,7 @@ #include "player.h" #include "snake.h" #include "bat.h" +#include "pickup.h" #include "map.h" @@ -28,6 +29,8 @@ static void (* const init[])(Entity *) = { snake_init, bat_init, + pickup_time_init, + pickup_bonus_init, }; void map_init(const uint8_t map[]) diff --git a/src/pickup.c b/src/pickup.c new file mode 100644 index 0000000..6b53cda --- /dev/null +++ b/src/pickup.c @@ -0,0 +1,195 @@ +#include +#include + +#include "vga.h" +#include "entities.h" +#include "game.h" + +#include "player.h" +#include "effect.h" + +#include "pickup.h" + +#define MAX_FRAME 3 +#define MAX_BONUS_FRAME 4 + +#define MAX_TTL ((uint16_t)800) + +typedef enum +{ + PICKUP_TIME = 0, + PICKUP_BONUS, + PICKUP_PICKAXE, + PICKUP_GOLD_KEY, + PICKUP_SILVER_KEY, +} PickupType; + +static const Rect frames_in[2 * 4] = +{ + { 96, 16, 144, 144 }, + { 128, 16, 144, 144 }, + { 112, 16, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +static const Rect frames[2 * 4] = +{ + /* time */ + { 80, 16, 144, 144 }, + + /* pickaxe */ + { 16, 32, 144, 144 }, + + /* gold key */ + { 0, 0, 144, 144 }, + + /* siver key */ + { 0, 0, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +static const Rect frames_bonus0[2 * 4] = +{ + { 0, 48, 144, 144 }, + { 16, 48, 144, 144 }, + { 32, 48, 144, 144 }, + { 48, 48, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +static const Rect frames_bonus1[2 * 4] = +{ + { 0, 64, 144, 144 }, + { 16, 64, 144, 144 }, + { 32, 64, 144, 144 }, + { 48, 64, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +static const Rect frames_bonus2[2 * 4] = +{ + { 64, 64, 144, 144 }, + { 80, 64, 144, 144 }, + { 96, 64, 144, 144 }, + { 112, 64, 144, 144 }, + + /* not used */ + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 }, + { 0, 0, 144, 144 } +}; + +static const Rect *frames_bonuses[] = +{ + frames_bonus0, frames_bonus1, frames_bonus2 +}; + +void pickup_time_init(Entity *e) +{ + e->frames = (const Rect *)frames_in; + e->flags = PICKUP_TIME; + e->counter = MAX_TTL * 2 + (rand() % MAX_TTL); + e->update = pickup_wait_update; +} + +void pickup_bonus_init(Entity *e) +{ + e->frames = (const Rect *)frames_in; + e->flags = PICKUP_BONUS; + e->counter = MAX_TTL + (rand() % MAX_TTL); + e->update = pickup_wait_update; +} + +void pickup_wait_update(Entity *e) +{ + if (e->counter-- == 0) + e->update = pickup_in_update; +} + +void pickup_in_update(Entity *e) +{ + if (e->delay++ == WALK_DELAY - 2) + { + e->delay = 0; + e->frame++; + if (e->frame == MAX_FRAME) + { + switch (e->flags) + { + case PICKUP_TIME: + e->frames = (const Rect *)frames; + e->frame = 0; + e->counter = 0; + e->update = pickup_update; + break; + case PICKUP_BONUS: + e->frames = (const Rect *)frames_bonuses[rand() % 3]; + e->frame = 0; + e->counter = 0; + e->update = pickup_update; + break; + } + + } + } +} + +void pickup_update(Entity *e) +{ + if (e->counter++ == MAX_TTL) + { + effect_out_init(e); + return; + } + + /* the bonuses have an animation */ + if (e->flags == PICKUP_BONUS) + { + e->delay++; + /* first frame lasts longer */ + if ((e->frame == 0 && e->delay == WALK_DELAY * 4) + || (e->frame != 0 && e->delay == WALK_DELAY - 2)) + { + e->delay = 0; + e->frame++; + if (e->frame == MAX_BONUS_FRAME) + e->frame = 0; + } + } + + if (player_collision(e)) + { + switch (e->flags) + { + case PICKUP_TIME: + reset_time(); + break; + case PICKUP_BONUS: + add_score(250); + break; + } + e->used = 0; + } +} diff --git a/src/pickup.h b/src/pickup.h new file mode 100644 index 0000000..97777c2 --- /dev/null +++ b/src/pickup.h @@ -0,0 +1,11 @@ +#ifndef _PIKCUP_H +#define _PIKCUP_H + +void pickup_time_init(Entity *e); +void pickup_bonus_init(Entity *e); + +void pickup_wait_update(Entity *e); +void pickup_in_update(Entity *e); +void pickup_update(Entity *e); + +#endif /* _PIKCUP_H */ diff --git a/tools/map.py b/tools/map.py index c2f05f4..1b134bb 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", "Bat") +entity_types = ("Player", "Snake", "Bat", "Time", "Bonus") def get_layer(data, name): -- cgit v1.2.3