This commit is contained in:
Bill Rossi 2025-01-19 10:10:42 -05:00
parent ad6a921bef
commit 465ac02048
7 changed files with 28 additions and 65 deletions

View File

@ -61,7 +61,10 @@ void load_action(Game *g, char *line) {
#include "data/actions.c" #include "data/actions.c"
void game_load_actions(Game *g) { void game_load_actions(Game *g) {
g->actions = malloc(sizeof(Actions));
g->actions->count = 0;
parse_multiline_string(g, data_actions_txt, &load_action); parse_multiline_string(g, data_actions_txt, &load_action);
printf("loaded actions\n");
} }
Action *find_action(Game *g, const char *command) { Action *find_action(Game *g, const char *command) {

View File

@ -19,7 +19,10 @@ void load_flag(Game *g, char *line) {
#include "data/flags.c" #include "data/flags.c"
void game_load_flags(Game *g) { void game_load_flags(Game *g) {
g->flags = malloc(sizeof(Flags));
g->flags->count = 0;
parse_multiline_string(g, data_flags_txt, &load_flag); parse_multiline_string(g, data_flags_txt, &load_flag);
printf("loaded flags\n");
} }
int flag_value(Flags *f, char *key) { int flag_value(Flags *f, char *key) {

View File

@ -16,8 +16,10 @@ Game *game_create(void) {
Game *g = malloc(sizeof(Game)); Game *g = malloc(sizeof(Game));
g->should_close = false; g->should_close = false;
g->rooms = malloc(sizeof(Rooms)); game_load_words(g);
g->rooms->count = 0; game_load_flags(g);
game_load_actions(g);
game_load_rooms(g);
Log *log = create_log(); Log *log = create_log();
g->log = log; g->log = log;
@ -31,15 +33,6 @@ Game *game_create(void) {
g->input = input; g->input = input;
g->words = malloc(sizeof(Words));
g->words->count = 0;
g->flags = malloc(sizeof(Flags));
g->flags->count = 0;
g->actions = malloc(sizeof(Actions));
g->actions->count = 0;
return g; return g;
} }
@ -50,45 +43,13 @@ void free_game(Game *g) {
free(g); free(g);
} }
bool string_in(const char *input, ...) {
va_list argp;
va_start(argp, input);
char *candidate;
while ((candidate = va_arg(argp, char*))) {
if (strcmp(input, candidate) == 0) {
va_end(argp);
return true;
}
}
return false;
}
CommandType command_from_string(const char *string) {
if (string_in(string, "QUIT", "Q", "EXIT", "CLOSE", NULL)) {
return COMMAND_QUIT;
}
if (string_in(string, "LOOK", "L", NULL)) {
return COMMAND_LOOK;
}
if (string_in(string, "NORTH", "N", NULL)) {
return COMMAND_NORTH;
}
if (string_in(string, "SOUTH", "S", NULL)) {
return COMMAND_SOUTH;
}
if (string_in(string, "EAST", "E", NULL)) {
return COMMAND_EAST;
}
if (string_in(string, "WEST", "W", NULL)) {
return COMMAND_WEST;
}
return COMMAND_UNKNOWN;
}
#define INVALID_COMMAND "I don't know how to %s" #define INVALID_COMMAND "I don't know how to %s"
void game_handle_command(Game *g, const char *command) { void game_handle_command(Game *g, const char *command) {
if (strlen(command) == 0) {
push_line_to_log(g->input->log, "?");
return;
}
Action *a = find_action(g, command); Action *a = find_action(g, command);
if (a) { if (a) {
if (strcmp(a->description, "*") != 0) push_line_to_log(g->input->log, a->description); if (strcmp(a->description, "*") != 0) push_line_to_log(g->input->log, a->description);

View File

@ -1,6 +1,8 @@
#ifndef _FD_GAME_ #ifndef _FD_GAME_
#define _FD_GAME_ #define _FD_GAME_
#include <stdbool.h>
typedef struct Game Game; typedef struct Game Game;
typedef enum CommandType { typedef enum CommandType {
@ -13,7 +15,6 @@ typedef enum CommandType {
COMMAND_WEST, COMMAND_WEST,
} CommandType; } CommandType;
#include <stdbool.h>
#include "input.h" #include "input.h"
#include "room.h" #include "room.h"
#include "log.h" #include "log.h"

View File

@ -1,28 +1,16 @@
#include "game.h"
#include "../raylib.h"
#include "log.h"
#include "input.h"
#include "parse.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#define TARGET_FPS 60 #include "../raylib.h"
#include "game.h"
int main(void) { int main(void) {
InitWindow(800, 450, "Text Adventure"); InitWindow(800, 450, "Text Adventure");
SetTargetFPS(TARGET_FPS); SetTargetFPS(60);
Game *g = game_create(); Game *g = game_create();
game_load_rooms(g);
g->current_room = &g->rooms->rooms[0];
game_load_words(g);
printf("loaded words\n");
game_load_flags(g);
printf("loaded flags\n");
game_load_actions(g);
printf("loaded actions\n");
game_run_until_close(g); game_run_until_close(g);
CloseWindow(); CloseWindow();

View File

@ -20,7 +20,11 @@ void load_room(Game *g, char *line) {
#include "data/rooms.c" #include "data/rooms.c"
void game_load_rooms(Game *g) { void game_load_rooms(Game *g) {
g->rooms = malloc(sizeof(Rooms));
g->rooms->count = 0;
parse_multiline_string(g, data_rooms_txt, &load_room); parse_multiline_string(g, data_rooms_txt, &load_room);
printf("loaded rooms\n");
g->current_room = &g->rooms->rooms[0];
} }
void free_room(Room r) { void free_room(Room r) {

View File

@ -23,7 +23,10 @@ void load_word(Game *g, char *line) {
#include "data/words.c" #include "data/words.c"
void game_load_words(Game *g) { void game_load_words(Game *g) {
g->words = malloc(sizeof(Words));
g->words->count = 0;
parse_multiline_string(g, data_words_txt, &load_word); parse_multiline_string(g, data_words_txt, &load_word);
printf("loaded words\n");
} }
Word *find_word(Words *words, char *word_or_syn) { Word *find_word(Words *words, char *word_or_syn) {