Move from room to room!
This commit is contained in:
parent
db45910125
commit
c51a25f0b5
@ -3,6 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "transition.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
@ -74,6 +75,22 @@ Command command_from_string(const char *string) {
|
|||||||
return COMMAND_UNKNOWN;
|
return COMMAND_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define INVALID_DIRECTIONAL_COMMAND "I can't go %s from here"
|
||||||
|
void game_handle_directional_command(Game *g, const char *c) {
|
||||||
|
Room *r = g->current_room;
|
||||||
|
Transition *transition = find_transition(g->transitions, r, command_from_string(c));
|
||||||
|
|
||||||
|
if (transition) {
|
||||||
|
push_line_to_log(g->input->log, transition->description);
|
||||||
|
g->current_room = transition->to;
|
||||||
|
} else {
|
||||||
|
char *response = malloc(strlen(INVALID_DIRECTIONAL_COMMAND) + strlen(c) + 1);
|
||||||
|
sprintf(response, INVALID_DIRECTIONAL_COMMAND, c);
|
||||||
|
push_line_to_log(g->input->log, response);
|
||||||
|
free(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#define INVALID_COMMAND "I don't know how to "
|
#define INVALID_COMMAND "I don't know how to "
|
||||||
void game_handle_command(Game *g, const char *command) {
|
void game_handle_command(Game *g, const char *command) {
|
||||||
Input *input = g->input;
|
Input *input = g->input;
|
||||||
@ -85,6 +102,12 @@ void game_handle_command(Game *g, const char *command) {
|
|||||||
case COMMAND_LOOK:
|
case COMMAND_LOOK:
|
||||||
push_line_to_log(input->log, g->current_room->description);
|
push_line_to_log(input->log, g->current_room->description);
|
||||||
break;
|
break;
|
||||||
|
case COMMAND_NORTH:
|
||||||
|
case COMMAND_SOUTH:
|
||||||
|
case COMMAND_EAST:
|
||||||
|
case COMMAND_WEST:
|
||||||
|
game_handle_directional_command(g, command);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
char *response = malloc(strlen(INVALID_COMMAND) + strlen(command) + 1);
|
char *response = malloc(strlen(INVALID_COMMAND) + strlen(command) + 1);
|
||||||
sprintf(response, INVALID_COMMAND "%s", command);
|
sprintf(response, INVALID_COMMAND "%s", command);
|
||||||
|
@ -15,6 +15,7 @@ int main(void) {
|
|||||||
|
|
||||||
Game *g = game_create();
|
Game *g = game_create();
|
||||||
game_load_rooms(g);
|
game_load_rooms(g);
|
||||||
|
game_load_transitions(g);
|
||||||
|
|
||||||
game_run_until_close(g);
|
game_run_until_close(g);
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
@ -30,3 +30,12 @@ void game_load_transitions(Game *g) {
|
|||||||
|
|
||||||
fclose(transitions_file);
|
fclose(transitions_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Transition *find_transition(Transitions *t, Room *from, Command via) {
|
||||||
|
for (int i = 0; i < t->count; i++) {
|
||||||
|
Transition *candidate = &t->transitions[i];
|
||||||
|
if (candidate->from == from && candidate->via == via) return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
@ -20,5 +20,6 @@ struct Transitions {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void game_load_transitions(Game *g);
|
void game_load_transitions(Game *g);
|
||||||
|
Transition *find_transition(Transitions *t, Room *from, Command via);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user