Add flags
This commit is contained in:
		
							parent
							
								
									842a6852cd
								
							
						
					
					
						commit
						e8a9caf304
					
				| @ -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 | ||||
|  | ||||
							
								
								
									
										2
									
								
								01_text_adventure/data/flags.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								01_text_adventure/data/flags.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| LEVER_PULLED|0 | ||||
| STEPS_TAKEN|0 | ||||
							
								
								
									
										31
									
								
								01_text_adventure/flag.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								01_text_adventure/flag.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,31 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #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; | ||||
| } | ||||
							
								
								
									
										22
									
								
								01_text_adventure/flag.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								01_text_adventure/flag.h
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
| @ -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; | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
| @ -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(); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user