From d489b6ef92ae9f61bbceb5f594026a51e71538f2 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 20 Jun 2023 22:31:57 +0100 Subject: Add gold support - added gold layer to the map - the player can collect gold - updates the score - TODO: end of stage --- src/map.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/map.c') diff --git a/src/map.c b/src/map.c index 87a0b3b..9908384 100644 --- a/src/map.c +++ b/src/map.c @@ -1,4 +1,5 @@ #include +#include #include "vga.h" #include "data.h" @@ -7,10 +8,22 @@ /* current map; set via map_init */ static const uint8_t *cmap; +static uint8_t gold[MAP_W * MAP_H]; +static uint8_t total_gold; void map_init(const uint8_t map[]) { cmap = map; + + /* make a copy of the gold data in RAM */ + memcpy(gold, cmap + MAP_W * MAP_H, MAP_W * MAP_H); + + /* count how many pieces of gold on this map */ + total_gold = 0; + for (uint16_t i = 0; i < MAP_W * MAP_H; i++) + /* gold is not 0xff */ + if (gold[i] != 0xff) + total_gold++; } void map_render() @@ -28,6 +41,24 @@ void map_render() src.x = (t % MAP_TILESET_COLS) * MAP_TILE_W; src.y = (t / MAP_TILESET_COLS) * MAP_TILE_H; + blitrc(binary_tiles_start, &src, &dst); + } + + for (uint8_t y = 0; y < MAP_H; y++) + for (uint8_t x = 0; x < MAP_W; x++) + { + dst.x = x * MAP_TILE_W; + dst.y = y * MAP_TILE_H + MAP_OFFS_Y; + + uint8_t t = gold[x + y * MAP_W]; + + /* not gold, skip! */ + if (t == 0xff) + continue; + + src.x = (t % MAP_TILESET_COLS) * MAP_TILE_W; + src.y = (t / MAP_TILESET_COLS) * MAP_TILE_H; + blitrc(binary_tiles_start, &src, &dst); } } @@ -41,3 +72,38 @@ uint8_t map_is_deadly(uint16_t x, uint16_t y) { return cmap[(x / MAP_TILE_W) + (y / MAP_TILE_H) * MAP_W] >= MAP_FIRST_DEADLY; } + +uint8_t map_update_gold(uint16_t x, uint16_t y) +{ + Rect src = { 0, 0, 160, 48 }; + Rect dst = { 0, 0, 8, 8 }; + + uint16_t mx = x / MAP_TILE_W; + uint16_t my = y / MAP_TILE_H; + + if (gold[mx + my * MAP_W] != 0xff) + { + /* this gold is collected */ + gold[mx + my * MAP_W] = 0xff; + + /* erase the background */ + dst.x = mx * MAP_TILE_W; + dst.y = my * MAP_TILE_H + MAP_OFFS_Y; + + uint8_t t = cmap[mx + my * MAP_W]; + src.x = (t % MAP_TILESET_COLS) * MAP_TILE_W; + src.y = (t / MAP_TILESET_COLS) * MAP_TILE_H; + + blitrc(binary_tiles_start, &src, &dst); + + total_gold--; + return 1; + } + + return 0; +} + +uint8_t map_is_complete() +{ + return total_gold == 0; +} -- cgit v1.2.3