Add words and their synonyms

This commit is contained in:
Bill Rossi 2025-01-12 19:50:11 -05:00
parent a09f7106de
commit 204bf6c3c9
9 changed files with 61 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
raylib.h
game
data/*.c
*/data/*.c

View File

@ -3,10 +3,10 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
.PHONY: clean run
game: rooms.data transitions.data *.c
game: data/rooms.c data/transitions.c data/words.c *.c
$(CC) *.c $(CFLAGS) -o game
%.data: data/%.txt
data/%.c: data/%.txt
echo -n "char *data_$*_txt = \"" > data/$*.c
cat data/$*.txt | perl -pe 's/\n/\\n/g' >> data/$*.c
echo "\";" >> data/$*.c

View File

@ -0,0 +1,2 @@
PULL|PULL,YANK,TUG
ROPE|ROPE,CORD,STRING,CABLE

View File

@ -30,6 +30,9 @@ Game *game_create(void) {
g->input = input;
g->words = malloc(sizeof(Words));
g->words->count = 0;
return g;
}

View File

@ -18,6 +18,7 @@ typedef enum Command {
#include "room.h"
#include "transition.h"
#include "log.h"
#include "word.h"
struct Game {
bool should_close;
@ -26,6 +27,7 @@ struct Game {
Rooms *rooms;
Room *current_room;
Transitions *transitions;
Words *words;
};
Game *game_create(void);

View File

@ -17,6 +17,7 @@ int main(void) {
game_load_rooms(g);
g->current_room = &g->rooms->rooms[0];
game_load_transitions(g);
game_load_words(g);
game_run_until_close(g);
CloseWindow();

27
01_text_adventure/word.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word.h"
#include "game.h"
#include "util.h"
void load_word(Game *g, char *line) {
char *token = strtok(line, "|");
Word *word = &g->words->words[g->words->count];
word->word = malloc(strlen(token) + 1);
strcpy(word->word, token);
word->synonyms_count = 0;
while ((token = strtok(NULL, ",")) != NULL) {
word->synonyms[word->synonyms_count] = malloc(strlen(token) + 1);
strcpy(word->synonyms[word->synonyms_count], token);
word->synonyms_count++;
}
g->words->count++;
}
#include "data/words.c"
void game_load_words(Game *g) {
parse_multiline_string(g, data_words_txt, &load_word);
}

21
01_text_adventure/word.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef _FD_WORD_
#define _FD_WORD_
typedef struct Word Word;
typedef struct Words Words;
#include "game.h"
struct Word {
char *word;
char* synonyms[100];
int synonyms_count;
};
struct Words {
Word words[200];
int count;
};
void game_load_words(Game *g);
#endif