Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12385 was 12385, checked in by pemil, 5 years ago

final 1.0

  • Property svn:executable set to *
File size: 7.4 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        laser = 5; //after that number of eaten pointSpheres, the laser appears
51
52    }
53
54    void Pacman::levelUp()
55    {
56        //Reset each object
57        for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){
58                nextsphere->resetPacmanPointSphere();
59        }
60
61        for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){
62            next->resetPacmanPointAfraid();
63        }
64
65        //Level up ghosts
66        for(PacmanGhost* nextghost : ObjectList<PacmanGhost>()){
67            nextghost->levelupvelo(); 
68        }
69        //Reset ghosts and player
70        this->posreset();
71
72        //Increase maximum of points and level
73        totallevelpoint = ObjectList<PacmanPointSphere>().size() + totallevelpoint;
74        level++;
75    }
76
77
78    PacmanGhost* ghosts[8];
79
80
81    void Pacman::tick(float dt)
82    {
83
84        SUPER(Pacman, tick, dt);
85
86
87        //Needed for gameover
88        if(deathtime != 0){
89            dead(dt);
90
91
92         
93        }
94
95        //ingame loop
96        else{
97
98            //support by laser
99            if(point > laser){
100             
101                for(PacmanPointSphere* pointer : ObjectList<PacmanPointSphere>()){
102                    Vector3 pointSpherePosition = pointer->getPosition();
103
104                     if(pointSpherePosition.y > 0){// pointSphere above surface =not eaten yet
105
106                        PacmanLaser* pos = *(ObjectList<PacmanLaser>().begin());
107                        pos->setPosition(pointSpherePosition);
108                        break;
109                    }
110
111
112                }
113
114            }
115
116            //Register ghosts
117            int i = 0;
118            for(PacmanGhost* nextghost: ObjectList<PacmanGhost>()){
119                ghosts[i] = nextghost;
120                i++;
121            }
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            }
129
130            //Get position of player
131            player = this->getPlayer();
132            if (player != nullptr)
133            {
134                currentPosition = player->getWorldPosition();
135            }
136
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            }
142
143            if(bcolli){
144                this->catched(dt);
145            }
146
147            //Check for collision with PointSpheres and PacmanPointAfraid
148            for(PacmanPointSphere* nextsphere : ObjectList<PacmanPointSphere>()){
149                 if(nextsphere->taken(currentPosition))
150                    takePoint(nextsphere);
151            }
152
153            for(PacmanPointAfraid* next : ObjectList<PacmanPointAfraid>()){
154                if(next->taken(currentPosition))
155                  setAfraid();
156            }
157
158        } 
159
160    }
161
162    //Check for collisions between to objects (compare float numbers)
163    bool Pacman::collis(Vector3 one, Vector3 other){
164        if((abs(one.x-other.x)<19) && (abs(one.y-other.y)<10) && (abs(one.z-other.z)<19))
165            return true;
166        return false;
167    }
168
169    //Decrease live or resetghost
170    void Pacman::catched(float dt){
171
172    if(!this->afraid) {
173        if(!this->lives){
174          deathtime = 5;
175          this->dead(dt); 
176        }
177        --lives;
178        this->posreset();
179        }
180    else{
181        for(int nrghost = 0; nrghost<8; ++nrghost){
182            bcolli = collis(ghosts[nrghost]->getPosition(), currentPosition);
183            if(bcolli) ghosts[nrghost]->resetGhost();
184                bcolli = false;
185        }
186      }
187    }
188
189    //Change ghost design (to afraid)
190    void Pacman::setAfraid(){
191
192        timer = 10; //Set timer to 10 seconds
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; 
203    } 
204
205    //Change ghost design (to not afraid)
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
219    //Reset ghosts and plazer
220    void Pacman::posreset(){
221        for(int i = 0; i<4; ++i){
222            ghosts[i]->resetGhost();
223        }
224        player->setPosition(startposplayer);
225    }
226
227    //Collision with PointSphere
228    void Pacman::takePoint(PacmanPointSphere* taken){
229        ++point;
230       
231        if(point == totallevelpoint){ 
232            this->levelUp();
233            return;
234        }
235    }
236
237
238    PacmanGelb* Pacman::getPlayer()
239    {
240        for (PacmanGelb* ship : ObjectList<PacmanGelb>())
241        {
242            return ship;
243        }
244        return nullptr;
245    }
246
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
260    int Pacman::getPoints(){
261        return point;
262    }
263    //Getter
264    int Pacman::getLives(){
265        return lives;
266    }
267    //Getter
268    bool Pacman::isdead(){
269        return death;
270    }
271    //Getter
272    int Pacman::getTotalpoints(){
273        return totallevelpoint;
274    }
275
276
277    void Pacman::start()
278    {
279        Deathmatch::start();
280
281        //Hide afraided ghosts under map
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        }
290        orxout() << i << endl;
291
292        //Set maximum of points of first level
293        totallevelpoint = ObjectList<PacmanPointSphere>().size();
294
295    }
296
297    void Pacman::dead(float dt){
298        death = true;
299
300        deathtime = deathtime-dt;
301
302        if(deathtime<0)
303            this->end();
304    }
305
306    void Pacman::end()
307    {
308        GSLevel::startMainMenu();
309    }
310}
Note: See TracBrowser for help on using the repository browser.