Items + showing them in rooms
This commit is contained in:
		
							parent
							
								
									b786a0fa04
								
							
						
					
					
						commit
						faccabcafc
					
				| @ -3,7 +3,7 @@ CFLAGS=-Wall -lraylib -lm -lpthread -ldl -lX11 | ||||
| 
 | ||||
| .PHONY: clean run | ||||
| 
 | ||||
| game: data/actions.c data/rooms.c data/room_ins.c data/words.c data/flags.c *.c | ||||
| game: data/actions.c data/rooms.c data/room_ins.c data/words.c data/flags.c data/items.c *.c | ||||
| 	$(CC) *.c $(CFLAGS) -o game | ||||
| 
 | ||||
| data/%.c: data/%.txt | ||||
|  | ||||
							
								
								
									
										4
									
								
								01_text_adventure/data/items.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								01_text_adventure/data/items.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| UNLIT_TORCH | a torch | true | A sturdy torch. It's unlit. | ||||
| LIT_TORCH | a lit torch | true | A sturdy torch. It's on fire, and illuminates its surroundings. | ||||
| FLASK | a flask | true | An empty glass flask, with a cork. | ||||
| ANVIL | an anvil | false | A heavy iron anvil. | ||||
| @ -9,3 +9,6 @@ WEST|WEST,W | ||||
| LOOK|LOOK,L | ||||
| INVENTORY|INVENTORY,I,INV | ||||
| HELP|HELP,H | ||||
| ANVIL|ANVIL | ||||
| FLASK|FLASK,BOTTLE,JAR | ||||
| TORCH|TORCH | ||||
|  | ||||
| @ -32,6 +32,7 @@ void cause_effect(Game *g, Effect *e) { | ||||
|     break; | ||||
|   case EFFECT_LOOK_ROOM: | ||||
|     push_line_to_log(g->input->log, find_room_in(g)->description); | ||||
|     log_items_in_room(g, g->current_room); | ||||
|     break; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -11,6 +11,7 @@ | ||||
| #include "predicate.h" | ||||
| #include "action.h" | ||||
| #include "parse.h" | ||||
| #include "item.h" | ||||
| 
 | ||||
| Game *game_create(void) { | ||||
|   Game *g = malloc(sizeof(Game)); | ||||
| @ -21,6 +22,9 @@ Game *game_create(void) { | ||||
|   game_load_actions(g); | ||||
|   game_load_rooms(g); | ||||
|   game_load_room_ins(g); | ||||
|   game_load_items(g); | ||||
| 
 | ||||
|   g->items->items[0].location = &g->rooms->rooms[0]; | ||||
| 
 | ||||
|   Log *log = create_log(); | ||||
|   g->log = log; | ||||
|  | ||||
| @ -22,6 +22,7 @@ typedef enum CommandType { | ||||
| #include "word.h" | ||||
| #include "flag.h" | ||||
| #include "action.h" | ||||
| #include "item.h" | ||||
| 
 | ||||
| struct Game { | ||||
|   bool should_close; | ||||
| @ -33,6 +34,7 @@ struct Game { | ||||
|   Words *words; | ||||
|   Flags *flags; | ||||
|   Actions *actions; | ||||
|   Items *items; | ||||
| }; | ||||
| 
 | ||||
| Game *game_create(void); | ||||
|  | ||||
							
								
								
									
										52
									
								
								01_text_adventure/item.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								01_text_adventure/item.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,52 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "room.h" | ||||
| #include "item.h" | ||||
| #include "util.h" | ||||
| 
 | ||||
| void load_item(Game *g, char *line) { | ||||
|   char *token = strtok(line, "|"); | ||||
|   Item *item = &g->items->items[g->items->count]; | ||||
|   item->name = malloc(strlen(token) + 1); | ||||
|   strcpy(item->name, token); | ||||
| 
 | ||||
|   token = strtok(NULL, "|"); | ||||
|   item->indefinite = malloc(strlen(token) + 1); | ||||
|   strcpy(item->indefinite, token); | ||||
| 
 | ||||
|   token = strtok(NULL, "|"); | ||||
|   item->pickuppable = strcmp("true", token) == 0; | ||||
| 
 | ||||
|   token = strtok(NULL, "|"); | ||||
|   item->description = malloc(strlen(token) + 1); | ||||
|   strcpy(item->description, token); | ||||
| 
 | ||||
|   item->in_inventory = false; | ||||
|   item->location = NULL; | ||||
| 
 | ||||
|   g->items->count++; | ||||
| } | ||||
| 
 | ||||
| #include "data/items.c" | ||||
| void game_load_items(Game *g) { | ||||
|   g->items = malloc(sizeof(Items)); | ||||
|   g->items->count = 0; | ||||
|   parse_multiline_string(g, data_items_txt, &load_item); | ||||
|   printf("loaded items\n"); | ||||
| } | ||||
| 
 | ||||
| #define ITEM_IN_ROOM "There is %s here." | ||||
| void log_item_in_room(Game *g, Item *i) { | ||||
|   char *response = malloc(strlen(ITEM_IN_ROOM) + strlen(i->indefinite) + 1); | ||||
|   sprintf(response, ITEM_IN_ROOM, i->indefinite); | ||||
|   push_line_to_log(g->input->log, response); | ||||
|   free(response); | ||||
| } | ||||
| 
 | ||||
| void log_items_in_room(Game *g, Room *r) { | ||||
|   for (int i = 0; i < g->items->count; i++) { | ||||
|     if (g->items->items[i].location == r) log_item_in_room(g, &g->items->items[i]); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										27
									
								
								01_text_adventure/item.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								01_text_adventure/item.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| #ifndef _FD_ITEM_ | ||||
| #define _FD_ITEM_ | ||||
| 
 | ||||
| typedef struct Item Item; | ||||
| typedef struct Items Items; | ||||
| 
 | ||||
| #include "game.h" | ||||
| #include "room.h" | ||||
| 
 | ||||
| struct Item { | ||||
|   char *name; | ||||
|   char *indefinite; | ||||
|   bool pickuppable; | ||||
|   char *description; | ||||
|   bool in_inventory; | ||||
|   Room *location; | ||||
| }; | ||||
| 
 | ||||
| struct Items { | ||||
|   Item items[200]; | ||||
|   int count; | ||||
| }; | ||||
| 
 | ||||
| void game_load_items(Game *g); | ||||
| void log_items_in_room(Game *g, Room *r); | ||||
| 
 | ||||
| #endif | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user