| 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 | /** | 
|---|
| 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 | { | 
|---|
| 39 | RegisterClass(Pacman); | 
|---|
| 40 |  | 
|---|
| 41 | Pacman::Pacman(Context* context) : Deathmatch(context) | 
|---|
| 42 | { | 
|---|
| 43 | RegisterObject(Pacman); | 
|---|
| 44 |  | 
|---|
| 45 | lives = 3; | 
|---|
| 46 | point = 0; | 
|---|
| 47 | level = 1; | 
|---|
| 48 |  | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void Pacman::levelUp() | 
|---|
| 52 | { | 
|---|
| 53 | //Reset each object | 
|---|
| 54 | for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){ | 
|---|
| 55 | nextsphere->resetPacmanPointSphere(); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){ | 
|---|
| 59 | next->resetPacmanPointAfraid(); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | //Level up ghosts | 
|---|
| 63 | for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){ | 
|---|
| 64 | nextghost->levelupvelo(); | 
|---|
| 65 | } | 
|---|
| 66 | //Reset ghosts and player | 
|---|
| 67 | this->posreset(); | 
|---|
| 68 |  | 
|---|
| 69 | //Increase maximum of points and level | 
|---|
| 70 | totallevelpoint = ObjectList<PacmanPointSphere>().size() + totallevelpoint; | 
|---|
| 71 | level++; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | PacmanGhost* ghosts[4]; | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | void Pacman::tick(float dt) | 
|---|
| 79 | { | 
|---|
| 80 | SUPER(Pacman, tick, dt); | 
|---|
| 81 |  | 
|---|
| 82 | //Needed for gameover | 
|---|
| 83 | if(deathtime != 0){ | 
|---|
| 84 | dead(dt); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | //ingame loop | 
|---|
| 88 | else{ | 
|---|
| 89 |  | 
|---|
| 90 | //Register ghosts | 
|---|
| 91 | int i = 0; | 
|---|
| 92 | for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){ | 
|---|
| 93 | ghosts[i] = nextghost; | 
|---|
| 94 | i++; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 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 | } | 
|---|
| 104 |  | 
|---|
| 105 | //Get position of player | 
|---|
| 106 | player = this->getPlayer(); | 
|---|
| 107 | if (player != nullptr) | 
|---|
| 108 | { | 
|---|
| 109 | currentPosition = player->getWorldPosition(); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 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 | } | 
|---|
| 117 |  | 
|---|
| 118 | if(bcolli){ | 
|---|
| 119 | this->catched(dt); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | //Check for collision with PointSpheres and PacmanPointAfraid | 
|---|
| 123 | for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){ | 
|---|
| 124 | if(nextsphere->taken(currentPosition)) | 
|---|
| 125 | takePoint(nextsphere); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){ | 
|---|
| 129 | if(next->taken(currentPosition)) | 
|---|
| 130 | setAfraid(); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | //Check for collisions between to objects (compare float numbers) | 
|---|
| 138 | bool Pacman::collis(Vector3 one, Vector3 other){ | 
|---|
| 139 | if((abs(one.x-other.x)<10) && (abs(one.y-other.y)<10) && (abs(one.z-other.z)<10)) | 
|---|
| 140 | return true; | 
|---|
| 141 | return false; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | //Decrease live or resetghost | 
|---|
| 145 | void Pacman::catched(float dt){ | 
|---|
| 146 |  | 
|---|
| 147 | if(!this->afraid) { | 
|---|
| 148 | if(!this->lives){ | 
|---|
| 149 | deathtime = 5; | 
|---|
| 150 | this->dead(dt); | 
|---|
| 151 | } | 
|---|
| 152 | --lives; | 
|---|
| 153 | this->posreset(); | 
|---|
| 154 | } | 
|---|
| 155 | else{ | 
|---|
| 156 | for(int nrghost = 0; nrghost<8; ++nrghost){ | 
|---|
| 157 | bcolli = collis(ghosts[nrghost]->getPosition(), currentPosition); | 
|---|
| 158 | if(bcolli) ghosts[nrghost]->resetGhost(); | 
|---|
| 159 | bcolli = false; | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | //Change ghost design (to afraid) | 
|---|
| 165 | void Pacman::setAfraid(){ | 
|---|
| 166 |  | 
|---|
| 167 | timer = 10; //Set timer to 10 seconds | 
|---|
| 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; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | //Change ghost design (to not afraid) | 
|---|
| 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 |  | 
|---|
| 194 | //Reset ghosts and plazer | 
|---|
| 195 | void Pacman::posreset(){ | 
|---|
| 196 | for(int i = 0; i<4; ++i){ | 
|---|
| 197 | ghosts[i]->resetGhost(); | 
|---|
| 198 | } | 
|---|
| 199 | player->setPosition(startposplayer); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | //Collision with PointSphere | 
|---|
| 203 | void Pacman::takePoint(PacmanPointSphere* taken){ | 
|---|
| 204 | ++point; | 
|---|
| 205 | if(point == totallevelpoint){ | 
|---|
| 206 | this->levelUp(); | 
|---|
| 207 | return; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 | PacmanGelb* Pacman::getPlayer() | 
|---|
| 213 | { | 
|---|
| 214 | for (PacmanGelb* ship : ObjectList<PacmanGelb>()) | 
|---|
| 215 | { | 
|---|
| 216 | return ship; | 
|---|
| 217 | } | 
|---|
| 218 | return nullptr; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 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 | 
|---|
| 234 | int Pacman::getPoints(){ | 
|---|
| 235 | return point; | 
|---|
| 236 | } | 
|---|
| 237 | //Getter | 
|---|
| 238 | int Pacman::getLives(){ | 
|---|
| 239 | return lives; | 
|---|
| 240 | } | 
|---|
| 241 | //Getter | 
|---|
| 242 | bool Pacman::isdead(){ | 
|---|
| 243 | return death; | 
|---|
| 244 | } | 
|---|
| 245 | //Getter | 
|---|
| 246 | int Pacman::getTotalpoints(){ | 
|---|
| 247 | return totallevelpoint; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 |  | 
|---|
| 251 | void Pacman::start() | 
|---|
| 252 | { | 
|---|
| 253 | Deathmatch::start(); | 
|---|
| 254 |  | 
|---|
| 255 | //Hide afraided ghosts under map | 
|---|
| 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 | } | 
|---|
| 264 |  | 
|---|
| 265 | //Set maximum of points of first level | 
|---|
| 266 | totallevelpoint = ObjectList<PacmanPointSphere>().size(); | 
|---|
| 267 |  | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | void Pacman::dead(float dt){ | 
|---|
| 271 | death = true; | 
|---|
| 272 |  | 
|---|
| 273 | deathtime = deathtime-dt; | 
|---|
| 274 |  | 
|---|
| 275 | if(deathtime<0) | 
|---|
| 276 | this->end(); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | void Pacman::end() | 
|---|
| 280 | { | 
|---|
| 281 | GSLevel::startMainMenu(); | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|