Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added several pacmans

  • Property svn:executable set to *
File size: 6.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
37    //Check if there is a collision
38        bool findpos(Vector3 one, Vector3 other){
39       if((abs(one.x - other.x)<0.5) && (abs(one.y - other.y)<0.5) && (abs(one.z - other.z)<0.5)) return true;
40        return false;
41        }
42
43    //All positions in the map, see documentation
44     Vector3 possibleposition[67] = {Vector3(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4
45        Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9
46        Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14
47        Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19
48        Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24
49        Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29
50        Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34
51        Vector3(-70,10,105),Vector3(-20,10,105),Vector3(20,10,105),Vector3(70,10,105),Vector3(70,10,60), //35-39
52        Vector3(0,10,60),Vector3(-70,10,60),Vector3(-135,10,15),Vector3(-70,10,60),Vector3(0,10,15), //40-44
53        Vector3(70,10,15),Vector3(-70,10,-35),Vector3(-20,10,-35),Vector3(20,10,-35),Vector3(70,10,-35), //45-49
54        Vector3(70,10,-85),Vector3(20,10,-85),Vector3(-20,10,-85),Vector3(-70,10,-85),Vector3(-135,10,-85), //50-54
55        Vector3(-215,10,-85),Vector3(-215,10,-135),Vector3(-135,10,-135),Vector3(-70,10,-135),Vector3(-20,10,-135), //55-59
56        Vector3(20,10,-135),Vector3(70,10,-135),Vector3(20,10,-195),Vector3(-20,10,-195),Vector3(-135,10,-195), //60-64
57        Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66
58
59    RegisterClass(PacmanGhost);
60
61    /**
62    @brief
63        Constructor. Registers the object and initializes some default values.
64    @param creator
65        The creator of this object.
66    */
67    PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context)
68    {
69        RegisterObject(PacmanGhost);
70
71        this->velocity = Vector3(0, 0, 0);
72
73        this->setCollisionType(CollisionType::Dynamic);
74       
75        this->actuelposition = this->getPosition();
76
77        if(findpos(actuelposition, Vector3(0,-20,0)))
78            dontmove = true;
79       
80        this->target_x = actuelposition.x;
81        this->target_z = actuelposition.z; 
82
83    }
84
85    /**
86    @brief
87        Destructor. Destroys ghost, if present.
88    */
89    PacmanGhost::~PacmanGhost()
90    {
91        // Deletes the controller if the object was initialized and the pointer to the controller is not NULL.
92    }
93
94    /**
95    @brief
96        Method for creating a ghost through XML.
97    */
98     void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode)
99    {
100        SUPER(PacmanGhost, XMLPort, xmlelement, mode);
101    }
102
103    //Change this with other ghost
104    void PacmanGhost::changewith(PacmanGhost* otherghost){
105
106        while(lockmove){};
107        lockmove = true;    //Prevent change of target while ghost is changed
108
109        otherghost->setPosition(this->getPosition());
110        this->setPosition(0,-20,0);
111        otherghost->target_x = this->target_x;   
112        otherghost->target_z = this->target_z;
113        otherghost->ismoving = this->ismoving;
114
115        this->dontmove = true;
116        otherghost->dontmove = false;
117
118        lockmove = false;
119    }
120
121    //Move ghost with rotation
122    void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){
123        if(!dontmove){
124            this->setPosition(Vector3(actuelposition.x+speed*velocity.x*dt,10,actuelposition.z+speed*velocity.z*dt));
125       
126        //Rotate ghost in the direction of movement
127        if((abs(abs(velocity.x)-1)<0.1) && (abs(velocity.z-0)<0.1)){
128            if(velocity.x<0){
129                 this->setOrientation(Quaternion(Radian(-1.57), Vector3(0, 1, 0))); 
130            }
131            else{
132                 this->setOrientation(Quaternion(Radian(1.57), Vector3(0, 1, 0))); 
133            }
134        }
135        if((abs(abs(velocity.z)-1)<0.1) && (abs(velocity.x-0)<0.1)){
136            if(velocity.z<0){
137                 this->setOrientation(Quaternion(Radian(3.14), Vector3(0, 1, 0))); 
138            }
139            else{
140                 this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0))); 
141            }
142        }
143                     
144     }
145    }
146
147    //Change ability to move
148    void PacmanGhost::changemovability(){
149        if(dontmove){
150         dontmove = false;}
151        else{
152         dontmove = true;   
153        }
154    }
155
156    //ResetGhost
157    void PacmanGhost::resetGhost(){
158   
159        this->setPosition(this->resetposition);
160        this->ismoving = false;
161        this->actuelposition = this->getPosition();
162       
163        this->target_x = actuelposition.x;
164        this->target_z = actuelposition.z;
165   
166    }
167
168    //Increase speed of ghosts
169    void PacmanGhost::levelupvelo(){
170        speed ++;
171    }
172
173    Vector3 PacmanGhost::setPureArrayPos(Vector3 &posToSet){
174        //given that the position of a pacman is generally not "neat",
175        //we need to set it to the nearest point on the map
176        // if the pacman is not moving anymore, i.e. has reached a point
177        int i=0;
178        while(!(findpos(possibleposition[i], posToSet))){
179            i++;
180        }
181        return possibleposition[i];
182    }
183
184
185}
Note: See TracBrowser for help on using the repository browser.