-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameEngine.cpp
157 lines (141 loc) · 3.51 KB
/
GameEngine.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//==============================================================================
#include "GameEngine.h"
#include "TextRenderer.h"
#include "character.h"
#include "renderer.h"
#include "object.h"
#include <unordered_set>
#include <iostream>
#include <sstream>
#include <array>
GameEngine::GameEngine() :
m_running(true),
m_world(),
m_renderer(std::unique_ptr<Renderer>(new TextRenderer()))
{
}
GameEngine::~GameEngine() {}
void GameEngine::Run()
{
Init();
m_renderer->Draw(m_world);
Render();
while(m_running)
{
std::string command;
std::getline(std::cin, command);
UserInput(command);
Render();
}
CleanUp();
}
void GameEngine::Init()
{
m_world.LoadWorld("world.txt");
}
void GameEngine::UserInput(const std::string& command)
{
if(command == "quit")
{
m_running = false;
return;
}
std::stringstream ss(command);
std::string token;
ss >> token;
if(token == "examine")
{
std::ws(ss);
std::getline(ss, token);
auto currentRoom = m_world.GetPlayerRoom();
std::shared_ptr<Object> object = currentRoom->GetObjectByName(token);
if(object)
{
m_renderer->DrawText(object->Examine() + "\n");
}
else
{
m_renderer->DrawText(token + " is nowhere to be found here.\n");
}
return;
}
if(token == "open")
{
std::ws(ss);
std::getline(ss, token);
auto currentRoom = m_world.GetPlayerRoom();
auto object = currentRoom->GetObjectByName(token);
if(object)
{
m_renderer->DrawText(object->Open() + "\n");
}
else
{
m_renderer->DrawText(token + " is nowhere to be found here.\n");
}
return;
}
if(token == "use")
{
std::array<std::string, 2> arguments;
int i = 0;
ss.unsetf(std::ios::skipws);
std::ws(ss);
std::getline(ss, token);
auto position = token.find("with");
if(position == std::string::npos)
{
m_renderer->DrawText("Command unknown.\n");
return;
}
arguments[0] = token.substr(0, position - 1);
arguments[1] = token.substr(position + 5);
auto currentRoom = m_world.GetPlayerRoom();
auto object1 = currentRoom->GetObjectByName(arguments[0]);
if(!object1)
{
m_renderer->DrawText(arguments[0] + " is nowhere to be found.\n");
return;
}
auto object2 = currentRoom->GetObjectByName(arguments[1]);
if(!object2)
{
m_renderer->DrawText(arguments[1] + " is nowhere to be found.\n");
return;
}
m_renderer->DrawText(object1->UseWith(object2));
return;
}
if(token == "inventory")
{
//Show inventory
}
if(token == "go")
{
if(ss >> token && token == "to")
{
//Skip whitespaces
std::ws(ss);
std::getline(ss, token);
auto player = m_world.GetPlayer();
auto newRoom = player->GetLocation()->GetExitTo(token);
if(newRoom)
{
player->SetLocation(newRoom);
m_renderer->Draw(m_world);
}
else
{
m_renderer->DrawText(token + " is not a valid exit.\n");
}
return;
}
}
}
void GameEngine::Render()
{
m_renderer->Display();
}
void GameEngine::CleanUp()
{
}