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 | * Leo Mehr Holz |
---|
24 | * Pascal Schärli |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file FlappyOrx.cc |
---|
30 | @brief Implementation of the FlappyOrx class. |
---|
31 | */ |
---|
32 | |
---|
33 | #include "FlappyOrx.h" |
---|
34 | #include "Highscore.h" |
---|
35 | #include "core/CoreIncludes.h" |
---|
36 | |
---|
37 | |
---|
38 | #include "core/EventIncludes.h" |
---|
39 | #include "core/command/Executor.h" |
---|
40 | #include "core/config/ConfigValueIncludes.h" |
---|
41 | #include "core/XMLPort.h" |
---|
42 | #include "gamestates/GSLevel.h" |
---|
43 | #include "chat/ChatManager.h" |
---|
44 | |
---|
45 | // ! HACK |
---|
46 | #include "infos/PlayerInfo.h" |
---|
47 | |
---|
48 | #include "FlappyOrxCenterPoint.h" |
---|
49 | #include "FlappyOrxAsteroid.h" |
---|
50 | #include "FlappyOrxShip.h" |
---|
51 | |
---|
52 | #include "core/command/ConsoleCommand.h" |
---|
53 | #include "worldentities/ExplosionPart.h" |
---|
54 | #include <vector> |
---|
55 | |
---|
56 | namespace orxonox |
---|
57 | { |
---|
58 | RegisterUnloadableClass(FlappyOrx); |
---|
59 | |
---|
60 | FlappyOrx::FlappyOrx(Context* context) : Deathmatch(context) |
---|
61 | { |
---|
62 | RegisterObject(FlappyOrx); |
---|
63 | this->center_ = nullptr; |
---|
64 | point = 0; //number of cleared tubes |
---|
65 | bIsDead = true; |
---|
66 | firstGame = true; //needed for the HUD |
---|
67 | |
---|
68 | tubeDistance=200; //distance between tubes |
---|
69 | tubeOffsetX=500; //tube offset (so that we can't see them spawn) |
---|
70 | |
---|
71 | circlesUsed=0; |
---|
72 | setHUDTemplate("FlappyOrxHUD"); |
---|
73 | } |
---|
74 | |
---|
75 | void FlappyOrx::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
76 | { |
---|
77 | SUPER(FlappyOrx, XMLPort, xmlelement, mode); |
---|
78 | XMLPortParam(FlappyOrx, "spawnDistance", setspawnDistance, getspawnDistance, xmlelement, mode); |
---|
79 | XMLPortParam(FlappyOrx, "Speed", setSpeed, getSpeed, xmlelement, mode); |
---|
80 | } |
---|
81 | |
---|
82 | void FlappyOrx::updatePlayerPos(int x){ |
---|
83 | |
---|
84 | //Spawn a new Tube when the spawn distance is reached |
---|
85 | if(this->tubes.size()==0||x-tubes.back()+tubeOffsetX>tubeDistance){ |
---|
86 | spawnTube(); |
---|
87 | this->tubes.push(x+tubeOffsetX); |
---|
88 | } |
---|
89 | //Delete Tubes when we pass through them |
---|
90 | if(this->tubes.size()!=0&&x>this->tubes.front()){ |
---|
91 | this->tubes.pop(); |
---|
92 | levelUp(); |
---|
93 | } |
---|
94 | //Delete Asteroids which are not visible anymore |
---|
95 | while((this->asteroids.front())->getPosition().x<x-tubeOffsetX){ |
---|
96 | MovableEntity* deleteMe = asteroids.front(); |
---|
97 | asteroids.pop(); |
---|
98 | deleteMe->destroy(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | //Gets called when we pass through a Tube |
---|
103 | void FlappyOrx::levelUp(){ |
---|
104 | point++; |
---|
105 | tubeDistance = tubeDistanceBase-tubeDistanceIncrease*point; //smaller spawn Distance |
---|
106 | getPlayer()->setSpeed(speedBase+speedIncrease*point); //increase speed |
---|
107 | } |
---|
108 | |
---|
109 | //Returns our Ship |
---|
110 | FlappyOrxShip* FlappyOrx::getPlayer(){ |
---|
111 | if (player == nullptr){ |
---|
112 | for (FlappyOrxShip* ship : ObjectList<FlappyOrxShip>()) { |
---|
113 | player = ship; |
---|
114 | } |
---|
115 | } |
---|
116 | return player; |
---|
117 | } |
---|
118 | |
---|
119 | //Spawn new Tube |
---|
120 | void FlappyOrx::spawnTube(){ |
---|
121 | if (getPlayer() == nullptr) |
---|
122 | return; |
---|
123 | |
---|
124 | int space = 120; //vertical space between top and bottom tube |
---|
125 | int height = (float(rand())/RAND_MAX-0.5)*(280-space); //Randomize height |
---|
126 | |
---|
127 | Vector3 pos = player->getPosition(); |
---|
128 | |
---|
129 | //create the two Asteroid fields (Tubes) |
---|
130 | asteroidField(pos.x+tubeOffsetX,height-space/2,0.8); //bottom |
---|
131 | asteroidField(pos.x+tubeOffsetX,height+space/2,-0.8); //top |
---|
132 | } |
---|
133 | |
---|
134 | //Creates a new asteroid Field |
---|
135 | void FlappyOrx::asteroidField(int x, int y, float slope){ |
---|
136 | int r = 20; //Radius of added Asteroids |
---|
137 | int noadd = 0; //how many times we failed to add a new asteroid |
---|
138 | |
---|
139 | clearCircles(); //Delete Circles (we use circles to make sure we don't spawn two asteroids on top of eachother) |
---|
140 | Circle newAsteroid = Circle(); |
---|
141 | newAsteroid.x=x; |
---|
142 | newAsteroid.y=y; |
---|
143 | newAsteroid.r=r; |
---|
144 | addIfPossible(newAsteroid); //Add Asteroid at peak |
---|
145 | |
---|
146 | //Fill up triangle with asteroids |
---|
147 | while(noadd<5&&circlesUsed<nCircles){ |
---|
148 | if(slope>0) |
---|
149 | newAsteroid.y=float(rand())/RAND_MAX*(150+y)-150; //create asteroid on bottom |
---|
150 | else |
---|
151 | newAsteroid.y=float(rand())/RAND_MAX*(150-y)+y; //create asteroid on top |
---|
152 | |
---|
153 | newAsteroid.x=x+(float(rand())/RAND_MAX-0.5)*(y-newAsteroid.y)/slope; |
---|
154 | newAsteroid.r=r; |
---|
155 | |
---|
156 | int i = addIfPossible(newAsteroid); //Add Asteroid if it doesn't collide |
---|
157 | if(i==0) |
---|
158 | noadd++; |
---|
159 | else if(i==2) |
---|
160 | noadd=5; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | //Create a new Asteroid |
---|
165 | void FlappyOrx::createAsteroid(Circle &c){ |
---|
166 | MovableEntity* newAsteroid = new MovableEntity(this->center_->getContext()); |
---|
167 | |
---|
168 | //Add Model fitting the Size of the Asteroid |
---|
169 | if(c.r<=5) |
---|
170 | newAsteroid->addTemplate(Asteroid5[rand()%NUM_ASTEROIDS]); |
---|
171 | else if(c.r<=10) |
---|
172 | newAsteroid->addTemplate(Asteroid10[rand()%NUM_ASTEROIDS]); |
---|
173 | else if(c.r<=15) |
---|
174 | newAsteroid->addTemplate(Asteroid15[rand()%NUM_ASTEROIDS]); |
---|
175 | else |
---|
176 | newAsteroid->addTemplate(Asteroid20[rand()%NUM_ASTEROIDS]); |
---|
177 | |
---|
178 | //Set position |
---|
179 | newAsteroid->setPosition(Vector3(c.x, 0, c.y)); |
---|
180 | |
---|
181 | //Randomize orientation |
---|
182 | newAsteroid->setOrientation(Vector3::UNIT_Z, Degree(rand()%360)); |
---|
183 | newAsteroid->setOrientation(Vector3::UNIT_Y, Degree(rand()%360)); |
---|
184 | |
---|
185 | //add to Queue (so that we can delete it again) |
---|
186 | asteroids.push(newAsteroid); |
---|
187 | } |
---|
188 | |
---|
189 | //Deletes Asteroids array which stores all the circles used to make sure no asteroids collide when spawning |
---|
190 | void FlappyOrx::clearCircles(){ |
---|
191 | circlesUsed=0; |
---|
192 | for(int i = 0; i<this->nCircles; i++){ |
---|
193 | circles[i].r=0; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | //checks if two circles collide |
---|
198 | bool FlappyOrx::circleCollision(Circle &c1, Circle &c2){ |
---|
199 | if(c1.r<=0 || c2.r<=0) |
---|
200 | return false; |
---|
201 | int x = c1.x - c2.x; |
---|
202 | int y = c1.y - c2.y; |
---|
203 | int r = c1.r + c2.r; |
---|
204 | |
---|
205 | return x*x+y*y<r*r*1.5; |
---|
206 | } |
---|
207 | |
---|
208 | //Adds a circle if its not colliding |
---|
209 | int FlappyOrx::addIfPossible(Circle c){ |
---|
210 | int i; |
---|
211 | for(i=0; i<this->nCircles && this->circles[i].r!=0 && c.r>0;i++){ |
---|
212 | while(circleCollision(c,this->circles[i])){ |
---|
213 | c.r-=5; //when it collides, try to make it smaller |
---|
214 | } |
---|
215 | } |
---|
216 | if(c.r<=0){ |
---|
217 | return 0; |
---|
218 | } |
---|
219 | circlesUsed++; |
---|
220 | this->circles[i].x=c.x; |
---|
221 | this->circles[i].y=c.y; |
---|
222 | this->circles[i].r=c.r; |
---|
223 | createAsteroid(c); |
---|
224 | return 1; |
---|
225 | } |
---|
226 | |
---|
227 | void FlappyOrx::setCenterpoint(FlappyOrxCenterPoint* center){ |
---|
228 | this->center_ = center; |
---|
229 | } |
---|
230 | |
---|
231 | bool FlappyOrx::isDead(){ |
---|
232 | return bIsDead; |
---|
233 | } |
---|
234 | |
---|
235 | void FlappyOrx::setDead(bool value){ |
---|
236 | bIsDead = value; |
---|
237 | if(not value){ |
---|
238 | point = -1; |
---|
239 | levelUp(); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | void FlappyOrx::start() |
---|
244 | { |
---|
245 | // Set variable to temporarily force the player to spawn. |
---|
246 | this->bForceSpawn_ = true; |
---|
247 | |
---|
248 | if (this->center_ == nullptr) // abandon mission! |
---|
249 | { |
---|
250 | orxout(internal_error) << "FlappyOrx: No Centerpoint specified." << endl; |
---|
251 | GSLevel::startMainMenu(); |
---|
252 | return; |
---|
253 | } |
---|
254 | // Call start for the parent class. |
---|
255 | Deathmatch::start(); |
---|
256 | } |
---|
257 | |
---|
258 | //RIP |
---|
259 | void FlappyOrx::death(){ |
---|
260 | bIsDead = true; |
---|
261 | firstGame = false; |
---|
262 | |
---|
263 | //Set randomized deathmessages |
---|
264 | if(point<10) sDeathMessage = DeathMessage10[rand()%(DeathMessage10.size())]; |
---|
265 | else if(point<30) sDeathMessage = DeathMessage30[rand()%(DeathMessage30.size())]; |
---|
266 | else if(point<50) sDeathMessage = DeathMessage50[rand()%(DeathMessage50.size())]; |
---|
267 | else sDeathMessage = DeathMessageover50[rand()%(DeathMessageover50.size())]; |
---|
268 | |
---|
269 | //Update Highscore |
---|
270 | if (Highscore::exists()){ |
---|
271 | int score = this->getPoints(); |
---|
272 | if(score > Highscore::getInstance().getHighestScoreOfGame("Flappy Orx")) |
---|
273 | Highscore::getInstance().storeHighscore("Flappy Orx",score); |
---|
274 | } |
---|
275 | |
---|
276 | //Delete all Tubes and asteroids |
---|
277 | while (!tubes.empty()){ |
---|
278 | tubes.pop(); |
---|
279 | } |
---|
280 | while (!asteroids.empty()){ |
---|
281 | MovableEntity* deleteMe = asteroids.front(); |
---|
282 | asteroids.pop(); |
---|
283 | deleteMe->destroy(); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | void FlappyOrx::end() |
---|
288 | { |
---|
289 | // DON'T CALL THIS! |
---|
290 | // Deathmatch::end(); |
---|
291 | // It will misteriously crash the game! |
---|
292 | // Instead startMainMenu, this won't crash. |
---|
293 | if (Highscore::exists()){ |
---|
294 | int score = this->getPoints(); |
---|
295 | if(score > Highscore::getInstance().getHighestScoreOfGame("Orxonox Arcade")) |
---|
296 | Highscore::getInstance().storeHighscore("Orxonox Arcade",score); |
---|
297 | |
---|
298 | } |
---|
299 | GSLevel::startMainMenu(); |
---|
300 | } |
---|
301 | } |
---|