-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThiefOwnedStates.cpp
230 lines (181 loc) · 6.04 KB
/
ThiefOwnedStates.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "ThiefOwnedStates.h"
#include "Locations.h"
#include "Thief.h"
#include "MessageDispatcher.h"
#include "MessageTypes.h"
#include "CrudeTimer.h"
#include "EntityNames.h"
#include <iostream>
using std::cout;
#ifdef TEXTOUTPUT
#include <fstream>
extern std::ofstream os;
#define cout os
#endif
//------------------------------------------------------------------------methods for EnterBankAndStealTheNugget
EnterBankAndStealTheNugget* EnterBankAndStealTheNugget::Instance()
{
static EnterBankAndStealTheNugget instance;
return &instance;
}
void EnterBankAndStealTheNugget::Enter(Thief* pThief)
{
//if the thief is not in the bank, he moves to the bank.
if (pThief->Location() != bank)
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "Walkin' to the bank";
pThief->ChangeLocation(bank);
}
if (!pThief->Stealing())
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": EveryBody calm down, it's a heist !";
//send a delayed message myself so that I know when to get ou of the bank.
Dispatch->DispatchMessage(1, //time delay
pThief->ID(), //sender ID
pThief->ID(), //receiver ID
Msg_ThiefInTheBank, // Type of message
NO_ADDITIONAL_INFO);
pThief->SetStealing(true);
}
}
void EnterBankAndStealTheNugget::Execute(Thief* pThief)
{
//Now the thief is in the bank and he stealing gold nuggets.
pThief->AddToGoldCarried(1);
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "Steal a nugget";
//if the thief is full of gold nuggets, he go back to his house.
if (pThief->PocketsFull())
{
pThief->GetFSM()->ChangeState(GoBackToHouseWithNugget::Instance());
}
}
void EnterBankAndStealTheNugget::Exit(Thief* pThief)
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": "
<< "Leaving the bank";
pThief->SetStealing(false);
}
bool EnterBankAndStealTheNugget::OnMessage(Thief* pThief, const Telegram& msg)
{
{
SetTextColor(BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
switch (msg.Msg)
{
// When he receives this message, he go back to his house.
case Msg_ThiefInTheBank:
{
cout << "\nMessage received by " << GetNameOfEntity(pThief->ID()) <<
" at time: " << Clock->GetCurrentTime();
SetTextColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": Time to get out of the bank";
pThief->SetStealing(false);
pThief->GetFSM()->ChangeState(GoBackToHouseWithNugget::Instance());
}
// When he receives this message, it means that bob is in the bank and
// he surrenders
case Msg_HoldUp:
{
cout << "\nMessage received by " << GetNameOfEntity(pThief->ID()) <<
" at time: " << Clock->GetCurrentTime();
SetTextColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": No, i surrender, don't kill me !";
pThief->SetStealing(false);
pThief->GetFSM()->ChangeState(GoToPrisonWithMiner::Instance());
}
return true;
}//end switch
return false;
}
}
//------------------------------------------------------------------------methods for GoBackToHouseWithNugget
GoBackToHouseWithNugget* GoBackToHouseWithNugget::Instance()
{
static GoBackToHouseWithNugget instance;
return &instance;
}
void GoBackToHouseWithNugget::Enter(Thief* pThief)
{
//if the thief is not already located at his house, he must
//change location to his house
if (pThief->Location() != thief_house)
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "Yeepee Yo yeepee yeah goin' home with pockets full of gold !";
pThief->ChangeLocation(thief_house);
}
}
void GoBackToHouseWithNugget::Execute(Thief* pThief)
{
//Now the Thief is at his home and he stores the gold he stole
//and looks to his treasure to determine if he stays at home or
//go back to the bank to steal other gold nuggets
pThief->AddToTreasure(pThief->GoldCarried());
pThief->SetGoldCarried(0);
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "Store his stolen nuggets";
if (pThief->Treasure() < ComfortLevelLupin)
{
pThief->GetFSM()->ChangeState(EnterBankAndStealTheNugget::Instance());
}
}
void GoBackToHouseWithNugget::Exit(Thief* pThief)
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": "
<< "Leaving his house";
}
bool GoBackToHouseWithNugget::OnMessage(Thief* pThief, const Telegram& msg)
{
return false;
}
//------------------------------------------------------------------------methods for GoToPrisonWithMiner
GoToPrisonWithMiner* GoToPrisonWithMiner::Instance()
{
static GoToPrisonWithMiner instance;
return &instance;
}
void GoToPrisonWithMiner::Enter(Thief* pThief)
{
//if the thief is not already in prison, he goes to the prison.
if (pThief->Location() != prison)
{
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "Walkin' to the prison with Bob";
pThief->ChangeLocation(prison);
}
if (!pThief->InPrison())
{
// Send a message to myself to know when i can be free.
Dispatch->DispatchMessage(5, //time delay
pThief->ID(), //sender ID
pThief->ID(), //receiver ID
Msg_FreeToGo, // Type of message
NO_ADDITIONAL_INFO);
pThief->SetInPrison(true);
}
}
void GoToPrisonWithMiner::Execute(Thief* pThief)
{
//Now the thief is in prison. He will be free in a certain period of time when he will receive a message
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": " << "i'm in prison now... what a shame";
}
void GoToPrisonWithMiner::Exit(Thief* pThief)
{
}
bool GoToPrisonWithMiner::OnMessage(Thief* pThief, const Telegram& msg)
{
{
SetTextColor(BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
switch (msg.Msg)
{
// When he receives this message, he go back to his house.
case Msg_FreeToGo:
{
cout << "\nMessage received by " << GetNameOfEntity(pThief->ID()) <<
" at time: " << Clock->GetCurrentTime();
SetTextColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "\n" << GetNameOfEntity(pThief->ID()) << ": Yeah ! Free to go !";
pThief->SetInPrison(false);
pThief->GetFSM()->ChangeState(GoBackToHouseWithNugget::Instance());
}
return true;
}
return false;
}
}