aboutsummaryrefslogtreecommitdiff
path: root/frontend/GameController.cpp
blob: f160006a7b94357a2c5c940f65a8880f1ad506c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "generate_dungeon.h"
#include "load_dungeon.h"
#include "rl.h"
#include "GameData.h"
#include "strings.h"
#include "GameController.h"

#include "backend/Exception.h"
#include "backend/print.h"
#include "backend/WeaponObject.h"
#include "backend/Dungeon.h"

using namespace std;

GameController::GameController() {
	// Ensure DB is accessible
	GameData::get_instance();
}

void GameController::cmdset_default() {
	this->cmds.clear();
	this->cmds["Kijk"] = &GameController::cmd_query;
	this->cmds["Zoek"] = &GameController::cmd_search;
	this->cmds["Ga"] = &GameController::cmd_go;
	this->cmds["Pak"] = &GameController::cmd_get;
	this->cmds["Leg"] = &GameController::cmd_put;
	this->cmds["Bekijk"] = &GameController::cmd_view;
	this->cmds["Sla"] = &GameController::cmd_hit;
	this->cmds["Draag"] = &GameController::cmd_equip;
	this->cmds["Wacht"] = &GameController::cmd_wait;
	this->cmds["Consumeer"] = &GameController::cmd_use;
	this->cmds["Help"] = &GameController::cmd_help;
	this->cmds["Godmode"] = &GameController::cmd_cheat;
	this->cmds["Quit"] = &GameController::cmd_quit;
}

void GameController::cmdset_death() {
	this->cmds.clear();
	this->cmds["Help"] = &GameController::cmd_help;
	this->cmds["Quit"] = &GameController::cmd_quit;
	this->cmds["Opnieuw"] = &GameController::cmd_restart;
}

void GameController::cmd(string & argv) {
	if (argv.size() == 0) return;
	string cmd = str_title(str_consume_arg(argv));

	if (this->cmds.contains(cmd))
		return (this->*cmds.at(cmd))(argv);

	str_print(strings::UNKNOWN_CMD);
}

unique_ptr<Dungeon> GameController::make_dungeon() noexcept {
	while (1) {
		str_print(strings::INTRO);
		string filename = rl();
		try {
			if (filename.size() == 0) {
				return generate_dungeon();
			} else {
				return load_dungeon(filename);
			}
		} catch (Exception & e) {
			lprtf("FOUT: %s\n", e.what());
		}
	}
}

void GameController::gameloop() {
	this->dungeon = make_dungeon();
	Player & player = this->dungeon->get_player();

	lprtf("Wat is de naam van je karakter?\n");
	player.name = rl().c_str();
	player.set_location(dungeon->get_start_location());
	player.equip(static_cast<WeaponObject *>(GameData::get_instance().create_object("Dolk")));

	cmdset_default();

	this->playing = true;
	while (this->playing) {
		string line = rl();
		if (line.length() == 0) continue;
		try {
			this->cmd(line);
		} catch (Exception & e) {
			lprtf("FOUT: %s\n", e.what());
		}

		if (player.is_dead())
			cmdset_death();
	}
}

int GameController::main() {
	while (!this->quit) {
		this->gameloop();
	}
	return EXIT_SUCCESS;
}