Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12313 was 12313, checked in by rueegseb, 5 years ago

extern variable in pacman.h hinzugefuegt

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