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