From 2d2379251e71b5f315235db7980d0cfe03132561 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 29 Jun 2023 21:47:55 +0100 Subject: Add pickaxe pickup --- TODO.md | 3 --- data/stage.json | 15 +++++++++++++-- src/game.c | 37 +++++++++++++++++++++++++++++++++++-- src/game.h | 4 +++- src/map.c | 11 +++-------- src/pickup.c | 24 ++++++++++++++++++------ src/pickup.h | 1 + src/player.c | 12 ++++++++---- tools/map.py | 2 +- 9 files changed, 82 insertions(+), 27 deletions(-) diff --git a/TODO.md b/TODO.md index 441915b..922ac2a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,6 @@ # TODO - pick ups - - extra time - - bonuses - - pickaxe - key / doors - end of stage - enemies diff --git a/data/stage.json b/data/stage.json index bb04ee9..8abed82 100644 --- a/data/stage.json +++ b/data/stage.json @@ -95,7 +95,7 @@ { "height":16, "id":10, - "name":"Bonus", + "name":"Pickaxe", "rotation":0, "type":"", "visible":true, @@ -103,6 +103,17 @@ "x":56, "y":104 }, + { + "height":16, + "id":13, + "name":"Pickaxe", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":120, + "y":152 + }, { "height":16, "id":11, @@ -194,7 +205,7 @@ "y":0 }], "nextlayerid":4, - "nextobjectid":13, + "nextobjectid":14, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.7.2", diff --git a/src/game.c b/src/game.c index 70b1e74..64a7b23 100644 --- a/src/game.c +++ b/src/game.c @@ -16,12 +16,14 @@ #define GAME_LIVES_START 3 #define GAME_LIVES_MAX 9 #define GAME_TIME_MAX 60 +#define GAME_MAX_PICKAXE 3 #define HUD_CLEAN 0 #define HUD_LIVES 1 #define HUD_SCORE 2 #define HUD_STAGE 4 #define HUD_TIME 8 +#define HUD_PICKAXE 16 #define HUD_ALL 255 #define GAMEOVER_DELAY 96 @@ -35,8 +37,8 @@ static volatile uint8_t clock_updated; static uint8_t lives; static uint32_t score; static uint8_t stage; -static uint8_t gold; static uint8_t time; +static uint8_t pickaxe; static uint8_t gameover; static void hud_render() @@ -51,6 +53,11 @@ static void hud_render() /* lives */ blitrc(binary_sprites_start, &src, &dst); + /* pickaxe */ + src.x = 112; + dst.x = 90; + blitrc(binary_sprites_start, &src, &dst); + put_text(132, 4, "TIME", 1); put_text(249, 4, "STAGE", 1); } @@ -67,6 +74,12 @@ static void hud_render() put_text(34, 4, b, 5); } + if (hud & HUD_PICKAXE) + { + sprintf(b, "%d", pickaxe); + put_text(106, 4, b, 1); + } + if (hud & HUD_TIME) { sprintf(b, "%02d", time); @@ -105,7 +118,7 @@ void run_game() lives = GAME_LIVES_START; score = 0; stage = 0; - gold = 30; + pickaxe = 0; time = GAME_TIME_MAX; hud = HUD_ALL; @@ -191,3 +204,23 @@ void reset_time() hud |= HUD_TIME; timer_start(GAME_TIME_MAX, &clock_updated); } + +void add_pickaxe() +{ + if (pickaxe < GAME_MAX_PICKAXE) + { + pickaxe++; + hud |= HUD_PICKAXE; + } +} + +uint8_t use_pickaxe() +{ + if (pickaxe) + { + pickaxe--; + hud |= HUD_PICKAXE; + return 1; + } + return 0; +} diff --git a/src/game.h b/src/game.h index 5a13927..ea01495 100644 --- a/src/game.h +++ b/src/game.h @@ -2,10 +2,12 @@ #define _GAME_H void run_game(); + void add_score(uint8_t v); uint32_t get_hiscore(); - uint8_t dec_lives(); void reset_time(); +void add_pickaxe(); +uint8_t use_pickaxe(); #endif /* _GAME_H */ diff --git a/src/map.c b/src/map.c index cf25426..d34a5a9 100644 --- a/src/map.c +++ b/src/map.c @@ -14,12 +14,6 @@ #include "map.h" -typedef enum -{ - Player = 0, - Snake, -} EntityType; - /* current map; set via map_init */ static const uint8_t *cmap; static uint8_t gold[MAP_W * MAP_H]; @@ -31,6 +25,7 @@ static void (* const init[])(Entity *) = bat_init, pickup_time_init, pickup_bonus_init, + pickup_pickaxe_init, }; void map_init(const uint8_t map[]) @@ -58,8 +53,8 @@ void map_init(const uint8_t map[]) ent += 4 ) { - /* the player is not part of the entity system */ - if (*ent == Player) + /* the player (entity 0) is not part of the entity system */ + if (*ent == 0) { player_init(ent[1] * MAP_TILE_W, ent[2] * MAP_TILE_H, ent[3] & 1); continue; diff --git a/src/pickup.c b/src/pickup.c index 6b53cda..e1ad424 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -44,7 +44,7 @@ static const Rect frames[2 * 4] = { 80, 16, 144, 144 }, /* pickaxe */ - { 16, 32, 144, 144 }, + { 64, 16, 144, 144 }, /* gold key */ { 0, 0, 144, 144 }, @@ -122,6 +122,14 @@ void pickup_bonus_init(Entity *e) e->update = pickup_wait_update; } +void pickup_pickaxe_init(Entity *e) +{ + e->frames = (const Rect *)frames_in; + e->flags = PICKUP_PICKAXE; + e->counter = MAX_TTL / 2 + (rand() % MAX_TTL); + e->update = pickup_wait_update; +} + void pickup_wait_update(Entity *e) { if (e->counter-- == 0) @@ -141,17 +149,18 @@ void pickup_in_update(Entity *e) 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; + case PICKUP_PICKAXE: + e->frames = (const Rect *)frames; + e->frame = 1; break; } - + e->counter = 0; + e->update = pickup_update; } } } @@ -189,6 +198,9 @@ void pickup_update(Entity *e) case PICKUP_BONUS: add_score(250); break; + case PICKUP_PICKAXE: + add_pickaxe(); + break; } e->used = 0; } diff --git a/src/pickup.h b/src/pickup.h index 97777c2..ecb5350 100644 --- a/src/pickup.h +++ b/src/pickup.h @@ -3,6 +3,7 @@ void pickup_time_init(Entity *e); void pickup_bonus_init(Entity *e); +void pickup_pickaxe_init(Entity *e); void pickup_wait_update(Entity *e); void pickup_in_update(Entity *e); diff --git a/src/player.c b/src/player.c index ae50348..71c951a 100644 --- a/src/player.c +++ b/src/player.c @@ -309,8 +309,12 @@ uint8_t player_collision(Entity *e) void player_hit() { - /* TODO: pickaxe */ - dying = 1; - frame = FRAME_DYING; - gravity = GRAVITY_UP; + if (use_pickaxe()) + invuln = INVULN_TIME; + else + { + dying = 1; + frame = FRAME_DYING; + gravity = GRAVITY_UP; + } } diff --git a/tools/map.py b/tools/map.py index 1b134bb..694ed2b 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", "Time", "Bonus") +entity_types = ("Player", "Snake", "Bat", "Time", "Bonus", "Pickaxe") def get_layer(data, name): -- cgit v1.2.3