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