Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Red Pacman seems to work partially

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