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