Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS18/src/modules/pacman/PacmanGhost.cc @ 11915

Last change on this file since 11915 was 11915, checked in by dreherm, 6 years ago

Game playable

  • Property svn:executable set to *
File size: 11.8 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 *      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
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
50        this->setCollisionType(CollisionType::Dynamic);
51
52        this->resetposition = this->getPosition(); //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(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4
84        Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9
85        Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14
86        Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19
87        Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24
88        Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29
89        Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105) //30-34
90    };
91
92    /**
93    @brief
94        Defines which actions the AutonomousDrone has to take in each tick.
95    @param dt
96        The length of the tick.
97    */
98    void PacmanGhost::tick(float dt)
99    {
100        SUPER(PacmanGhost, tick, dt);
101
102        //setorientation
103
104        this->actuelposition = this->getPosition();
105       
106        //Stop, if target arrived
107        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
108                 this->ismoving = false;
109        }
110
111        //Move, if ghost hasn't arrived yet
112        if(this->ismoving){
113            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
114                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)*2);
115                move(dt, actuelposition, velocity);
116            }   
117            if(!(abs(this->actuelposition.x-target_x)<0.5)){
118                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x)*2,0,0);
119                move(dt, actuelposition, velocity);
120            }
121        }
122
123        //Check on which position ghost has arrived and set new target
124         else{
125            if(findpos(actuelposition,possibleposition[0])){
126                setnewTarget(1,17,19);
127            }
128            else if(findpos(actuelposition,possibleposition[1])){
129                setnewTarget(0,2);
130            }
131            else if(findpos(actuelposition,possibleposition[2])){
132                        setnewTarget(1,3);
133            }
134            else if(findpos(actuelposition,possibleposition[3])){
135                            setnewTarget(2,4,5);
136            }
137            else if(findpos(actuelposition,possibleposition[4])){
138                                setnewTarget(3,6);
139            }
140            else if(findpos(actuelposition,possibleposition[5])){
141                setnewTarget(3,7);
142            }
143            else if(findpos(actuelposition,possibleposition[6])){
144                setnewTarget(4,9,26);
145            }
146            else if(findpos(actuelposition,possibleposition[7])){
147                setnewTarget(5,8);
148            }
149            else if(findpos(actuelposition,possibleposition[8])){
150                setnewTarget(7,9);
151            }
152            else if(findpos(actuelposition,possibleposition[9])){
153                setnewTarget(6,8,10); //38
154            }
155            else if(findpos(actuelposition,possibleposition[10])){
156                setnewTarget(9,11); //45,9
157            }
158            else if(findpos(actuelposition,possibleposition[11])){
159                setnewTarget(10,12,13);
160            }
161            else if(findpos(actuelposition,possibleposition[12])){
162                setnewTarget(11,14);
163            }
164            else if(findpos(actuelposition,possibleposition[13])){
165                setnewTarget(11,14,16); //61
166            }
167            else if(findpos(actuelposition,possibleposition[14])){
168                setnewTarget(12,13,15);
169            }
170            else if(findpos(actuelposition,possibleposition[15])){
171                setnewTarget(14,16);
172            }
173            else if(findpos(actuelposition,possibleposition[16])){
174                setnewTarget(13,15); //62
175            }
176            else if(findpos(actuelposition,possibleposition[17])){
177                setnewTarget(0,25);
178            }
179            else if(findpos(actuelposition,possibleposition[18])){
180                setnewTarget(19,24);
181            }
182            else if(findpos(actuelposition,possibleposition[19])){
183                setnewTarget(0,18,20);
184            }
185            else if(findpos(actuelposition,possibleposition[20])){
186                setnewTarget(19,21);
187            }
188            else if(findpos(actuelposition,possibleposition[21])){
189                setnewTarget(20,22);
190            }
191            else if(findpos(actuelposition,possibleposition[22])){
192                setnewTarget(21,23,31);
193            }
194            else if(findpos(actuelposition,possibleposition[23])){
195                setnewTarget(22,30);
196            }
197            else if(findpos(actuelposition,possibleposition[24])){
198                setnewTarget(18,29);
199            }
200            else if(findpos(actuelposition,possibleposition[25])){
201                setnewTarget(17,26);
202            }
203            else if(findpos(actuelposition,possibleposition[26])){
204                setnewTarget(6,25,27);
205            }
206            else if(findpos(actuelposition,possibleposition[27])){
207                setnewTarget(26,28);//37
208            }
209            else if(findpos(actuelposition,possibleposition[28])){
210                setnewTarget(27,29); //36
211            }
212            else if(findpos(actuelposition,possibleposition[29])){
213                setnewTarget(24,28,30);
214            }
215            else if(findpos(actuelposition,possibleposition[30])){
216                setnewTarget(23,29,34);
217            }
218            else if(findpos(actuelposition,possibleposition[31])){
219                setnewTarget(22,32);
220            }
221            else if(findpos(actuelposition,possibleposition[32])){
222                setnewTarget(31,33);
223            }
224            else if(findpos(actuelposition,possibleposition[33])){
225                setnewTarget(32,34);
226            }
227            else if(findpos(actuelposition,possibleposition[34])){
228                setnewTarget(30,33);//35,42
229            }
230
231
232            else{
233            } //End of Position table
234            }
235
236    }
237
238    void PacmanGhost::setnewTarget(int firstdec){
239          decision = rand()%1;
240            switch(decision){
241                case 0:
242                    this->target_x = possibleposition[firstdec].x;
243                    this->target_z = possibleposition[firstdec].z; 
244                    this->ismoving = true;
245                    break;
246                }
247    }
248
249    void PacmanGhost::setnewTarget(int firstdec, int seconddec){
250           decision = rand()%2;
251            switch(decision){
252                case 0:
253                    this->target_x = possibleposition[firstdec].x;
254                    this->target_z = possibleposition[firstdec].z; 
255                    this->ismoving = true;
256                    break;
257                case 1:
258                    this->target_x = possibleposition[seconddec].x;
259                    this->target_z = possibleposition[seconddec].z; 
260                    this->ismoving = true;
261                    break; 
262            }
263    }
264
265    void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec){
266           decision = rand()%3;
267            switch(decision){
268                case 0:
269                    this->target_x = possibleposition[firstdec].x;
270                    this->target_z = possibleposition[firstdec].z; 
271                    this->ismoving = true;
272                    break;
273                case 1:
274                    this->target_x = possibleposition[seconddec].x;
275                    this->target_z = possibleposition[seconddec].z; 
276                    this->ismoving = true;
277                    break;
278                case 2:
279                    this->target_x = possibleposition[thirddec].x;
280                    this->target_z = possibleposition[thirddec].z; 
281                    this->ismoving = true;
282                    break;   
283                }
284        }
285
286    void PacmanGhost::setnewTarget(int firstdec, int seconddec, int thirddec, int fourthdec){
287           decision = rand()%4;
288            switch(decision){
289                case 0:
290                    this->target_x = possibleposition[firstdec].x;
291                    this->target_z = possibleposition[firstdec].z; 
292                    this->ismoving = true;
293                    break;
294                case 1:
295                    this->target_x = possibleposition[seconddec].x;
296                    this->target_z = possibleposition[seconddec].z; 
297                    this->ismoving = true;
298                    break;
299                case 2:
300                    this->target_x = possibleposition[thirddec].x;
301                    this->target_z = possibleposition[thirddec].z; 
302                    this->ismoving = true;
303                    break;
304                case 3:
305                        this->target_x = possibleposition[fourthdec].x;
306                    this->target_z = possibleposition[fourthdec].z; 
307                    this->ismoving = true;
308                    break;   
309                }
310        }
311
312
313
314    void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){
315        this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt));
316    }
317
318    bool PacmanGhost::findpos(Vector3 one, Vector3 other){
319        if((abs(one.x - other.x)<0.5) && (abs(one.z - other.z)<0.5)) return true;
320        return false;
321    }
322
323    void PacmanGhost::resetGhost(){
324        this->setPosition(this->resetposition);
325        this->ismoving = false;
326        this->actuelposition = this->getPosition();
327       
328        this->target_x = actuelposition.x;
329        this->target_z = actuelposition.z;
330    }
331}
Note: See TracBrowser for help on using the repository browser.