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 | * Oli Scheuss |
---|
24 | * Co-authors: |
---|
25 | * Damian 'Mozork' Frick |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "PacmanGhost.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
33 | |
---|
34 | namespace 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 | |
---|
50 | this->setCollisionType(CollisionType::Dynamic); |
---|
51 | |
---|
52 | this->resetposition = Vector3(0,20,245); //Set Default start position |
---|
53 | |
---|
54 | this->actuelposition = this->getPosition(); |
---|
55 | |
---|
56 | this->target_x = actuelposition.x; |
---|
57 | this->target_z = actuelposition.z; |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | @brief |
---|
63 | Destructor. Destroys controller, if present. |
---|
64 | */ |
---|
65 | PacmanGhost::~PacmanGhost() |
---|
66 | { |
---|
67 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | @brief |
---|
72 | Method for creating a AutonomousDrone through XML. |
---|
73 | */ |
---|
74 | void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
75 | { |
---|
76 | SUPER(PacmanGhost, XMLPort, xmlelement, mode); |
---|
77 | |
---|
78 | XMLPortParam(PacmanGhost, "resetposition", setResetPosition, getResetPosition, xmlelement, mode); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | Vector3 possibleposition[] = {Vector3(175,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), |
---|
84 | Vector3(185,10,150),Vector3(135,10,145),Vector3(215,10,150)}; |
---|
85 | |
---|
86 | /** |
---|
87 | @brief |
---|
88 | Defines which actions the AutonomousDrone has to take in each tick. |
---|
89 | @param dt |
---|
90 | The length of the tick. |
---|
91 | */ |
---|
92 | void PacmanGhost::tick(float dt) |
---|
93 | { |
---|
94 | SUPER(PacmanGhost, tick, dt); |
---|
95 | |
---|
96 | //setorientation |
---|
97 | |
---|
98 | this->actuelposition = this->getPosition(); |
---|
99 | |
---|
100 | //Stop, if target arrived |
---|
101 | if((abs(this->actuelposition.x - this->target_x)<0.1) && (abs(this->actuelposition.z - this->target_z)<0.1)){ |
---|
102 | this->ismoving = false; |
---|
103 | } |
---|
104 | |
---|
105 | //Move, if ghost hasn't arrived yet |
---|
106 | if(this->ismoving){ |
---|
107 | if(!(abs(this->actuelposition.z-target_z)<0.1)) { |
---|
108 | velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); |
---|
109 | move(dt, actuelposition, velocity); |
---|
110 | } |
---|
111 | if(!(abs(this->actuelposition.x-target_x)<0.1)){ |
---|
112 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); |
---|
113 | move(dt, actuelposition, velocity); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | //Check on which position ghost has arrived and set new target |
---|
118 | else{ |
---|
119 | if(findpos(actuelposition,possibleposition[0])){ |
---|
120 | decision = rand()%1; |
---|
121 | switch(decision){ |
---|
122 | case 0: |
---|
123 | this->target_x = possibleposition[1].x; |
---|
124 | this->target_z = possibleposition[1].z; |
---|
125 | this->ismoving = true; |
---|
126 | break; |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|
130 | else if(findpos(actuelposition,possibleposition[1])){ |
---|
131 | decision = rand()%2; |
---|
132 | switch(decision){ |
---|
133 | case 0: |
---|
134 | this->target_x = possibleposition[0].x; |
---|
135 | this->target_z = possibleposition[0].z; |
---|
136 | this->ismoving = true; |
---|
137 | break; |
---|
138 | case 1: |
---|
139 | this->target_x = possibleposition[2].x; |
---|
140 | this->target_z = possibleposition[2].z; |
---|
141 | this->ismoving = true; |
---|
142 | break; |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | else if(findpos(actuelposition,possibleposition[2])){ |
---|
147 | decision = rand()%2; |
---|
148 | switch(decision){ |
---|
149 | case 0: |
---|
150 | this->target_x = possibleposition[1].x; |
---|
151 | this->target_z = possibleposition[1].z; |
---|
152 | this->ismoving = true; |
---|
153 | break; |
---|
154 | case 1: |
---|
155 | this->target_x = possibleposition[3].x; |
---|
156 | this->target_z = possibleposition[3].z; |
---|
157 | this->ismoving = true; |
---|
158 | break; |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | else if(findpos(actuelposition,possibleposition[3])){ |
---|
164 | decision = rand()%3; |
---|
165 | switch(decision){ |
---|
166 | case 0: |
---|
167 | this->target_x = possibleposition[2].x; |
---|
168 | this->target_z = possibleposition[2].z; |
---|
169 | this->ismoving = true; |
---|
170 | break; |
---|
171 | case 1: |
---|
172 | this->target_x = possibleposition[4].x; |
---|
173 | this->target_z = possibleposition[4].z; |
---|
174 | this->ismoving = true; |
---|
175 | break; |
---|
176 | case 2: |
---|
177 | this->target_x = possibleposition[5].x; |
---|
178 | this->target_z = possibleposition[5].z; |
---|
179 | this->ismoving = true; |
---|
180 | break; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | else if(findpos(actuelposition,possibleposition[4])){ |
---|
185 | decision = rand()%2; |
---|
186 | switch(decision){ |
---|
187 | case 0: |
---|
188 | this->target_x = possibleposition[3].x; |
---|
189 | this->target_z = possibleposition[3].z; |
---|
190 | this->ismoving = true; |
---|
191 | break; |
---|
192 | case 1: |
---|
193 | this->target_x = possibleposition[6].x; |
---|
194 | this->target_z = possibleposition[6].z; |
---|
195 | this->ismoving = true; |
---|
196 | break; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | else if(findpos(actuelposition,possibleposition[5])){ |
---|
201 | decision = rand()%2; |
---|
202 | switch(decision){ |
---|
203 | case 0: |
---|
204 | this->target_x = possibleposition[3].x; |
---|
205 | this->target_z = possibleposition[3].z; |
---|
206 | this->ismoving = true; |
---|
207 | break; |
---|
208 | case 1: |
---|
209 | this->target_x = possibleposition[7].x; |
---|
210 | this->target_z = possibleposition[7].z; |
---|
211 | this->ismoving = true; |
---|
212 | break; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | else if(findpos(actuelposition,possibleposition[6])){ |
---|
217 | decision = rand()%1; |
---|
218 | switch(decision){ |
---|
219 | case 0: |
---|
220 | this->target_x = possibleposition[4].x; |
---|
221 | this->target_z = possibleposition[4].z; |
---|
222 | this->ismoving = true; |
---|
223 | break; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | else if(findpos(actuelposition,possibleposition[7])){ |
---|
228 | decision = rand()%1; |
---|
229 | switch(decision){ |
---|
230 | case 0: |
---|
231 | this->target_x = possibleposition[5].x; |
---|
232 | this->target_z = possibleposition[5].z; |
---|
233 | this->ismoving = true; |
---|
234 | break; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | else{ |
---|
240 | } //End of Position table |
---|
241 | } |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ |
---|
247 | this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt)); |
---|
248 | } |
---|
249 | |
---|
250 | bool PacmanGhost::findpos(Vector3 one, Vector3 other){ |
---|
251 | if((abs(one.x - other.x)<0.1) && (abs(one.z - other.z)<0.1)) return true; |
---|
252 | return false; |
---|
253 | } |
---|
254 | |
---|
255 | void PacmanGhost::resetGhost(){ |
---|
256 | this->setPosition(resetposition); |
---|
257 | } |
---|
258 | } |
---|