Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS18/src/modules/pacman/Pacman.cc @ 11992

Last change on this file since 11992 was 11992, checked in by dreherm, 6 years ago

Testversion 1

  • Property svn:executable set to *
File size: 6.9 KB
RevLine 
[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
37namespace 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            i = 0;
124            for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){
125                if(collis(nextsphere->getPosition(), currentPosition))
126                 takePoint(nextsphere);
[11898]127            }
128
[11984]129            for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){
130                if(next->taken(currentPosition))
131                  setAfraid();
[11933]132            }
133
[11979]134        } 
[11976]135
[11898]136    }
137
[11984]138    //Check for collisions between to objects (compare float numbers)
[11898]139    bool Pacman::collis(Vector3 one, Vector3 other){
[11931]140        if((abs(one.x-other.x)<10) && (abs(one.y-other.y)<10) && (abs(one.z-other.z)<10))
[11898]141            return true;
142        return false;
143    }
144
[11984]145    //Decrease live or resetghost
[11976]146    void Pacman::catched(float dt){
[11933]147
[11984]148    if(!this->afraid) {
149        if(!this->lives){
[11976]150          deathtime = 5;
151          this->dead(dt); 
152        }
[11898]153        --lives;
154        this->posreset();
[11933]155        }
156    else{
[11945]157        for(int nrghost = 0; nrghost<8; ++nrghost){
[11984]158            bcolli = collis(ghosts[nrghost]->getPosition(), currentPosition);
159            if(bcolli) ghosts[nrghost]->resetGhost();
160                bcolli = false;
[11933]161        }
162      }
163    }
164
[11984]165    //Change ghost design (to afraid)
[11944]166    void Pacman::setAfraid(){
[11945]167
[11944]168        timer = 10; //Set timer to 10 seconds
[11945]169
170        //Change normal Ghosts with afraid ones
171        if(!afraid){
172            ghosts[0]->changewith(ghosts[4]);
173            ghosts[1]->changewith(ghosts[5]);
174            ghosts[2]->changewith(ghosts[6]);
175            ghosts[3]->changewith(ghosts[7]);
176        }
177
178        afraid = true; 
[11898]179    } 
180
[11984]181    //Change ghost design (to not afraid)
[11945]182    void Pacman::setNormal(){
183
184        timer = 0;
185
186        //Change normal Ghosts with afraid ones
187            ghosts[4]->changewith(ghosts[0]);
188            ghosts[5]->changewith(ghosts[1]);
189            ghosts[6]->changewith(ghosts[2]);
190            ghosts[7]->changewith(ghosts[3]);
191
192        afraid = false; 
193    } 
194
[11984]195    //Reset ghosts and plazer
[11898]196    void Pacman::posreset(){
[11956]197        for(int i = 0; i<4; ++i){
198            ghosts[i]->resetGhost();
[11898]199        }
200        player->setPosition(startposplayer);
201    }
202
[11984]203    //Collision with PointSphere
[11898]204    void Pacman::takePoint(PacmanPointSphere* taken){
205        ++point;
[11979]206        if(point == totallevelpoint){ 
207            this->levelUp();
208            return;
209        }
[11984]210        //Set PointSphere under map
[11898]211        Vector3 postaken = taken->getPosition();
212        postaken.y = -50;
213        taken->setPosition(postaken);
214    }
215
216
217    PacmanGelb* Pacman::getPlayer()
218    {
219        for (PacmanGelb* ship : ObjectList<PacmanGelb>())
220        {
221            return ship;
222        }
223        return nullptr;
224    }
225
[11984]226    //Getter
227    bool Pacman::getAfraid(){
228        return afraid;
229    }
230    //Getter
231    int Pacman::getTimer(){
232        return timer;
233    }
234    //Getter
235    int Pacman::getLevel(){
236        return level;
237    }
238    //Getter
[11898]239    int Pacman::getPoints(){
240        return point;
241    }
[11984]242    //Getter
[11978]243    int Pacman::getLives(){
244        return lives;
245    }
[11984]246    //Getter
247    bool Pacman::isdead(){
248        return death;
249    }
[11992]250    //Getter
251    int Pacman::getTotalpoints(){
252        return totallevelpoint;
253    }
[11898]254
[11978]255
[11898]256    void Pacman::start()
257    {
258        Deathmatch::start();
[11954]259
[11984]260        //Hide afraided ghosts under map
[11954]261        int i = 0;
262        for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){
263            if(3<i){ 
264                nextghost->setPosition(0,-20,0);
265                nextghost->dontmove =  true;
266            };
267            i++;
268        }
[11956]269
[11984]270        //Set maximum of points of first level
[11956]271        totallevelpoint = ObjectList<PacmanPointSphere>().size();
272
[11898]273    }
274
[11976]275    void Pacman::dead(float dt){
276        death = true;
277
278        deathtime = deathtime-dt;
279
280        if(deathtime<0)
281            this->end();
282    }
283
[11898]284    void Pacman::end()
285    {
286        GSLevel::startMainMenu();
287    }
288}
Note: See TracBrowser for help on using the repository browser.