Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

final 1.2

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