-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPC.cpp
More file actions
235 lines (204 loc) · 4.5 KB
/
NPC.cpp
File metadata and controls
235 lines (204 loc) · 4.5 KB
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
231
232
233
234
235
// NPC.cpp: implementation of the CNPC class.
//
//////////////////////////////////////////////////////////////////////
#include "NPC.h"
#include <fstream>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNPC::CNPC()
{
Ticker = 0;
Speed = 1;
Alive = true;
// MoveStep = STEPS_TO_MOVE;
TimeToMove = 30;
Direction = RIGHT;
NPCFollow = NULL;
isFollowingUs = NULL;
// Default position 5,3
SetPosTile(rand()%20 + 1, rand()%15 + 1);
}
CNPC::~CNPC()
{
}
void CNPC::SetArea(int a, int b, int c, int d)
{
// Sets area fish can roam.
TileLeftBorder = a;
TileUpBorder = b;
TileRightBorder = c;
TileDownBorder = d;
}
bool CNPC::GetTicker()
{
// counter for every other tic.
Ticker++;
if (Ticker > 5)
{
Ticker = 0;
return true;
}
return false;
}
bool CNPC::Move(int aDirection)
{
if (!Alive)
return false;
Direction = aDirection;
return CCharacter::Move(Direction, Speed);
}
bool CNPC::Move()
{
// Move character in a random direction.
// Get random number 1-TimeToMove, if it equals TimeToMove then
// let the player move in a random direction.
if (!Alive)
return false;
isAnimated = false;
if (MoveStep > 0)
{
// Moving between tiles.
isAnimated = true;
return CCharacter::Draw2();
}
if (NPCFollow != NULL)
{
// Move towards another NPC.
if (GetTileX() < NPCFollow->GetTileX())
Direction = RIGHT;
else if (GetTileX() > NPCFollow->GetTileX())
Direction = LEFT;
else if (GetTileY() < NPCFollow->GetTileY())
Direction = DOWN;
else if (GetTileY() > NPCFollow->GetTileY())
Direction = UP;
if (!CCharacter::Move(Direction, Speed))
{
// We're blocked, try to move around it.
if (Direction == RIGHT || Direction == LEFT)
{
// Can't move right, so try up or down.
if (GetTileY() < NPCFollow->GetTileY())
Direction = DOWN;
else if (GetTileY() > NPCFollow->GetTileY())
Direction = UP;
}
else if (Direction == UP || Direction == DOWN)
{
// Can't move up or down, so try left or right.
if (GetTileX() < NPCFollow->GetTileX())
Direction = RIGHT;
else if (GetTileX() > NPCFollow->GetTileX())
Direction = LEFT;
}
if (!CCharacter::Move(Direction, Speed))
{
// Ok, problem. We are losing the leader because we're stuck in a corner.
// Let's think here.
}
}
}
else
{
// Only move if we are not currently walking towards a tile.
int r = rand() % (TimeToMove+1); // 0 - TimeToMove
if (r == TimeToMove)
{
r = rand() % 4 + 1; // 1 - 4
switch (r)
{
case 1 : Direction = LEFT; break;
case 2 : Direction = RIGHT; break;
case 3 : Direction = UP; break;
case 4 : Direction = DOWN; break;
};
CCharacter::Move(Direction, Speed);
}
}
/* if (!CCharacter::Move(Direction, Speed))
{
MoveStep = 0;
// If it can't move in first choice direction, try any direction.
/* for (int i=1; i < 5; i++)
{
switch (i)
{
case 1 : Direction = LEFT; break;
case 2 : Direction = RIGHT; break;
case 3 : Direction = UP; break;
case 4 : Direction = DOWN; break;
};
bool result = false;
//if (Direction != r)
//{
// Try a new direction.
result = CCharacter::Move(Direction, Speed);
//}
if (result == true)
{
// The new direction worked, we moved, so we're done.
//Direction = i;
return true;
}
else
{
}
}
// If we get here, we could not move. Must be stuck in a hole?
return false;*/
/* }
else
{
// We were able to move first time.
return true;
}
*/
return false;
}
bool CNPC::Draw()
{
if (!Alive)
return false;
int ret = CCharacter::Draw2();
// Only draw NPC extras like bubbles if NPC is on screen.
if (ret)
{
}
return ret;
}
void CNPC::Die()
{
// This fish has been killed.
Alive = false;
CharacterMap->CharacterMap[GetTileX()][GetTileY()] = 0;
// Show die animation.
}
void CNPC::Revive()
{
Alive = true;
CharacterMap->CharacterMap[GetTileX()][GetTileY()] = 1;
}
bool CNPC::Talk(int PlayerTileX, int PlayerTileY)
{
if (isAlive() && ((abs(GetTileX() - PlayerTileX) == 1 &&
GetTileY() == PlayerTileY) ||
(abs(GetTileY() - PlayerTileY) == 1 &&
GetTileX() == PlayerTileX)))
{
Speak();
return true;
}
return false;
}
bool CNPC::CheckTalk(int PlayerTileX, int PlayerTileY)
{
if (isAlive() && ((abs(GetTileX() - PlayerTileX) == 1 &&
GetTileY() == PlayerTileY) ||
(abs(GetTileY() - PlayerTileY) == 1 &&
GetTileX() == PlayerTileX)))
{
return true;
}
return false;
}