| [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: | 
|---|
| [11984] | 23 |  *      Marc Dreher | 
|---|
| [11898] | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | /** | 
|---|
 | 30 |     @file 3DPacman.cc | 
|---|
 | 31 |     @brief Implementation of the 3DPacman class. | 
|---|
 | 32 | */ | 
|---|
 | 33 |  | 
|---|
 | 34 | #include "Pacman.h" | 
|---|
 | 35 | #include "core/CoreIncludes.h" | 
|---|
 | 36 |  | 
|---|
 | 37 | namespace orxonox | 
|---|
 | 38 | { | 
|---|
| [11915] | 39 |     RegisterClass(Pacman); | 
|---|
| [11898] | 40 |  | 
|---|
 | 41 |     Pacman::Pacman(Context* context) : Deathmatch(context) | 
|---|
 | 42 |     { | 
|---|
 | 43 |         RegisterObject(Pacman); | 
|---|
 | 44 |  | 
|---|
| [11978] | 45 |         lives = 3; | 
|---|
| [11898] | 46 |         point = 0; | 
|---|
 | 47 |         level = 1; | 
|---|
 | 48 |  | 
|---|
 | 49 |     } | 
|---|
 | 50 |  | 
|---|
 | 51 |     void Pacman::levelUp() | 
|---|
 | 52 |     { | 
|---|
| [11984] | 53 |         //Reset each object | 
|---|
| [11958] | 54 |         for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){ | 
|---|
 | 55 |                 nextsphere->resetPacmanPointSphere(); | 
|---|
 | 56 |         } | 
|---|
 | 57 |  | 
|---|
 | 58 |         for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){ | 
|---|
 | 59 |             next->resetPacmanPointAfraid(); | 
|---|
 | 60 |         } | 
|---|
 | 61 |  | 
|---|
| [11984] | 62 |         //Level up ghosts | 
|---|
| [11958] | 63 |         for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){ | 
|---|
 | 64 |             nextghost->levelupvelo();  | 
|---|
 | 65 |         } | 
|---|
| [11984] | 66 |         //Reset ghosts and player | 
|---|
| [11958] | 67 |         this->posreset(); | 
|---|
| [11979] | 68 |  | 
|---|
| [11984] | 69 |         //Increase maximum of points and level | 
|---|
| [11979] | 70 |         totallevelpoint = ObjectList<PacmanPointSphere>().size() + totallevelpoint; | 
|---|
 | 71 |         level++; | 
|---|
| [11898] | 72 |     } | 
|---|
 | 73 |  | 
|---|
 | 74 |  | 
|---|
 | 75 |     PacmanGhost* ghosts[4]; | 
|---|
 | 76 |  | 
|---|
 | 77 |  | 
|---|
 | 78 |     void Pacman::tick(float dt) | 
|---|
 | 79 |     { | 
|---|
| [11915] | 80 |         SUPER(Pacman, tick, dt); | 
|---|
 | 81 |  | 
|---|
| [11984] | 82 |         //Needed for gameover | 
|---|
| [11976] | 83 |         if(deathtime != 0){ | 
|---|
 | 84 |             dead(dt); | 
|---|
 | 85 |         } | 
|---|
| [11954] | 86 |  | 
|---|
| [11984] | 87 |         //ingame loop | 
|---|
| [11976] | 88 |         else{ | 
|---|
| [11954] | 89 |  | 
|---|
| [11984] | 90 |             //Register ghosts | 
|---|
 | 91 |             int i = 0; | 
|---|
 | 92 |             for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){ | 
|---|
 | 93 |                 ghosts[i] = nextghost; | 
|---|
 | 94 |                 i++; | 
|---|
| [11939] | 95 |             } | 
|---|
 | 96 |  | 
|---|
| [11984] | 97 |             //Switch ghost to not-catchable, if timer is zero | 
|---|
 | 98 |             if(afraid){ | 
|---|
 | 99 |                 timer = timer - dt; | 
|---|
 | 100 |                 if(timer<=0){ | 
|---|
 | 101 |                     setNormal(); | 
|---|
 | 102 |                 } | 
|---|
 | 103 |             } | 
|---|
| [11976] | 104 |  | 
|---|
| [11984] | 105 |             //Get position of player | 
|---|
 | 106 |             player = this->getPlayer(); | 
|---|
 | 107 |             if (player != nullptr) | 
|---|
 | 108 |             { | 
|---|
 | 109 |                 currentPosition = player->getWorldPosition(); | 
|---|
 | 110 |             } | 
|---|
| [11898] | 111 |  | 
|---|
| [11984] | 112 |             //Check for collision with ghosts | 
|---|
 | 113 |             bcolli = false; | 
|---|
 | 114 |             for(int nrghost = 0; (nrghost<8) && (!bcolli); ++nrghost){ | 
|---|
 | 115 |                 bcolli = collis(ghosts[nrghost]->getPosition(), currentPosition); | 
|---|
 | 116 |             } | 
|---|
| [11898] | 117 |  | 
|---|
| [11984] | 118 |             if(bcolli){ | 
|---|
 | 119 |                 this->catched(dt); | 
|---|
 | 120 |             } | 
|---|
| [11915] | 121 |  | 
|---|
| [11984] | 122 |             //Check for collision with PointSpheres and PacmanPointAfraid | 
|---|
 | 123 |             for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){ | 
|---|
| [12003] | 124 |                  if(nextsphere->taken(currentPosition)) | 
|---|
 | 125 |                     takePoint(nextsphere); | 
|---|
| [11898] | 126 |             } | 
|---|
 | 127 |  | 
|---|
| [11984] | 128 |             for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){ | 
|---|
 | 129 |                 if(next->taken(currentPosition)) | 
|---|
 | 130 |                   setAfraid(); | 
|---|
| [11933] | 131 |             } | 
|---|
 | 132 |  | 
|---|
| [11979] | 133 |         }  | 
|---|
| [11976] | 134 |  | 
|---|
| [11898] | 135 |     } | 
|---|
 | 136 |  | 
|---|
| [11984] | 137 |     //Check for collisions between to objects (compare float numbers) | 
|---|
| [11898] | 138 |     bool Pacman::collis(Vector3 one, Vector3 other){ | 
|---|
| [11931] | 139 |         if((abs(one.x-other.x)<10) && (abs(one.y-other.y)<10) && (abs(one.z-other.z)<10)) | 
|---|
| [11898] | 140 |             return true; | 
|---|
 | 141 |         return false; | 
|---|
 | 142 |     } | 
|---|
 | 143 |  | 
|---|
| [11984] | 144 |     //Decrease live or resetghost | 
|---|
| [11976] | 145 |     void Pacman::catched(float dt){ | 
|---|
| [11933] | 146 |  | 
|---|
| [11984] | 147 |     if(!this->afraid) { | 
|---|
 | 148 |         if(!this->lives){ | 
|---|
| [11976] | 149 |           deathtime = 5; | 
|---|
 | 150 |           this->dead(dt);   | 
|---|
 | 151 |         } | 
|---|
| [11898] | 152 |         --lives; | 
|---|
 | 153 |         this->posreset(); | 
|---|
| [11933] | 154 |         } | 
|---|
 | 155 |     else{ | 
|---|
| [11945] | 156 |         for(int nrghost = 0; nrghost<8; ++nrghost){ | 
|---|
| [11984] | 157 |             bcolli = collis(ghosts[nrghost]->getPosition(), currentPosition); | 
|---|
 | 158 |             if(bcolli) ghosts[nrghost]->resetGhost(); | 
|---|
 | 159 |                 bcolli = false; | 
|---|
| [11933] | 160 |         } | 
|---|
 | 161 |       } | 
|---|
 | 162 |     } | 
|---|
 | 163 |  | 
|---|
| [11984] | 164 |     //Change ghost design (to afraid) | 
|---|
| [11944] | 165 |     void Pacman::setAfraid(){ | 
|---|
| [11945] | 166 |  | 
|---|
| [11944] | 167 |         timer = 10; //Set timer to 10 seconds | 
|---|
| [11945] | 168 |  | 
|---|
 | 169 |         //Change normal Ghosts with afraid ones | 
|---|
 | 170 |         if(!afraid){ | 
|---|
 | 171 |             ghosts[0]->changewith(ghosts[4]); | 
|---|
 | 172 |             ghosts[1]->changewith(ghosts[5]); | 
|---|
 | 173 |             ghosts[2]->changewith(ghosts[6]); | 
|---|
 | 174 |             ghosts[3]->changewith(ghosts[7]); | 
|---|
 | 175 |         } | 
|---|
 | 176 |  | 
|---|
 | 177 |         afraid = true;  | 
|---|
| [11898] | 178 |     }  | 
|---|
 | 179 |  | 
|---|
| [11984] | 180 |     //Change ghost design (to not afraid) | 
|---|
| [11945] | 181 |     void Pacman::setNormal(){ | 
|---|
 | 182 |  | 
|---|
 | 183 |         timer = 0; | 
|---|
 | 184 |  | 
|---|
 | 185 |         //Change normal Ghosts with afraid ones | 
|---|
 | 186 |             ghosts[4]->changewith(ghosts[0]); | 
|---|
 | 187 |             ghosts[5]->changewith(ghosts[1]); | 
|---|
 | 188 |             ghosts[6]->changewith(ghosts[2]); | 
|---|
 | 189 |             ghosts[7]->changewith(ghosts[3]); | 
|---|
 | 190 |  | 
|---|
 | 191 |         afraid = false;  | 
|---|
 | 192 |     }  | 
|---|
 | 193 |  | 
|---|
| [11984] | 194 |     //Reset ghosts and plazer | 
|---|
| [11898] | 195 |     void Pacman::posreset(){ | 
|---|
| [11956] | 196 |         for(int i = 0; i<4; ++i){ | 
|---|
 | 197 |             ghosts[i]->resetGhost(); | 
|---|
| [11898] | 198 |         } | 
|---|
 | 199 |         player->setPosition(startposplayer); | 
|---|
 | 200 |     } | 
|---|
 | 201 |  | 
|---|
| [11984] | 202 |     //Collision with PointSphere | 
|---|
| [11898] | 203 |     void Pacman::takePoint(PacmanPointSphere* taken){ | 
|---|
 | 204 |         ++point; | 
|---|
| [11979] | 205 |         if(point == totallevelpoint){  | 
|---|
 | 206 |             this->levelUp(); | 
|---|
 | 207 |             return; | 
|---|
 | 208 |         } | 
|---|
| [11898] | 209 |     } | 
|---|
 | 210 |  | 
|---|
 | 211 |  | 
|---|
 | 212 |     PacmanGelb* Pacman::getPlayer() | 
|---|
 | 213 |     { | 
|---|
 | 214 |         for (PacmanGelb* ship : ObjectList<PacmanGelb>()) | 
|---|
 | 215 |         { | 
|---|
 | 216 |             return ship; | 
|---|
 | 217 |         } | 
|---|
 | 218 |         return nullptr; | 
|---|
 | 219 |     } | 
|---|
 | 220 |  | 
|---|
| [11984] | 221 |     //Getter | 
|---|
 | 222 |     bool Pacman::getAfraid(){ | 
|---|
 | 223 |         return afraid; | 
|---|
 | 224 |     } | 
|---|
 | 225 |     //Getter | 
|---|
 | 226 |     int Pacman::getTimer(){ | 
|---|
 | 227 |         return timer; | 
|---|
 | 228 |     } | 
|---|
 | 229 |     //Getter | 
|---|
 | 230 |     int Pacman::getLevel(){ | 
|---|
 | 231 |         return level; | 
|---|
 | 232 |     } | 
|---|
 | 233 |     //Getter | 
|---|
| [11898] | 234 |     int Pacman::getPoints(){ | 
|---|
 | 235 |         return point; | 
|---|
 | 236 |     } | 
|---|
| [11984] | 237 |     //Getter | 
|---|
| [11978] | 238 |     int Pacman::getLives(){ | 
|---|
 | 239 |         return lives; | 
|---|
 | 240 |     } | 
|---|
| [11984] | 241 |     //Getter | 
|---|
 | 242 |     bool Pacman::isdead(){ | 
|---|
 | 243 |         return death; | 
|---|
 | 244 |     } | 
|---|
| [11992] | 245 |     //Getter | 
|---|
 | 246 |     int Pacman::getTotalpoints(){ | 
|---|
 | 247 |         return totallevelpoint; | 
|---|
 | 248 |     } | 
|---|
| [11898] | 249 |  | 
|---|
| [11978] | 250 |  | 
|---|
| [11898] | 251 |     void Pacman::start() | 
|---|
 | 252 |     { | 
|---|
 | 253 |         Deathmatch::start(); | 
|---|
| [11954] | 254 |  | 
|---|
| [11984] | 255 |         //Hide afraided ghosts under map | 
|---|
| [11954] | 256 |         int i = 0; | 
|---|
 | 257 |         for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){ | 
|---|
 | 258 |             if(3<i){  | 
|---|
 | 259 |                 nextghost->setPosition(0,-20,0); | 
|---|
 | 260 |                 nextghost->dontmove =  true; | 
|---|
 | 261 |             }; | 
|---|
 | 262 |             i++; | 
|---|
 | 263 |         } | 
|---|
| [11956] | 264 |  | 
|---|
| [11984] | 265 |         //Set maximum of points of first level | 
|---|
| [11956] | 266 |         totallevelpoint = ObjectList<PacmanPointSphere>().size(); | 
|---|
 | 267 |  | 
|---|
| [11898] | 268 |     } | 
|---|
 | 269 |  | 
|---|
| [11976] | 270 |     void Pacman::dead(float dt){ | 
|---|
 | 271 |         death = true; | 
|---|
 | 272 |  | 
|---|
 | 273 |         deathtime = deathtime-dt; | 
|---|
 | 274 |  | 
|---|
 | 275 |         if(deathtime<0) | 
|---|
 | 276 |             this->end(); | 
|---|
 | 277 |     } | 
|---|
 | 278 |  | 
|---|
| [11898] | 279 |     void Pacman::end() | 
|---|
 | 280 |     { | 
|---|
 | 281 |         GSLevel::startMainMenu(); | 
|---|
 | 282 |     } | 
|---|
 | 283 | } | 
|---|