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; |
---|
16 | this->setPosition(Vector3(0,10,15)); |
---|
17 | this->lastPlayerPassedPoint=Vector3(70,10,-135); |
---|
18 | |
---|
19 | this->isPatrolling = false; |
---|
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 |
---|
48 | if(this->ismoving){ |
---|
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); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | //Check on which position the ghost has arrived and set new target |
---|
60 | else{ |
---|
61 | while(lockmove){}; |
---|
62 | lockmove = true; |
---|
63 | |
---|
64 | Vector3 cyanPos=Vector3(this->target_x, 10, this->target_z); |
---|
65 | |
---|
66 | if(this->isPatrolling==false){ |
---|
67 | //we are not patrolling anymore, choose new patrol |
---|
68 | |
---|
69 | |
---|
70 | this->nextPatrol(); |
---|
71 | |
---|
72 | } |
---|
73 | else if(this->passedByStart==false){ |
---|
74 | //we have not even reached our startPatrol point |
---|
75 | |
---|
76 | if(findpos(cyanPos, startPatrol)){ |
---|
77 | this->passedByStart=true; |
---|
78 | } |
---|
79 | else{ |
---|
80 | nextMove(cyanPos, startPatrol); |
---|
81 | } |
---|
82 | } |
---|
83 | else if(this->passedByGoal==false){ |
---|
84 | //we have reached our startPatrol point, now we go to goalPoint |
---|
85 | |
---|
86 | if(findpos(cyanPos, goalPatrol)){ |
---|
87 | this->passedByGoal=true; |
---|
88 | } |
---|
89 | else{ |
---|
90 | nextMove(cyanPos, goalPatrol); |
---|
91 | } |
---|
92 | |
---|
93 | } |
---|
94 | else if(!findpos(cyanPos, this->startPatrol)){ |
---|
95 | //we reached our goal, now we return to start |
---|
96 | |
---|
97 | nextMove(cyanPos, startPatrol); |
---|
98 | |
---|
99 | } |
---|
100 | else { |
---|
101 | //we reached startPoint again. Either we change patrol |
---|
102 | //or redo same patrol |
---|
103 | |
---|
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 | } |
---|
115 | } |
---|
116 | |
---|
117 | lockmove=false; //NEVER FORGET THIS ONE !!!!!!! |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | void PacmanCyan::nextPatrol(){ |
---|
123 | |
---|
124 | int indexRand1=rand()%67; |
---|
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 | |
---|
141 | this->passedByStart=false; |
---|
142 | this->passedByGoal=false; |
---|
143 | this->isPatrolling=true; |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | void PacmanCyan::nextMove( Vector3 cyanPosP, Vector3 playerPos){ |
---|
151 | |
---|
152 | Vector3 nextTarget = getShortestPath(cyanPosP, playerPos); |
---|
153 | |
---|
154 | setNewTargetGhost(nextTarget); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | } |
---|