Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

distinguish the different Ghost when the Ghost is not moving

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