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