Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS19/src/modules/pacman/Pacman.cc @ 12303

Last change on this file since 12303 was 12303, checked in by peterf, 5 years ago

PacmanRandom

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