Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS19/src/modules/pacman/PacmanGhost.cc @ 12303

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

PacmanRandom

  • Property svn:executable set to *
File size: 7.4 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 *      Marc Dreher
24 *   Co-authors:
25 *      ..
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        this->setCollisionType(CollisionType::Dynamic);
50       
51        this->actuelposition = this->getPosition();
52
53        if(findpos(actuelposition, Vector3(0,-20,0)))
54            dontmove = true;
55       
56        this->target_x = actuelposition.x;
57        this->target_z = actuelposition.z; 
58
59    }
60
61    /**
62    @brief
63        Destructor. Destroys ghost, 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 ghost through XML.
73    */
74    void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode)
75    {
76        SUPER(PacmanGhost, XMLPort, xmlelement, mode);
77
78    }
79
80
81   /* //All positions in the map, see documentation
82    Vector3 possibleposition[] = {Vector3(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4
83        Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9
84        Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14
85        Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19
86        Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24
87        Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29
88        Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34
89        Vector3(-70,10,105),Vector3(-20,10,105),Vector3(20,10,105),Vector3(70,10,105),Vector3(70,10,60), //35-39
90        Vector3(0,10,60),Vector3(-70,10,60),Vector3(-135,10,15),Vector3(-70,10,60),Vector3(0,10,15), //40-44
91        Vector3(70,10,15),Vector3(-70,10,-35),Vector3(-20,10,-35),Vector3(20,10,-35),Vector3(70,10,-35), //45-49
92        Vector3(70,10,-85),Vector3(20,10,-85),Vector3(-20,10,-85),Vector3(-70,10,-85),Vector3(-135,10,-85), //50-54
93        Vector3(-215,10,-85),Vector3(-215,10,-135),Vector3(-135,10,-135),Vector3(-70,10,-135),Vector3(-20,10,-135), //55-59
94        Vector3(20,10,-135),Vector3(70,10,-135),Vector3(20,10,-195),Vector3(-20,10,-195),Vector3(-135,10,-195), //60-64
95        Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66*/
96
97    /**
98    @brief
99        Defines which actions the ghost has to take in each tick.
100    @param dt
101        The length of the tick.
102    */
103    void PacmanGhost::tick(float dt)
104    {
105        SUPER(PacmanGhost, tick, dt);
106
107        this->actuelposition = this->getPosition();
108       
109        //Stop, if target arrived
110        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
111                 this->ismoving = false;
112        }
113
114        //Move, if ghost hasn't arrived yet
115        if(this->ismoving){
116            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
117                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z));
118                move(dt, actuelposition, velocity);
119            }   
120            if(!(abs(this->actuelposition.x-target_x)<0.5)){
121                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0);
122                move(dt, actuelposition, velocity);
123            }
124        }
125
126    }
127
128
129    //Random choice of new target (not used in game, but useful)
130    void PacmanGhost::setnewTarget(int firstdec){
131       
132          decision = rand()%1;
133            switch(decision){
134                case 0:
135                    this->target_x = possibleposition[firstdec].x;
136                    this->target_z = possibleposition[firstdec].z; 
137                    this->ismoving = true;
138                    break;
139                }
140    }
141
142    //Change this with other ghost
143    void PacmanGhost::changewith(PacmanGhost* otherghost){
144
145        while(lockmove){};
146        lockmove = true;    //Prevent change of target while ghost is changed
147
148        otherghost->setPosition(this->getPosition());
149        this->setPosition(0,-20,0);
150        otherghost->target_x = this->target_x;   
151        otherghost->target_z = this->target_z;
152        otherghost->ismoving = this->ismoving;
153
154        this->dontmove = true;
155        otherghost->dontmove = false;
156
157        lockmove = false;
158    }
159
160    //Move ghost with rotation
161    void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){
162        if(!dontmove){
163            this->setPosition(Vector3(actuelposition.x+speed*velocity.x*dt,10,actuelposition.z+speed*velocity.z*dt));
164       
165        //Rotate ghost in the direction of movement
166        if((abs(abs(velocity.x)-1)<0.1) && (abs(velocity.z-0)<0.1))
167            if(velocity.x<0){
168                 this->setOrientation(Quaternion(Radian(-1.57), Vector3(0, 1, 0))); 
169            }
170            else{
171                 this->setOrientation(Quaternion(Radian(1.57), Vector3(0, 1, 0))); 
172            }
173        if((abs(abs(velocity.z)-1)<0.1) && (abs(velocity.x-0)<0.1))
174            if(velocity.z<0){
175                 this->setOrientation(Quaternion(Radian(3.14), Vector3(0, 1, 0))); 
176            }
177            else{
178                 this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0))); 
179            }
180                     
181     }
182    }
183
184    //Check if there is a collision
185    bool PacmanGhost::findpos(Vector3 one, Vector3 other){
186       if((abs(one.x - other.x)<0.5) && (abs(one.y - other.y)<0.5) && (abs(one.z - other.z)<0.5)) return true;
187        return false;
188    }
189
190    //Change ability to move
191    void PacmanGhost::changemovability(){
192        if(dontmove){
193         dontmove = false;}
194        else{
195         dontmove = true;   
196        }
197    }
198
199    //ResetGhost
200    void PacmanGhost::resetGhost(){
201   
202        this->setPosition(this->resetposition);
203        this->ismoving = false;
204        this->actuelposition = this->getPosition();
205       
206        this->target_x = actuelposition.x;
207        this->target_z = actuelposition.z;
208   
209    }
210
211    //Increase speed of ghosts
212    void PacmanGhost::levelupvelo(){
213        speed ++;
214    }
215}
Note: See TracBrowser for help on using the repository browser.