1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Oli Scheuss |
---|
24 | * Co-authors: |
---|
25 | * Damian 'Mozork' Frick |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "PacmanGhost.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | RegisterClass(PacmanGhost); |
---|
37 | |
---|
38 | /** |
---|
39 | @brief |
---|
40 | Constructor. Registers the object and initializes some default values. |
---|
41 | @param creator |
---|
42 | The creator of this object. |
---|
43 | */ |
---|
44 | PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context) |
---|
45 | { |
---|
46 | RegisterObject(PacmanGhost); |
---|
47 | |
---|
48 | this->velocity = Vector3(0, 0, 0); |
---|
49 | |
---|
50 | this->setCollisionType(CollisionType::Dynamic); |
---|
51 | |
---|
52 | this->actuelposition = this->getPosition(); |
---|
53 | |
---|
54 | this->target_x = actuelposition.x; |
---|
55 | this->target_z = actuelposition.z; |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | @brief |
---|
61 | Destructor. Destroys controller, if present. |
---|
62 | */ |
---|
63 | PacmanGhost::~PacmanGhost() |
---|
64 | { |
---|
65 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | @brief |
---|
70 | Method for creating a AutonomousDrone through XML. |
---|
71 | */ |
---|
72 | void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
73 | { |
---|
74 | SUPER(PacmanGhost, XMLPort, xmlelement, mode); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | Vector3 possibleposition[] = {Vector3(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4 |
---|
80 | Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9 |
---|
81 | Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14 |
---|
82 | Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19 |
---|
83 | Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24 |
---|
84 | Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29 |
---|
85 | Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34 |
---|
86 | Vector3(-70,10,105),Vector3(-20,10,105),Vector3(20,10,105),Vector3(70,10,105),Vector3(70,10,60), //35-39 |
---|
87 | Vector3(0,10,60),Vector3(-70,10,60),Vector3(-135,10,15),Vector3(-70,10,60),Vector3(0,10,15), //40-44 |
---|
88 | Vector3(70,10,15),Vector3(-70,10,-35),Vector3(-20,10,-35),Vector3(20,10,-35),Vector3(70,10,-35), //45-49 |
---|
89 | Vector3(70,10,-85),Vector3(20,10,-85),Vector3(-20,10,-85),Vector3(-70,10,-85),Vector3(-135,10,-85), //50-54 |
---|
90 | Vector3(-215,10,-85),Vector3(-215,10,-135),Vector3(-135,10,-135),Vector3(-70,10,-135),Vector3(-20,10,-135), //55-59 |
---|
91 | Vector3(20,10,-135),Vector3(70,10,-135),Vector3(20,10,-195),Vector3(-20,10,-195),Vector3(-135,10,-195), //60-64 |
---|
92 | Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66 |
---|
93 | |
---|
94 | /** |
---|
95 | @brief |
---|
96 | Defines which actions the AutonomousDrone has to take in each tick. |
---|
97 | @param dt |
---|
98 | The length of the tick. |
---|
99 | */ |
---|
100 | void PacmanGhost::tick(float dt) |
---|
101 | { |
---|
102 | SUPER(PacmanGhost, tick, dt); |
---|
103 | |
---|
104 | //setorientation |
---|
105 | |
---|
106 | this->actuelposition = this->getPosition(); |
---|
107 | |
---|
108 | //Stop, if target arrived |
---|
109 | if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){ |
---|
110 | this->ismoving = false; |
---|
111 | } |
---|
112 | |
---|
113 | //Move, if ghost hasn't arrived yet |
---|
114 | if(this->ismoving){ |
---|
115 | if(!(abs(this->actuelposition.z-target_z)<0.5)) { |
---|
116 | velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)*2); |
---|
117 | move(dt, actuelposition, velocity); |
---|
118 | } |
---|
119 | if(!(abs(this->actuelposition.x-target_x)<0.5)){ |
---|
120 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x)*2,0,0); |
---|
121 | move(dt, actuelposition, velocity); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | //Check on which position ghost has arrived and set new target |
---|
126 | else{ |
---|
127 | if(findpos(actuelposition,possibleposition[0])){ |
---|
128 | setnewTarget(1,17,19); |
---|
129 | } |
---|
130 | else if(findpos(actuelposition,possibleposition[1])){ |
---|
131 | setnewTarget(0,2); |
---|
132 | } |
---|
133 | else if(findpos(actuelposition,possibleposition[2])){ |
---|
134 | setnewTarget(1,3); |
---|
135 | } |
---|
136 | else if(findpos(actuelposition,possibleposition[3])){ |
---|
137 | setnewTarget(2,4,5); |
---|
138 | } |
---|
139 | else if(findpos(actuelposition,possibleposition[4])){ |
---|
140 | setnewTarget(3,6); |
---|
141 | } |
---|
142 | else if(findpos(actuelposition,possibleposition[5])){ |
---|
143 | setnewTarget(3,7); |
---|
144 | } |
---|
145 | else if(findpos(actuelposition,possibleposition[6])){ |
---|
146 | setnewTarget(4,9,26); |
---|
147 | } |
---|
148 | else if(findpos(actuelposition,possibleposition[7])){ |
---|
149 | setnewTarget(5,8); |
---|
150 | } |
---|
151 | else if(findpos(actuelposition,possibleposition[8])){ |
---|
152 | setnewTarget(7,9); |
---|
153 | } |
---|
154 | else if(findpos(actuelposition,possibleposition[9])){ |
---|
155 | setnewTarget(6,8,10,38); |
---|
156 | } |
---|
157 | else if(findpos(actuelposition,possibleposition[10])){ |
---|
158 | setnewTarget(9,11,45); |
---|
159 | } |
---|
160 | else if(findpos(actuelposition,possibleposition[11])){ |
---|
161 | setnewTarget(10,12,13); |
---|
162 | } |
---|
163 | else if(findpos(actuelposition,possibleposition[12])){ |
---|
164 | setnewTarget(11,14); |
---|
165 | } |
---|
166 | else if(findpos(actuelposition,possibleposition[13])){ |
---|
167 | setnewTarget(11,14,16,61); |
---|
168 | } |
---|
169 | else if(findpos(actuelposition,possibleposition[14])){ |
---|
170 | setnewTarget(12,13,15); |
---|
171 | } |
---|
172 | else if(findpos(actuelposition,possibleposition[15])){ |
---|
173 | setnewTarget(14,16); |
---|
174 | } |
---|
175 | else if(findpos(actuelposition,possibleposition[16])){ |
---|
176 | setnewTarget(13,15,62); |
---|
177 | } |
---|
178 | else if(findpos(actuelposition,possibleposition[17])){ |
---|
179 | setnewTarget(0,25); |
---|
180 | } |
---|
181 | else if(findpos(actuelposition,possibleposition[18])){ |
---|
182 | setnewTarget(19,24); |
---|
183 | } |
---|
184 | else if(findpos(actuelposition,possibleposition[19])){ |
---|
185 | setnewTarget(0,18,20); |
---|
186 | } |
---|
187 | else if(findpos(actuelposition,possibleposition[20])){ |
---|
188 | setnewTarget(19,21); |
---|
189 | } |
---|
190 | else if(findpos(actuelposition,possibleposition[21])){ |
---|
191 | setnewTarget(20,22); |
---|
192 | } |
---|
193 | else if(findpos(actuelposition,possibleposition[22])){ |
---|
194 | setnewTarget(21,23,31); |
---|
195 | } |
---|
196 | else if(findpos(actuelposition,possibleposition[23])){ |
---|
197 | setnewTarget(22,30); |
---|
198 | } |
---|
199 | else if(findpos(actuelposition,possibleposition[24])){ |
---|
200 | setnewTarget(18,29); |
---|
201 | } |
---|
202 | else if(findpos(actuelposition,possibleposition[25])){ |
---|
203 | setnewTarget(17,26); |
---|
204 | } |
---|
205 | else if(findpos(actuelposition,possibleposition[26])){ |
---|
206 | setnewTarget(6,25,27); |
---|
207 | } |
---|
208 | else if(findpos(actuelposition,possibleposition[27])){ |
---|
209 | setnewTarget(26,28,37); |
---|
210 | } |
---|
211 | else if(findpos(actuelposition,possibleposition[28])){ |
---|
212 | setnewTarget(27,29,36); |
---|
213 | } |
---|
214 | else if(findpos(actuelposition,possibleposition[29])){ |
---|
215 | setnewTarget(24,28,30); |
---|
216 | } |
---|
217 | else if(findpos(actuelposition,possibleposition[30])){ |
---|
218 | setnewTarget(23,29,34); |
---|
219 | } |
---|
220 | else if(findpos(actuelposition,possibleposition[31])){ |
---|
221 | setnewTarget(22,32); |
---|
222 | } |
---|
223 | else if(findpos(actuelposition,possibleposition[32])){ |
---|
224 | setnewTarget(31,33); |
---|
225 | } |
---|
226 | else if(findpos(actuelposition,possibleposition[33])){ |
---|
227 | setnewTarget(32,34); |
---|
228 | } |
---|
229 | else if(findpos(actuelposition,possibleposition[34])){ |
---|
230 | setnewTarget(30,33,35,42); |
---|
231 | } |
---|
232 | else if(findpos(actuelposition,possibleposition[35])){ |
---|
233 | setnewTarget(34,36,41); |
---|
234 | } |
---|
235 | else if(findpos(actuelposition,possibleposition[36])){ |
---|
236 | setnewTarget(28,35); |
---|
237 | } |
---|
238 | else if(findpos(actuelposition,possibleposition[37])){ |
---|
239 | setnewTarget(27,38); |
---|
240 | } |
---|
241 | else if(findpos(actuelposition,possibleposition[38])){ |
---|
242 | setnewTarget(9,37,39); |
---|
243 | } |
---|
244 | else if(findpos(actuelposition,possibleposition[39])){ |
---|
245 | setnewTarget(38,40,45); |
---|
246 | } |
---|
247 | else if(findpos(actuelposition,possibleposition[40])){ |
---|
248 | setnewTarget(39,41); //Shouldn't be able to return in center |
---|
249 | } |
---|
250 | else if(findpos(actuelposition,possibleposition[41])){ |
---|
251 | setnewTarget(35,43); |
---|
252 | } |
---|
253 | else if(findpos(actuelposition,possibleposition[42])){ |
---|
254 | setnewTarget(34,43,54); |
---|
255 | } |
---|
256 | else if(findpos(actuelposition,possibleposition[43])){ |
---|
257 | setnewTarget(41,46); |
---|
258 | } |
---|
259 | else if(findpos(actuelposition,possibleposition[44])){ |
---|
260 | setnewTarget(40,66); |
---|
261 | } |
---|
262 | else if(findpos(actuelposition,possibleposition[45])){ |
---|
263 | setnewTarget(10,39,49); |
---|
264 | } |
---|
265 | else if(findpos(actuelposition,possibleposition[46])){ |
---|
266 | setnewTarget(43,47); |
---|
267 | } |
---|
268 | else if(findpos(actuelposition,possibleposition[47])){ |
---|
269 | setnewTarget(46,52,66); |
---|
270 | } |
---|
271 | else if(findpos(actuelposition,possibleposition[48])){ |
---|
272 | setnewTarget(49,51,66); |
---|
273 | } |
---|
274 | else if(findpos(actuelposition,possibleposition[49])){ |
---|
275 | setnewTarget(45,48); |
---|
276 | } |
---|
277 | else if(findpos(actuelposition,possibleposition[50])){ |
---|
278 | setnewTarget(51,61); |
---|
279 | } |
---|
280 | else if(findpos(actuelposition,possibleposition[51])){ |
---|
281 | setnewTarget(48,50); |
---|
282 | } |
---|
283 | else if(findpos(actuelposition,possibleposition[52])){ |
---|
284 | setnewTarget(47,53); |
---|
285 | } |
---|
286 | else if(findpos(actuelposition,possibleposition[53])){ |
---|
287 | setnewTarget(52,58); |
---|
288 | } |
---|
289 | else if(findpos(actuelposition,possibleposition[54])){ |
---|
290 | setnewTarget(42,55,57); |
---|
291 | } |
---|
292 | else if(findpos(actuelposition,possibleposition[55])){ |
---|
293 | setnewTarget(54,56); |
---|
294 | } |
---|
295 | else if(findpos(actuelposition,possibleposition[56])){ |
---|
296 | setnewTarget(55,57,65); |
---|
297 | } |
---|
298 | else if(findpos(actuelposition,possibleposition[57])){ |
---|
299 | setnewTarget(54,56,58,64); |
---|
300 | } |
---|
301 | else if(findpos(actuelposition,possibleposition[58])){ |
---|
302 | setnewTarget(53,57,59); |
---|
303 | } |
---|
304 | else if(findpos(actuelposition,possibleposition[59])){ |
---|
305 | setnewTarget(58,59,63); |
---|
306 | } |
---|
307 | else if(findpos(actuelposition,possibleposition[60])){ |
---|
308 | setnewTarget(59,61,62); |
---|
309 | } |
---|
310 | else if(findpos(actuelposition,possibleposition[61])){ |
---|
311 | setnewTarget(13,50,60); |
---|
312 | } |
---|
313 | else if(findpos(actuelposition,possibleposition[62])){ |
---|
314 | setnewTarget(16,60); |
---|
315 | } |
---|
316 | else if(findpos(actuelposition,possibleposition[63])){ |
---|
317 | setnewTarget(59,64); |
---|
318 | } |
---|
319 | else if(findpos(actuelposition,possibleposition[64])){ |
---|
320 | setnewTarget(57,63,65); |
---|
321 | } |
---|
322 | else if(findpos(actuelposition,possibleposition[65])){ |
---|
323 | setnewTarget(56,64); |
---|
324 | } |
---|
325 | else if(findpos(actuelposition,possibleposition[66])){ |
---|
326 | setnewTarget(47,48); |
---|
327 | } |
---|
328 | |
---|
329 | else{ |
---|
330 | this->resetGhost(); //Shouldn't happen... |
---|
331 | } //End of Position table |
---|
332 | } |
---|
333 | |
---|
334 | } |
---|
335 | |
---|
336 | void PacmanGhost::setnewTarget(int firstdec){ |
---|
337 | |
---|
338 | decision = rand()%1; |
---|
339 | switch(decision){ |
---|
340 | case 0: |
---|
341 | this->target_x = possibleposition[firstdec].x; |
---|
342 | this->target_z = possibleposition[firstdec].z; |
---|
343 | this->ismoving = true; |
---|
344 | break; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | void PacmanGhost::setnewTarget(int firstdec, int seconddec){ |
---|
349 | decision = rand()%2; |
---|
350 | switch(decision){ |
---|
351 | case 0: |
---|
352 | this->target_x = possibleposition[firstdec].x; |
---|
353 | this->target_z = possibleposition[firstdec].z; |
---|
354 | this->ismoving = true; |
---|
355 | break; |
---|
356 | case 1: |
---|
357 | this->target_x = possibleposition[seconddec].x; |
---|
358 | this->target_z = possibleposition[seconddec].z; |
---|
359 | this->ismoving = true; |
---|
360 | break; |
---|
361 | } |
---|
362 | |
---|
363 | } |
---|
364 | |
---|
365 | void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec){ |
---|
366 | |
---|
367 | decision = rand()%3; |
---|
368 | switch(decision){ |
---|
369 | case 0: |
---|
370 | this->target_x = possibleposition[firstdec].x; |
---|
371 | this->target_z = possibleposition[firstdec].z; |
---|
372 | this->ismoving = true; |
---|
373 | break; |
---|
374 | case 1: |
---|
375 | this->target_x = possibleposition[seconddec].x; |
---|
376 | this->target_z = possibleposition[seconddec].z; |
---|
377 | this->ismoving = true; |
---|
378 | break; |
---|
379 | case 2: |
---|
380 | this->target_x = possibleposition[thirddec].x; |
---|
381 | this->target_z = possibleposition[thirddec].z; |
---|
382 | this->ismoving = true; |
---|
383 | break; |
---|
384 | } |
---|
385 | |
---|
386 | } |
---|
387 | |
---|
388 | void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec, int fourthdec){ |
---|
389 | |
---|
390 | decision = rand()%4; |
---|
391 | switch(decision){ |
---|
392 | case 0: |
---|
393 | this->target_x = possibleposition[firstdec].x; |
---|
394 | this->target_z = possibleposition[firstdec].z; |
---|
395 | this->ismoving = true; |
---|
396 | break; |
---|
397 | case 1: |
---|
398 | this->target_x = possibleposition[seconddec].x; |
---|
399 | this->target_z = possibleposition[seconddec].z; |
---|
400 | this->ismoving = true; |
---|
401 | break; |
---|
402 | case 2: |
---|
403 | this->target_x = possibleposition[thirddec].x; |
---|
404 | this->target_z = possibleposition[thirddec].z; |
---|
405 | this->ismoving = true; |
---|
406 | break; |
---|
407 | case 3: |
---|
408 | this->target_x = possibleposition[fourthdec].x; |
---|
409 | this->target_z = possibleposition[fourthdec].z; |
---|
410 | this->ismoving = true; |
---|
411 | break; |
---|
412 | } |
---|
413 | |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ |
---|
419 | if(!dontmove) |
---|
420 | this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt)); |
---|
421 | } |
---|
422 | |
---|
423 | bool PacmanGhost::findpos(Vector3 one, Vector3 other){ |
---|
424 | if((abs(one.x - other.x)<0.5) && (abs(one.z - other.z)<0.5)) return true; |
---|
425 | return false; |
---|
426 | } |
---|
427 | |
---|
428 | void changemovability(){ |
---|
429 | if(dontmove){ |
---|
430 | dontmove = false;} |
---|
431 | else{ |
---|
432 | dontmove = true; |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | void PacmanGhost::resetGhost(){ |
---|
437 | |
---|
438 | this->setPosition(this->resetposition); |
---|
439 | this->ismoving = false; |
---|
440 | this->actuelposition = this->getPosition(); |
---|
441 | |
---|
442 | this->target_x = actuelposition.x; |
---|
443 | this->target_z = actuelposition.z; |
---|
444 | |
---|
445 | } |
---|
446 | } |
---|