From 922d49d6a0a49fa843f402fbe7ee984d1a812f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 15 Jan 2022 16:52:16 +0000 Subject: [PATCH] Switchable tiles --- Makefile | 2 ++ platformer.c | 45 ++++++++++++++++++++++++++++++++------------ switch1_off_tile.txt | 14 ++++++++++++++ switch1_on_tile.txt | 14 ++++++++++++++ tile1_off_tile.txt | 14 ++++++++++++++ tile1_on_tile.txt | 14 ++++++++++++++ 6 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 switch1_off_tile.txt create mode 100644 switch1_on_tile.txt create mode 100644 tile1_off_tile.txt create mode 100644 tile1_on_tile.txt diff --git a/Makefile b/Makefile index 493939e..c45c13c 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ NAME = platformer TILES = grass_tile.inc dirt_tile.inc flag_tile.inc \ + switch1_off_tile.inc switch1_on_tile.inc \ + tile1_off_tile.inc tile1_on_tile.inc \ stage1_tile.inc stage2_tile.inc star_tile.inc all: $(NAME) diff --git a/platformer.c b/platformer.c index f5c696f..c4dedef 100644 --- a/platformer.c +++ b/platformer.c @@ -29,6 +29,9 @@ enum palette { GRASS_DARK, FLAG_STEM, FLAG_FLAG, + SWITCH_SWITCH, + SWITCH1_HIGHLIGHT, + TILE1, STAR_STAR, ERROR, }; @@ -37,6 +40,10 @@ static enum palette playfield[PLAYFIELD_SIDE * PLAYFIELD_SIDE]; #include "grass_tile.inc" #include "dirt_tile.inc" #include "flag_tile.inc" +#include "switch1_off_tile.inc" +#include "switch1_on_tile.inc" +#include "tile1_off_tile.inc" +#include "tile1_on_tile.inc" #include "stage1_tile.inc" #include "stage2_tile.inc" @@ -60,12 +67,15 @@ static char *stages[] = { " " " " " " - " " - " " - " !" - "gggggggggg" + " A " + " 11g " + "@ !" + "ggg gg" }; +enum switches_state { ALL_OFF, SWITCH1 }; +enum switches_state switches_state; + static uint32_t main_window = 0; static uint32_t window_width = 600; static uint32_t window_height = 500; @@ -126,7 +136,8 @@ static char sample_tilemap(size_t x, size_t y) { static bool solid_tile(size_t x, size_t y) { char tile = sample_tilemap(x, y); - return tile != ' ' && tile != '!' && tile != '@'; + return tile != ' ' && tile != '!' && tile != '@' && tile != 'A' && + (tile != '1' || switches_state == SWITCH1); } static bool jumping = false; @@ -140,6 +151,7 @@ static size_t selection_index = 0; static void initialize_stage(void) { strcpy(tilemap, stages[selection_index]); + switches_state = ALL_OFF; jumping = false; on_ground = false; player_dx = 0; @@ -294,13 +306,17 @@ static void update_stage(struct timespec now, struct timespec dt_timespec) { // Touching tiles size_t midpoint_x = player_x + TILE_SIDE / 2; size_t midpoint_y = player_y + TILE_SIDE / 2; - if (sample_tilemap(midpoint_x, midpoint_y) == '!') { - stages_beat[selection_index] = true; - if (selection_index < STAGES - 1) - selection_index++; - game_mode = TITLESCREEN; - initialize(); - return; + switch(sample_tilemap(midpoint_x, midpoint_y)) { + case '!': + stages_beat[selection_index] = true; + if (selection_index < STAGES - 1) + selection_index++; + game_mode = TITLESCREEN; + initialize(); + return; + case 'A': + switches_state = SWITCH1; + break; } } @@ -351,6 +367,8 @@ static void draw_tiles(void) { case 'g': tile = grass_tile; break; case 'd': tile = dirt_tile; break; case '!': tile = flag_tile; break; + case 'A': tile = switches_state == SWITCH1 ? switch1_on_tile : switch1_off_tile; break; + case '1': tile = switches_state == SWITCH1 ? tile1_on_tile : tile1_off_tile; break; } if (tile) draw_tile(tile, tx * TILE_SIDE, ty * TILE_SIDE); @@ -413,6 +431,9 @@ int main(int argc, char *argv[]) { [GRASS_DARK] = make_color(100, 64, 0), [FLAG_STEM] = make_color(0, 0, 0), [FLAG_FLAG] = make_color(255, 0, 0), + [SWITCH_SWITCH] = make_color(200, 64, 0), + [SWITCH1_HIGHLIGHT] = make_color(0, 255, 0), + [TILE1] = make_color(0, 255, 0), [STAR_STAR] = make_color(255, 255, 0), [ERROR] = make_color(255, 0, 255), }; diff --git a/switch1_off_tile.txt b/switch1_off_tile.txt new file mode 100644 index 0000000..eddf36d --- /dev/null +++ b/switch1_off_tile.txt @@ -0,0 +1,14 @@ +xSWITCH_SWITCH +tTEXT + BG +---------- + + xx + xx + xxxxxx + xx xx + xx xx + tt + ttt + tt + tttt diff --git a/switch1_on_tile.txt b/switch1_on_tile.txt new file mode 100644 index 0000000..89974f2 --- /dev/null +++ b/switch1_on_tile.txt @@ -0,0 +1,14 @@ +xSWITCH_SWITCH +tSWITCH1_HIGHLIGHT + BG +---------- + + tt + ttt + tt + tttt + xx xx + xx xx + xxxxxx + xx + xx diff --git a/tile1_off_tile.txt b/tile1_off_tile.txt new file mode 100644 index 0000000..4b3850d --- /dev/null +++ b/tile1_off_tile.txt @@ -0,0 +1,14 @@ +xTILE1 +tTEXT + BG +---------- + x x x x + x +x + tt x +x ttt + tt x +x tttt + x +x + x x x x diff --git a/tile1_on_tile.txt b/tile1_on_tile.txt new file mode 100644 index 0000000..3fc40d5 --- /dev/null +++ b/tile1_on_tile.txt @@ -0,0 +1,14 @@ +xTILE1 +tTEXT + BG +---------- + xxxxxxxx +xxxxxxxxxx +xxxxxxxxxx +xxxxttxxxx +xxxtttxxxx +xxxxttxxxx +xxxttttxxx +xxxxxxxxxx +xxxxxxxxxx + xxxxxxxx