Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS18/src/modules/pacman/PacmanGhost.cc @ 11976

Last change on this file since 11976 was 11976, checked in by dreherm, 6 years ago

HUD erweitert

  • Property svn:executable set to *
File size: 17.6 KB
Line 
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
34namespace 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        if(findpos(actuelposition, Vector3(0,-20,0)))
55            dontmove = true;
56       
57        this->target_x = actuelposition.x;
58        this->target_z = actuelposition.z; 
59
60    }
61
62    /**
63    @brief
64        Destructor. Destroys controller, if present.
65    */
66    PacmanGhost::~PacmanGhost()
67    {
68        // Deletes the controller if the object was initialized and the pointer to the controller is not NULL.
69    }
70
71    /**
72    @brief
73        Method for creating a AutonomousDrone through XML.
74    */
75    void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(PacmanGhost, XMLPort, xmlelement, mode);
78    }
79
80
81
82    Vector3 possibleposition[] = {Vector3(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4
83        Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9
84        Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14
85        Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19
86        Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24
87        Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29
88        Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34
89        Vector3(-70,10,105),Vector3(-20,10,105),Vector3(20,10,105),Vector3(70,10,105),Vector3(70,10,60), //35-39
90        Vector3(0,10,60),Vector3(-70,10,60),Vector3(-135,10,15),Vector3(-70,10,60),Vector3(0,10,15), //40-44
91        Vector3(70,10,15),Vector3(-70,10,-35),Vector3(-20,10,-35),Vector3(20,10,-35),Vector3(70,10,-35), //45-49
92        Vector3(70,10,-85),Vector3(20,10,-85),Vector3(-20,10,-85),Vector3(-70,10,-85),Vector3(-135,10,-85), //50-54
93        Vector3(-215,10,-85),Vector3(-215,10,-135),Vector3(-135,10,-135),Vector3(-70,10,-135),Vector3(-20,10,-135), //55-59
94        Vector3(20,10,-135),Vector3(70,10,-135),Vector3(20,10,-195),Vector3(-20,10,-195),Vector3(-135,10,-195), //60-64
95        Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66
96
97    /**
98    @brief
99        Defines which actions the AutonomousDrone has to take in each tick.
100    @param dt
101        The length of the tick.
102    */
103    void PacmanGhost::tick(float dt)
104    {
105        SUPER(PacmanGhost, tick, dt);
106
107        //setorientation
108
109        this->actuelposition = this->getPosition();
110       
111        //Stop, if target arrived
112        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
113                 this->ismoving = false;
114        }
115
116        //Move, if ghost hasn't arrived yet
117        if(this->ismoving){
118            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
119                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)*2);
120                move(dt, actuelposition, velocity);
121            }   
122            if(!(abs(this->actuelposition.x-target_x)<0.5)){
123                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x)*2,0,0);
124                move(dt, actuelposition, velocity);
125            }
126        }
127        //Check on which position ghost has arrived and set new target
128         else{
129            while(lockmove){};
130            lockmove = true;
131
132            if(findpos(actuelposition,possibleposition[0])){
133                setnewTarget(1,17,19);
134            }
135            else if(findpos(actuelposition,possibleposition[1])){
136                setnewTarget(0,2);
137            }
138            else if(findpos(actuelposition,possibleposition[2])){
139                        setnewTarget(1,3);
140            }
141            else if(findpos(actuelposition,possibleposition[3])){
142                            setnewTarget(2,4,5);
143            }
144            else if(findpos(actuelposition,possibleposition[4])){
145                                setnewTarget(3,6);
146            }
147            else if(findpos(actuelposition,possibleposition[5])){
148                setnewTarget(3,7);
149            }
150            else if(findpos(actuelposition,possibleposition[6])){
151                setnewTarget(4,9,26);
152            }
153            else if(findpos(actuelposition,possibleposition[7])){
154                setnewTarget(5,8);
155            }
156            else if(findpos(actuelposition,possibleposition[8])){
157                setnewTarget(7,9);
158            }
159            else if(findpos(actuelposition,possibleposition[9])){
160                setnewTarget(6,8,10,38);
161            }
162            else if(findpos(actuelposition,possibleposition[10])){
163                setnewTarget(9,11,45);
164            }
165            else if(findpos(actuelposition,possibleposition[11])){
166                setnewTarget(10,12,13);
167            }
168            else if(findpos(actuelposition,possibleposition[12])){
169                setnewTarget(11,14);
170            }
171            else if(findpos(actuelposition,possibleposition[13])){
172                setnewTarget(11,14,16,61);
173            }
174            else if(findpos(actuelposition,possibleposition[14])){
175                setnewTarget(12,13,15);
176            }
177            else if(findpos(actuelposition,possibleposition[15])){
178                setnewTarget(14,16);
179            }
180            else if(findpos(actuelposition,possibleposition[16])){
181                setnewTarget(13,15,62);
182            }
183            else if(findpos(actuelposition,possibleposition[17])){
184                setnewTarget(0,25);
185            }
186            else if(findpos(actuelposition,possibleposition[18])){
187                setnewTarget(19,24);
188            }
189            else if(findpos(actuelposition,possibleposition[19])){
190                setnewTarget(0,18,20);
191            }
192            else if(findpos(actuelposition,possibleposition[20])){
193                setnewTarget(19,21);
194            }
195            else if(findpos(actuelposition,possibleposition[21])){
196                setnewTarget(20,22);
197            }
198            else if(findpos(actuelposition,possibleposition[22])){
199                setnewTarget(21,23,31);
200            }
201            else if(findpos(actuelposition,possibleposition[23])){
202                setnewTarget(22,30);
203            }
204            else if(findpos(actuelposition,possibleposition[24])){
205                setnewTarget(18,29);
206            }
207            else if(findpos(actuelposition,possibleposition[25])){
208                setnewTarget(17,26);
209            }
210            else if(findpos(actuelposition,possibleposition[26])){
211                setnewTarget(6,25,27);
212            }
213            else if(findpos(actuelposition,possibleposition[27])){
214                setnewTarget(26,28,37);
215            }
216            else if(findpos(actuelposition,possibleposition[28])){
217                setnewTarget(27,29,36);
218            }
219            else if(findpos(actuelposition,possibleposition[29])){
220                setnewTarget(24,28,30);
221            }
222            else if(findpos(actuelposition,possibleposition[30])){
223                setnewTarget(23,29,34);
224            }
225            else if(findpos(actuelposition,possibleposition[31])){
226                setnewTarget(22,32);
227            }
228            else if(findpos(actuelposition,possibleposition[32])){
229                setnewTarget(31,33);
230            }
231            else if(findpos(actuelposition,possibleposition[33])){
232                setnewTarget(32,34);
233            }
234            else if(findpos(actuelposition,possibleposition[34])){
235                setnewTarget(30,33,35,42);
236            }
237            else if(findpos(actuelposition,possibleposition[35])){
238                setnewTarget(34,36,41);
239            }
240            else if(findpos(actuelposition,possibleposition[36])){
241                setnewTarget(28,35);
242            }
243            else if(findpos(actuelposition,possibleposition[37])){
244                setnewTarget(27,38);
245            }
246            else if(findpos(actuelposition,possibleposition[38])){
247                setnewTarget(9,37,39);
248            }
249            else if(findpos(actuelposition,possibleposition[39])){
250                setnewTarget(38,40,45);
251            }
252            else if(findpos(actuelposition,possibleposition[40])){
253                setnewTarget(39,41); //Shouldn't be able to return in center
254            }
255            else if(findpos(actuelposition,possibleposition[41])){
256                setnewTarget(35,43);
257            }
258            else if(findpos(actuelposition,possibleposition[42])){
259                setnewTarget(34,43,54);
260            }
261            else if(findpos(actuelposition,possibleposition[43])){
262                setnewTarget(41,46);
263            }
264            else if(findpos(actuelposition,possibleposition[44])){
265                setnewTarget(40,66);
266            }
267            else if(findpos(actuelposition,possibleposition[45])){
268                setnewTarget(10,39,49);
269            }
270            else if(findpos(actuelposition,possibleposition[46])){
271                setnewTarget(43,47);
272            }
273            else if(findpos(actuelposition,possibleposition[47])){
274                setnewTarget(46,52,66);
275            }
276            else if(findpos(actuelposition,possibleposition[48])){
277                setnewTarget(49,51,66);
278            }
279            else if(findpos(actuelposition,possibleposition[49])){
280                setnewTarget(45,48);
281            }
282            else if(findpos(actuelposition,possibleposition[50])){
283                setnewTarget(51,61);
284            }
285            else if(findpos(actuelposition,possibleposition[51])){
286                setnewTarget(48,50);
287            }
288            else if(findpos(actuelposition,possibleposition[52])){
289                setnewTarget(47,53);
290            }
291            else if(findpos(actuelposition,possibleposition[53])){
292                setnewTarget(52,58);
293            }
294            else if(findpos(actuelposition,possibleposition[54])){
295                setnewTarget(42,55,57);
296            }
297            else if(findpos(actuelposition,possibleposition[55])){
298                setnewTarget(54,56);
299            }
300            else if(findpos(actuelposition,possibleposition[56])){
301                setnewTarget(55,57,65);
302            }
303            else if(findpos(actuelposition,possibleposition[57])){
304                setnewTarget(54,56,58,64);
305            }
306            else if(findpos(actuelposition,possibleposition[58])){
307                setnewTarget(53,57,59);
308            }
309            else if(findpos(actuelposition,possibleposition[59])){
310                setnewTarget(58,59,63);
311            }
312            else if(findpos(actuelposition,possibleposition[60])){
313                setnewTarget(59,61,62);
314            }
315            else if(findpos(actuelposition,possibleposition[61])){
316                setnewTarget(13,50,60);
317            }
318            else if(findpos(actuelposition,possibleposition[62])){
319                setnewTarget(16,60);
320            }
321            else if(findpos(actuelposition,possibleposition[63])){
322                setnewTarget(59,64);
323            }
324            else if(findpos(actuelposition,possibleposition[64])){
325                setnewTarget(57,63,65);
326            }
327            else if(findpos(actuelposition,possibleposition[65])){
328                setnewTarget(56,64);
329            }
330            else if(findpos(actuelposition,possibleposition[66])){
331                setnewTarget(47,48);
332            }
333
334            else{
335                this->resetGhost(); //Shouldn't happen...
336            } //End of Position table
337                lockmove = false;
338            }
339       
340    }
341
342    void PacmanGhost::setnewTarget(int firstdec){
343       
344          decision = rand()%1;
345            switch(decision){
346                case 0:
347                    this->target_x = possibleposition[firstdec].x;
348                    this->target_z = possibleposition[firstdec].z; 
349                    this->ismoving = true;
350                    break;
351                }
352    }
353
354    void PacmanGhost::setnewTarget(int firstdec, int seconddec){ 
355           decision = rand()%2;
356            switch(decision){
357                case 0:
358                    this->target_x = possibleposition[firstdec].x;
359                    this->target_z = possibleposition[firstdec].z; 
360                    this->ismoving = true;
361                    break;
362                case 1:
363                    this->target_x = possibleposition[seconddec].x;
364                    this->target_z = possibleposition[seconddec].z; 
365                    this->ismoving = true;
366                    break; 
367            }
368           
369    }
370
371    void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec){
372       
373           decision = rand()%3;
374            switch(decision){
375                case 0:
376                    this->target_x = possibleposition[firstdec].x;
377                    this->target_z = possibleposition[firstdec].z; 
378                    this->ismoving = true;
379                    break;
380                case 1:
381                    this->target_x = possibleposition[seconddec].x;
382                    this->target_z = possibleposition[seconddec].z; 
383                    this->ismoving = true;
384                    break;
385                case 2:
386                    this->target_x = possibleposition[thirddec].x;
387                    this->target_z = possibleposition[thirddec].z; 
388                    this->ismoving = true;
389                    break;   
390                }
391           
392        }
393
394    void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec, int fourthdec){
395       
396           decision = rand()%4;
397            switch(decision){
398                case 0:
399                    this->target_x = possibleposition[firstdec].x;
400                    this->target_z = possibleposition[firstdec].z; 
401                    this->ismoving = true;
402                    break;
403                case 1:
404                    this->target_x = possibleposition[seconddec].x;
405                    this->target_z = possibleposition[seconddec].z; 
406                    this->ismoving = true;
407                    break;
408                case 2:
409                    this->target_x = possibleposition[thirddec].x;
410                    this->target_z = possibleposition[thirddec].z; 
411                    this->ismoving = true;
412                    break;
413                case 3:
414                        this->target_x = possibleposition[fourthdec].x;
415                    this->target_z = possibleposition[fourthdec].z; 
416                    this->ismoving = true;
417                    break;   
418                }
419           
420        }
421
422    void PacmanGhost::changewith(PacmanGhost* otherghost){
423
424        while(lockmove){};
425        lockmove = true;
426
427        otherghost->setPosition(this->getPosition());
428        this->setPosition(0,-20,0);
429        otherghost->target_x = this->target_x;   //Probleme bei parallelen Porzessen
430        otherghost->target_z = this->target_z;
431        otherghost->ismoving = this->ismoving;
432
433        this->dontmove = true;
434        otherghost->dontmove = false;
435
436        lockmove = false;
437    }
438
439
440    void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){
441        if(!dontmove){
442            this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt));
443           
444
445            if(abs(velocity.x) == 2){
446                this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0)));
447            }
448            else if(velocity.z == 2){
449                this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0)));
450            }
451            else if(velocity.z == -2){
452                this->setOrientation(Quaternion(Radian(180), Vector3(0, 1, 0)));
453            }
454            else{}
455
456     }
457    }
458
459    bool PacmanGhost::findpos(Vector3 one, Vector3 other){
460       if((abs(one.x - other.x)<0.5) && (abs(one.y - other.y)<0.5) && (abs(one.z - other.z)<0.5)) return true;
461        return false;
462    }
463
464    void PacmanGhost::changemovability(){
465        if(dontmove){
466         dontmove = false;}
467        else{
468         dontmove = true;   
469        }
470    }
471
472    void PacmanGhost::resetGhost(){
473   
474        this->setPosition(this->resetposition);
475        this->ismoving = false;
476        this->actuelposition = this->getPosition();
477       
478        this->target_x = actuelposition.x;
479        this->target_z = actuelposition.z;
480   
481    }
482
483    void PacmanGhost::levelupvelo(){
484        velocity.x = velocity.x + 2;
485        velocity.y = velocity.y + 2;
486        velocity.z = velocity.z + 2;
487    }
488}
Note: See TracBrowser for help on using the repository browser.