Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS19/src/modules/pacman/PacmanGhost.cc @ 12252

Last change on this file since 12252 was 12252, checked in by rueegseb, 5 years ago

Added a feature to differentiate the seperate ghosts

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