| [11898] | 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: | 
|---|
| [11992] | 23 |  *      Marc Dreher | 
|---|
| [11898] | 24 |  *   Co-authors: | 
|---|
| [11992] | 25 |  *      .. | 
|---|
| [11898] | 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 |  | 
|---|
| [11927] | 48 |         this->velocity = Vector3(0, 0, 0); | 
|---|
| [11898] | 49 |         this->setCollisionType(CollisionType::Dynamic); | 
|---|
 | 50 |          | 
|---|
 | 51 |         this->actuelposition = this->getPosition(); | 
|---|
| [11945] | 52 |  | 
|---|
 | 53 |         if(findpos(actuelposition, Vector3(0,-20,0))) | 
|---|
 | 54 |             dontmove = true; | 
|---|
| [11898] | 55 |          | 
|---|
 | 56 |         this->target_x = actuelposition.x; | 
|---|
| [11927] | 57 |         this->target_z = actuelposition.z;  | 
|---|
| [11898] | 58 |  | 
|---|
 | 59 |     } | 
|---|
 | 60 |  | 
|---|
 | 61 |     /** | 
|---|
 | 62 |     @brief | 
|---|
| [11992] | 63 |         Destructor. Destroys ghost, if present. | 
|---|
| [11898] | 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 | 
|---|
| [11992] | 72 |         Method for creating a ghost through XML. | 
|---|
| [11898] | 73 |     */ | 
|---|
 | 74 |     void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 75 |     { | 
|---|
 | 76 |         SUPER(PacmanGhost, XMLPort, xmlelement, mode); | 
|---|
| [12252] | 77 |  | 
|---|
| [11898] | 78 |     } | 
|---|
 | 79 |  | 
|---|
 | 80 |  | 
|---|
| [12303] | 81 |    /* //All positions in the map, see documentation | 
|---|
| [11915] | 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 | 
|---|
| [11926] | 88 |         Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34 | 
|---|
| [11917] | 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 | 
|---|
| [12303] | 95 |         Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66*/ | 
|---|
| [11924] | 96 |  | 
|---|
| [11898] | 97 |     /** | 
|---|
 | 98 |     @brief | 
|---|
| [11992] | 99 |         Defines which actions the ghost has to take in each tick. | 
|---|
| [11898] | 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 | 
|---|
| [11915] | 110 |         if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){ | 
|---|
| [11898] | 111 |                  this->ismoving = false; | 
|---|
 | 112 |         } | 
|---|
 | 113 |  | 
|---|
 | 114 |         //Move, if ghost hasn't arrived yet | 
|---|
 | 115 |         if(this->ismoving){ | 
|---|
| [11915] | 116 |             if(!(abs(this->actuelposition.z-target_z)<0.5)) { | 
|---|
| [11979] | 117 |                 velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); | 
|---|
| [12303] | 118 |                 move(dt, actuelposition, velocity); | 
|---|
| [11898] | 119 |             }     | 
|---|
| [11915] | 120 |             if(!(abs(this->actuelposition.x-target_x)<0.5)){ | 
|---|
| [11979] | 121 |                 velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); | 
|---|
| [11898] | 122 |                 move(dt, actuelposition, velocity); | 
|---|
 | 123 |             } | 
|---|
 | 124 |         } | 
|---|
| [12259] | 125 |  | 
|---|
| [11903] | 126 |     } | 
|---|
 | 127 |  | 
|---|
| [12272] | 128 |  | 
|---|
| [11992] | 129 |     //Random choice of new target (not used in game, but useful) | 
|---|
| [11903] | 130 |     void PacmanGhost::setnewTarget(int firstdec){ | 
|---|
| [11923] | 131 |          | 
|---|
| [11903] | 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 |  | 
|---|
| [11992] | 142 |     //Change this with other ghost | 
|---|
| [11947] | 143 |     void PacmanGhost::changewith(PacmanGhost* otherghost){ | 
|---|
| [11949] | 144 |  | 
|---|
| [11950] | 145 |         while(lockmove){}; | 
|---|
| [11992] | 146 |         lockmove = true;    //Prevent change of target while ghost is changed | 
|---|
| [11949] | 147 |  | 
|---|
| [11945] | 148 |         otherghost->setPosition(this->getPosition()); | 
|---|
 | 149 |         this->setPosition(0,-20,0); | 
|---|
| [11992] | 150 |         otherghost->target_x = this->target_x;    | 
|---|
| [11948] | 151 |         otherghost->target_z = this->target_z; | 
|---|
| [11945] | 152 |         otherghost->ismoving = this->ismoving; | 
|---|
| [11903] | 153 |  | 
|---|
| [11945] | 154 |         this->dontmove = true; | 
|---|
 | 155 |         otherghost->dontmove = false; | 
|---|
| [11949] | 156 |  | 
|---|
| [11950] | 157 |         lockmove = false; | 
|---|
| [11945] | 158 |     } | 
|---|
| [11903] | 159 |  | 
|---|
| [11992] | 160 |     //Move ghost with rotation | 
|---|
| [11903] | 161 |     void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ | 
|---|
| [11976] | 162 |         if(!dontmove){ | 
|---|
| [11979] | 163 |             this->setPosition(Vector3(actuelposition.x+speed*velocity.x*dt,10,actuelposition.z+speed*velocity.z*dt)); | 
|---|
| [11992] | 164 |          | 
|---|
 | 165 |         //Rotate ghost in the direction of movement  | 
|---|
| [11979] | 166 |         if((abs(abs(velocity.x)-1)<0.1) && (abs(velocity.z-0)<0.1)) | 
|---|
| [11978] | 167 |             if(velocity.x<0){ | 
|---|
 | 168 |                  this->setOrientation(Quaternion(Radian(-1.57), Vector3(0, 1, 0)));   | 
|---|
| [11976] | 169 |             } | 
|---|
| [11978] | 170 |             else{ | 
|---|
 | 171 |                  this->setOrientation(Quaternion(Radian(1.57), Vector3(0, 1, 0)));   | 
|---|
| [11976] | 172 |             } | 
|---|
| [11979] | 173 |         if((abs(abs(velocity.z)-1)<0.1) && (abs(velocity.x-0)<0.1)) | 
|---|
| [11978] | 174 |             if(velocity.z<0){ | 
|---|
 | 175 |                  this->setOrientation(Quaternion(Radian(3.14), Vector3(0, 1, 0)));   | 
|---|
| [11976] | 176 |             } | 
|---|
| [11978] | 177 |             else{ | 
|---|
 | 178 |                  this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0)));   | 
|---|
 | 179 |             } | 
|---|
 | 180 |                       | 
|---|
| [11976] | 181 |      } | 
|---|
| [11903] | 182 |     } | 
|---|
 | 183 |  | 
|---|
| [11992] | 184 |     //Check if there is a collision | 
|---|
| [11903] | 185 |     bool PacmanGhost::findpos(Vector3 one, Vector3 other){ | 
|---|
| [11945] | 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; | 
|---|
| [11922] | 187 |         return false; | 
|---|
| [11903] | 188 |     } | 
|---|
 | 189 |  | 
|---|
| [11992] | 190 |     //Change ability to move | 
|---|
| [11944] | 191 |     void PacmanGhost::changemovability(){ | 
|---|
| [11933] | 192 |         if(dontmove){ | 
|---|
 | 193 |          dontmove = false;} | 
|---|
 | 194 |         else{ | 
|---|
 | 195 |          dontmove = true;    | 
|---|
 | 196 |         } | 
|---|
 | 197 |     } | 
|---|
 | 198 |  | 
|---|
| [11992] | 199 |     //ResetGhost | 
|---|
| [11903] | 200 |     void PacmanGhost::resetGhost(){ | 
|---|
| [11927] | 201 |      | 
|---|
| [11915] | 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; | 
|---|
| [11927] | 208 |      | 
|---|
| [11903] | 209 |     } | 
|---|
| [11958] | 210 |  | 
|---|
| [11992] | 211 |     //Increase speed of ghosts | 
|---|
| [11959] | 212 |     void PacmanGhost::levelupvelo(){ | 
|---|
| [11979] | 213 |         speed ++; | 
|---|
| [11958] | 214 |     } | 
|---|
| [11915] | 215 | } | 
|---|