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