From e8a9caf304fb070b999571073061ac5314435e79 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Mon, 13 Jan 2025 19:37:38 -0500 Subject: [PATCH] Add flags --- 01_text_adventure/Makefile | 2 +- 01_text_adventure/data/flags.txt | 2 ++ 01_text_adventure/flag.c | 31 +++++++++++++++++++++++++++++++ 01_text_adventure/flag.h | 22 ++++++++++++++++++++++ 01_text_adventure/game.c | 5 +++++ 01_text_adventure/game.h | 2 ++ 01_text_adventure/main.c | 8 +++++--- 7 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 01_text_adventure/data/flags.txt create mode 100644 01_text_adventure/flag.c create mode 100644 01_text_adventure/flag.h diff --git a/01_text_adventure/Makefile b/01_text_adventure/Makefile index 5bd52b0..1f49760 100644 --- a/01_text_adventure/Makefile +++ b/01_text_adventure/Makefile @@ -3,7 +3,7 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 .PHONY: clean run -game: data/rooms.c data/transitions.c data/words.c *.c +game: data/rooms.c data/transitions.c data/words.c data/flags.c *.c $(CC) *.c $(CFLAGS) -o game data/%.c: data/%.txt diff --git a/01_text_adventure/data/flags.txt b/01_text_adventure/data/flags.txt new file mode 100644 index 0000000..deda289 --- /dev/null +++ b/01_text_adventure/data/flags.txt @@ -0,0 +1,2 @@ +LEVER_PULLED|0 +STEPS_TAKEN|0 diff --git a/01_text_adventure/flag.c b/01_text_adventure/flag.c new file mode 100644 index 0000000..4e0875e --- /dev/null +++ b/01_text_adventure/flag.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include "flag.h" +#include "game.h" +#include "util.h" + +void load_flag(Game *g, char *line) { + char *token = strtok(line, "|"); + Flag *flag = &g->flags->flags[g->flags->count]; + flag->key = malloc(strlen(token) + 1); + strcpy(flag->key, token); + + token = strtok(NULL, "|"); + flag->value = atoi(token); + + g->flags->count++; +} + +#include "data/flags.c" +void game_load_flags(Game *g) { + parse_multiline_string(g, data_flags_txt, &load_flag); +} + +int flag_value(Flags *f, char *key) { + for (int i = 0; i < f->count; i++) { + if (strcmp(f->flags[i].key, key) == 0) return f->flags[i].value; + } + + return -1; +} diff --git a/01_text_adventure/flag.h b/01_text_adventure/flag.h new file mode 100644 index 0000000..ac0d8d7 --- /dev/null +++ b/01_text_adventure/flag.h @@ -0,0 +1,22 @@ +#ifndef _FD_FLAG_ +#define _FD_FLAG_ + +typedef struct Flag Flag; +typedef struct Flags Flags; + +#include "game.h" + +struct Flag { + char *key; + int value; +}; + +struct Flags { + Flag flags[200]; + int count; +}; + +void game_load_flags(Game *g); +int flag_value(Flags *f, char *key); + +#endif diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index 68fdcf1..d1aebcf 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -7,6 +7,8 @@ #include "input.h" #include "log.h" #include "util.h" +#include "word.h" +#include "flag.h" Game *game_create(void) { Game *g = malloc(sizeof(Game)); @@ -33,6 +35,9 @@ Game *game_create(void) { g->words = malloc(sizeof(Words)); g->words->count = 0; + g->flags = malloc(sizeof(Flags)); + g->flags->count = 0; + return g; } diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h index 648c7f1..eeda252 100644 --- a/01_text_adventure/game.h +++ b/01_text_adventure/game.h @@ -19,6 +19,7 @@ typedef enum Command { #include "transition.h" #include "log.h" #include "word.h" +#include "flag.h" struct Game { bool should_close; @@ -28,6 +29,7 @@ struct Game { Room *current_room; Transitions *transitions; Words *words; + Flags *flags; }; Game *game_create(void); diff --git a/01_text_adventure/main.c b/01_text_adventure/main.c index b6db7aa..4d09ea7 100644 --- a/01_text_adventure/main.c +++ b/01_text_adventure/main.c @@ -19,10 +19,12 @@ int main(void) { g->current_room = &g->rooms->rooms[0]; game_load_transitions(g); game_load_words(g); + game_load_flags(g); + + for (int i = 0; i < g->flags->count; i++) { + printf("%s: %d\n", g->flags->flags[i].key, g->flags->flags[i].value); + } - parse(g, "PULL ROPE"); - parse(g, "YANK CORD"); - parse(g, "TUG STRING"); game_run_until_close(g); CloseWindow();