Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS19/src/modules/pacman/PacmanCyan.cc @ 12357

Last change on this file since 12357 was 12357, checked in by peterf, 5 years ago

Improved formatting and added some comments to make the code more precise

File size: 4.3 KB
RevLine 
[12320]1#include "PacmanCyan.h"
2//#include "Pacman.h"
3
4#include "core/CoreIncludes.h"
5#include "BulletDynamics/Dynamics/btRigidBody.h"
6
7        namespace orxonox{
8
9                RegisterClass(PacmanCyan);
10
11                PacmanCyan::PacmanCyan(Context* context) : PacmanGhost(context){
12
13        RegisterObject(PacmanCyan);
14        this->target_x=0;
15        this->target_z=15;
[12322]16        this->setPosition(Vector3(0,10,15));
[12320]17        this->lastPlayerPassedPoint=Vector3(70,10,-135);
[12321]18
19        this->isPatrolling = false;
[12320]20         
21    }
22
23
24    /**
25    @brief
26        Method for creating a ghost through XML.
27    */
28    void PacmanCyan::XMLPort(Element& xmlelement, XMLPort::Mode mode)
29    {
30        SUPER(PacmanCyan, XMLPort, xmlelement, mode);
31    }
32
33
34
35    void PacmanCyan::tick(float dt)
36    {
37       
38        SUPER(PacmanGhost, tick, dt);
39        this->actuelposition = this->getPosition();
40
41        //Stop, if target arrived
42        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
43           
44                 this->ismoving = false;
45        }
46
47        //Move, if ghost hasn't arrived yet
[12357]48        if(this->ismoving){     
[12320]49            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
50                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z));
51                move(dt, actuelposition, velocity);
52            }   
53            if(!(abs(this->actuelposition.x-target_x)<0.5)){
54                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0);
55                move(dt, actuelposition, velocity);
[12357]56            }   
[12320]57        }
58
59        //Check on which position the ghost has arrived and set new target
60        else{
61            while(lockmove){};
62            lockmove = true;
63
[12321]64            Vector3 cyanPos=Vector3(this->target_x, 10, this->target_z);
[12320]65               
[12321]66            if(this->isPatrolling==false){
67                //we are not patrolling anymore, choose new patrol
[12320]68
[12321]69
70                this->nextPatrol();
71
72            }
[12357]73            else if(this->passedByStart==false){
[12322]74                //we have not even reached our startPatrol point
[12321]75
[12322]76                if(findpos(cyanPos, startPatrol)){
77                    this->passedByStart=true;
78                }
79                else{
80                nextMove(cyanPos, startPatrol);
81                }
82            }
[12357]83            else if(this->passedByGoal==false){
[12322]84                //we have reached our startPatrol point, now we go to goalPoint
[12321]85
[12322]86                if(findpos(cyanPos, goalPatrol)){
87                    this->passedByGoal=true;
88                }
89                else{
90                nextMove(cyanPos, goalPatrol);
91                }
[12321]92
93            }
[12322]94            else if(!findpos(cyanPos, this->startPatrol)){
95                //we reached our goal, now we return to start
[12321]96
[12322]97                nextMove(cyanPos, startPatrol);
98
[12321]99            }
[12357]100            else {
[12322]101                //we reached startPoint again. Either we change patrol
102                //or redo same patrol
[12321]103
[12322]104                int redoORNot = rand()%2;
105
106                if(redoORNot==1){
107
108                this->isPatrolling=false;
109                //we will change patrol region
110                    }
111                else {
112                this->passedByGoal=false;
113                //repeat patrol region
114                    }
[12321]115            }
116
[12320]117            lockmove=false; //NEVER FORGET THIS ONE !!!!!!!
118        }
119       
120    }
121
[12321]122    void PacmanCyan::nextPatrol(){
[12320]123
[12322]124                int indexRand1=rand()%67;
[12321]125                while(indexRand1==44){
126                    //new try if index is that of the position in the middle
127                    indexRand1=rand()%67;
128                }
129
130                int indexRand2=rand()%67;
131                while((indexRand2==44)||(indexRand2==indexRand1)){
132                    //new try if 2nd index is same as first one or is index
133                    // of position in the middle
134
135                    indexRand2=rand()%67;
136                }
137
138              this->startPatrol =  possibleposition[indexRand1];
139              this->goalPatrol = possibleposition[indexRand2];
140
[12322]141              this->passedByStart=false;
142              this->passedByGoal=false;
143              this->isPatrolling=true;
144
[12321]145    }
146
147
148
149
[12320]150        void PacmanCyan::nextMove( Vector3 cyanPosP, Vector3 playerPos){
151
[12322]152           Vector3 nextTarget = getShortestPath(cyanPosP, playerPos);
[12320]153       
154           setNewTargetGhost(nextTarget);
155        }
156
157
158}
Note: See TracBrowser for help on using the repository browser.