This repository has been archived by the owner on May 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dialog.cpp
158 lines (126 loc) · 4.48 KB
/
Dialog.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
156
157
158
//*** Class CDialog ***//
#include "CDialog.hpp"
/**
* Constructor
* @parameter map<string, CDialogState*> (dictionary of dialog-states)
*/
CDialog::CDialog(std::map<std::string, CDialogState*> mapStates, std::string sEventmanager) {
//Assign attributes
m_mapStates = mapStates;
if(m_EM->getStaticManagers().count(sEventmanager) != 0)
m_EM = m_EM->getStaticManagers().at(sEventmanager);
else
m_EM = m_EM->getStaticManagers().at("standard");
}
//startDialog
void CDialog::startDialog()
{
//Attributes:
std::string sIndex = "START"; //Index points to the next state to be calles. First state
//Always has the id "START"
//Loop running till end of dialog is reached
for(;;)
{
//Get current state
CDialogState* curState = m_mapStates.at(sIndex);
//Call state function
callStateFunction(curState);
//Throw event "dialogstate [index] started"
CEvent* event = new CEvent(sIndex, "");
m_EM->throw_event(event);
/*
//Throw all eventmanagers of player
for(auto it=player.getEventmanagers().begin(); it!=player.getEventmanagers().end(); it++)
it->second->throw_event(event);
*/
delete event;
//Check wether end is reached
if(curState->getEnd() == true)
{
std::cout << curState->getDialogEnd() << "\n";
break;
}
//Print options
std::list<CDialogOptionState*> listPlayerOptions = curState->getPlayerOptions();
for(auto it=listPlayerOptions.begin(); it!=listPlayerOptions.end(); it++)
{
if((*it)->getActive() == true)
std::cout << (*it)->getKeyword() << ": " << (*it)->getText() << "\n";
}
bool input = false;
while(input == false)
{
//Player option
std::string sChoice;
size_t choice;
std::cout << ">";
getline(std::cin, sChoice);
//Convert to int
if(is_number(sChoice) == false)
{
std::cout << "Wrong input! Enter a number.\n";
continue;
}
choice = std::stoi(sChoice);
//If choice is within range, print player's text, then change index to target state
CDialogOptionState* optState = curState->getOptState(choice, true);
//Check whether state exists
if(!optState)
{
std::cout << "Wrong input! Select one of the options.\n";
continue;
}
//Print players text and change index to target state
std::cout << "\nPLAYER " << curState->getOptState(choice, true)->getText() << "\n";
sIndex.assign(curState->getOptState(choice, true)->getTargetState());
input = true;
}
}
std::cout << "\n";
}
bool CDialog::is_number(const std::string& s)
{
return !s.empty() && std::find_if(s.begin(),
s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}
// *** Functions *** //
//Initialize map of all dialog-state-functions
std::map<std::string, void(CDialog::*)(CDialogState*)>CDialog::m_mapDialogFuncs = {};
/**
* initializeFunctions: adds all functions to map of dialog-state-functions.
*/
void CDialog::initializeFunctions()
{
// *** Add dialog-state-function to map *** //
m_mapDialogFuncs["standard"] = &CDialog::func_standard;
m_mapDialogFuncs["parsen_anna"] = &CDialog::func_parsen_anna;
}
/**
* callStateFunction: call function of given state
* parameter CDialogState* (dialog state)
*/
void CDialog::callStateFunction(CDialogState* state) {
(this->*m_mapDialogFuncs.at(state->getFunction()))(state);
}
/**
* func_standard: standard function for calling dialog state
* @parameter CDialogState* (Dialog state which was called.)
*/
void CDialog::func_standard(CDialogState* state)
{
//Print text
std::cout << state->getSpeaker() << " " << state->getText() << "\n";
}
/**
* func_parsen_anna: change dialog of parsen
* @parameter CDialogState* (Dialog state which was called)
*/
void CDialog::func_parsen_anna(CDialogState* state)
{
//Print text
std::cout << state->getSpeaker() << " " << state->getText() << "\n";
//Change dialog of parsen
CDialogOptionState* optState = m_mapStates.at("START")->getOptState(3, false);
m_mapStates.at("START")->getOptState(3, true)->setActive(false);
optState->setActive(true);
}