| 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 | * Manuel Meier |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Cyrill Burgener |
|---|
| 26 | * Tomer Gidron |
|---|
| 27 | * |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | @file Hover.cc |
|---|
| 32 | @brief Implementation of the Hover class. Sets up the whole Minigame |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | #include "Hover.h" |
|---|
| 36 | #include "chat/ChatManager.h" |
|---|
| 37 | #include "HoverOrigin.h" |
|---|
| 38 | #include "HoverWall.h" |
|---|
| 39 | #include "HoverFlag.h" |
|---|
| 40 | #include "MazeGenerator.h" |
|---|
| 41 | #include "core/CoreIncludes.h" |
|---|
| 42 | #include "gamestates/GSLevel.h" |
|---|
| 43 | #include "HoverShip.h" |
|---|
| 44 | |
|---|
| 45 | #include "pickup/PickupSpawner.h" |
|---|
| 46 | #include "pickup/Pickup.h" |
|---|
| 47 | |
|---|
| 48 | namespace orxonox |
|---|
| 49 | { |
|---|
| 50 | RegisterUnloadableClass(Hover); |
|---|
| 51 | |
|---|
| 52 | Hover::Hover(Context* context) : Gametype(context) |
|---|
| 53 | { |
|---|
| 54 | RegisterObject(Hover); |
|---|
| 55 | |
|---|
| 56 | this->origin_ = nullptr; |
|---|
| 57 | this->numberOfFlags_ = 1; |
|---|
| 58 | this->firstTick_ = true; |
|---|
| 59 | level = 1; //start at level 1 |
|---|
| 60 | flagsTaken = 0;// took 0 flags in the beginning |
|---|
| 61 | lives = 3; |
|---|
| 62 | |
|---|
| 63 | numCells = 0; |
|---|
| 64 | cellSize = 0; |
|---|
| 65 | cellHeight = 0; |
|---|
| 66 | |
|---|
| 67 | bLevelUpgradeHUD = false; |
|---|
| 68 | |
|---|
| 69 | totFlags = 0; |
|---|
| 70 | |
|---|
| 71 | this->setHUDTemplate("HoverHUD"); |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void Hover::start() |
|---|
| 76 | { |
|---|
| 77 | Gametype::start(); |
|---|
| 78 | if(this->firstTick_ && this->origin_) |
|---|
| 79 | { |
|---|
| 80 | this->firstTick_ = false; |
|---|
| 81 | |
|---|
| 82 | numCells = this->origin_->getNumCells(); |
|---|
| 83 | cellSize = this->origin_->getCellSize(); |
|---|
| 84 | cellHeight = this->origin_->getCellHeight(); |
|---|
| 85 | |
|---|
| 86 | //TODO |
|---|
| 87 | //create boolean array/vector to assert that no two objects are placed in the same way |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | MazeGenerator generator(numCells); |
|---|
| 91 | generator.generateMaze(); |
|---|
| 92 | generator.renderMaze(); |
|---|
| 93 | |
|---|
| 94 | int* levelcode = generator.getLevelcode(); |
|---|
| 95 | |
|---|
| 96 | //Outer Walls |
|---|
| 97 | for(int i = 0; i<numCells; i++){ |
|---|
| 98 | (new HoverWall(origin_->getContext()))->init(0, i+1, cellSize, cellHeight, 1); |
|---|
| 99 | (new HoverWall(origin_->getContext()))->init(numCells, i+1, cellSize, cellHeight, 1); |
|---|
| 100 | (new HoverWall(origin_->getContext()))->init(i+1, 0, cellSize, cellHeight, 2); |
|---|
| 101 | (new HoverWall(origin_->getContext()))->init(i+1, numCells, cellSize, cellHeight, 2); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | //Ground |
|---|
| 106 | for(int i = 0; i<numCells; i++){ |
|---|
| 107 | for(int j = 0; j<numCells; j++){ |
|---|
| 108 | StaticEntity* groundCell = new StaticEntity(origin_->getContext()); |
|---|
| 109 | |
|---|
| 110 | groundCell->addTemplate(origin_->getGroundTemplate()); |
|---|
| 111 | groundCell->setPosition(get3dCoordinates(i,j,-60)); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | //Generate inner Walls according to levelcode |
|---|
| 119 | for(int y=0; y<numCells; y++){ |
|---|
| 120 | for(int x=0; x<numCells; x++){ |
|---|
| 121 | switch(levelcode[ y * numCells + x ]) |
|---|
| 122 | { |
|---|
| 123 | case 1: (new HoverWall(origin_->getContext()))->init(x+1, numCells-y, cellSize, cellHeight, 1); |
|---|
| 124 | break; |
|---|
| 125 | case 3: (new HoverWall(origin_->getContext()))->init(x+1, numCells-y, cellSize, cellHeight, 1); |
|---|
| 126 | case 2: (new HoverWall(origin_->getContext()))->init(x+1, numCells-y, cellSize, cellHeight, 0); |
|---|
| 127 | default: |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | createFlags(); |
|---|
| 134 | |
|---|
| 135 | //Generate 3 PickupSpawners randomly (destroy hover pickup) |
|---|
| 136 | for (int i = 0; i<3; i++) |
|---|
| 137 | { |
|---|
| 138 | PickupSpawner* pickupSpawner = new PickupSpawner(origin_->getContext()); |
|---|
| 139 | |
|---|
| 140 | pickupSpawner->setPosition(get3dCoordinates(rand()%numCells, rand()%numCells, 0.0f)); |
|---|
| 141 | pickupSpawner->setPickupTemplateName(origin_->getPickupTemplate()); |
|---|
| 142 | pickupSpawner->setMaxSpawnedItems(3); |
|---|
| 143 | pickupSpawner->setRespawnTime(30); |
|---|
| 144 | pickupSpawner->setTriggerDistance(40); |
|---|
| 145 | // Add pickup spawner to the pickup spawner list |
|---|
| 146 | pickupSpawners_.push_back(pickupSpawner); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | //Generate 3 PickupSpawners randomly (speed pickup) |
|---|
| 150 | for (int i = 0; i<3; i++) |
|---|
| 151 | { |
|---|
| 152 | PickupSpawner* pickupSpawner = new PickupSpawner(origin_->getContext()); |
|---|
| 153 | |
|---|
| 154 | pickupSpawner->setPosition(get3dCoordinates(rand()%numCells, rand()%numCells, 0.0f)); |
|---|
| 155 | pickupSpawner->setPickupTemplateName(origin_->getPickupTemplateSpeed()); |
|---|
| 156 | pickupSpawner->setMaxSpawnedItems(3); |
|---|
| 157 | pickupSpawner->setRespawnTime(30); |
|---|
| 158 | pickupSpawner->setTriggerDistance(40); |
|---|
| 159 | // Add pickup spawner to the pickup spawner list |
|---|
| 160 | pickupSpawners_.push_back(pickupSpawner); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | //Generate 3 PickupSpawners randomly (shrink pickup) |
|---|
| 164 | for (int i = 0; i<3; i++) |
|---|
| 165 | { |
|---|
| 166 | PickupSpawner* pickupSpawner = new PickupSpawner(origin_->getContext()); |
|---|
| 167 | |
|---|
| 168 | pickupSpawner->setPosition(get3dCoordinates(rand()%numCells, rand()%numCells, 0.0f)); |
|---|
| 169 | pickupSpawner->setPickupTemplateName(origin_->getPickupTemplateShrink()); |
|---|
| 170 | pickupSpawner->setMaxSpawnedItems(3); |
|---|
| 171 | pickupSpawner->setRespawnTime(30); |
|---|
| 172 | pickupSpawner->setTriggerDistance(40); |
|---|
| 173 | // Add pickup spawner to the pickup spawner list |
|---|
| 174 | pickupSpawners_.push_back(pickupSpawner); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | //***************************************************************************** |
|---|
| 178 | |
|---|
| 179 | //Generate destroyable crates randomly on field |
|---|
| 180 | |
|---|
| 181 | for (int i = 0; i<10; i++){ |
|---|
| 182 | |
|---|
| 183 | Pawn* crate = new Pawn(origin_->getContext()); |
|---|
| 184 | |
|---|
| 185 | crate->addTemplate(origin_->getObstacleTemplate()); |
|---|
| 186 | crate->setPosition(get3dCoordinates(rand()%numCells, rand()%numCells, 43.0f)); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | //If no lives are left, end game |
|---|
| 195 | if(lives <= 0) |
|---|
| 196 | { |
|---|
| 197 | GSLevel::startMainMenu(); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | // generate new Flags |
|---|
| 205 | void Hover::createFlags() |
|---|
| 206 | { |
|---|
| 207 | //TODO |
|---|
| 208 | //Generate flags randomly using bool array |
|---|
| 209 | |
|---|
| 210 | //Generate 5 flags randomly on field |
|---|
| 211 | for ( int i = 0; i < 5; i++ ) |
|---|
| 212 | { |
|---|
| 213 | HoverFlag* flag = new HoverFlag(origin_->getContext()); |
|---|
| 214 | flag->init(rand()%numCells, rand()%numCells, cellSize); |
|---|
| 215 | flags_.push_back(flag); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | if(flags_[i]->getPosition() == get3dCoordinates(0,0,-60)) |
|---|
| 220 | { |
|---|
| 221 | flags_[i]->destroyLater(); |
|---|
| 222 | flags_.erase(flags_.begin()+i); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void Hover::tick(float dt) |
|---|
| 229 | { |
|---|
| 230 | SUPER(Hover, tick, dt); |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | // Check if ship collided with one of the flags |
|---|
| 234 | for ( unsigned int i = 0; i < flags_.size(); i++ ) |
|---|
| 235 | { |
|---|
| 236 | if(flags_[i]->getCollided()) |
|---|
| 237 | { |
|---|
| 238 | flags_[i]->destroyLater(); |
|---|
| 239 | flags_.erase (flags_.begin()+i); |
|---|
| 240 | totFlags++; |
|---|
| 241 | if(flags_.size()<=0){ |
|---|
| 242 | //ChatManager::message("Level Up!"); |
|---|
| 243 | |
|---|
| 244 | levelUp(); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | } |
|---|
| 249 | numberOfFlags_ = flags_.size(); |
|---|
| 250 | |
|---|
| 251 | if(lives <= 0){ |
|---|
| 252 | GSLevel::startMainMenu(); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | //start new level - call to create new flags |
|---|
| 257 | void Hover::levelUp() |
|---|
| 258 | { |
|---|
| 259 | level++; |
|---|
| 260 | //increment lives after every 4 levels |
|---|
| 261 | if(level%4 == 0) |
|---|
| 262 | { |
|---|
| 263 | lives++; |
|---|
| 264 | } |
|---|
| 265 | createFlags(); |
|---|
| 266 | toggleShowLevel(); |
|---|
| 267 | showLevelTimer.setTimer(2.0f, false, createExecutor(createFunctor(&Hover::toggleShowLevel, this))); |
|---|
| 268 | |
|---|
| 269 | //spawn one additional crate randomly |
|---|
| 270 | Pawn* crate = new Pawn(origin_->getContext()); |
|---|
| 271 | crate->addTemplate(origin_->getObstacleTemplate()); |
|---|
| 272 | crate->setPosition(get3dCoordinates(rand()%numCells, rand()%numCells, 43.0f)); |
|---|
| 273 | |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | Vector3 Hover::get3dCoordinates(int x, int y, float heightOffset) |
|---|
| 277 | { |
|---|
| 278 | return Vector3(x*cellSize*1.0f + cellSize/2, heightOffset, y*cellSize*1.0f + cellSize/2); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | //if killed, subtract number of lives. If lives == 0, end game |
|---|
| 282 | void Hover::costLife() |
|---|
| 283 | { |
|---|
| 284 | lives--; |
|---|
| 285 | if (lives <= 0) |
|---|
| 286 | GSLevel::startMainMenu(); |
|---|
| 287 | } |
|---|
| 288 | } |
|---|