[9098] | 1 | /* |
---|
[9112] | 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[9098] | 4 | * |
---|
[9112] | 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 | * |
---|
[10258] | 22 | * Author: |
---|
[9112] | 23 | * |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | *NACHRICHT: |
---|
| 28 | * |
---|
| 29 | * Hier empfehle ich euch die gesamte Spielogik unter zu bringen. Viele Funktionen werden automatisch |
---|
| 30 | * bei gewissen Ereignissen aufgerufen bzw. lösen Ereignisse aus |
---|
| 31 | * |
---|
| 32 | *Z.B: |
---|
| 33 | * start() //wird aufgerufen, bevor das Spiel losgeht |
---|
| 34 | * end() //wenn man diese Funktion aufruft wird |
---|
| 35 | * pawnKilled() // wird aufgerufen, wenn ein Pawn stirbt (z.B: wenn ) |
---|
| 36 | * playerScored() // kann man aufrufen um dem Spieler Punkte zu vergeben. |
---|
| 37 | * |
---|
| 38 | * |
---|
| 39 | *TIPP: Eclipse hilft euch schnell auf bereits vorhanden Funktionen zuzugreifen: |
---|
| 40 | * einfach "this->" eingeben und kurz warten. Dann tauch eine Liste mit Vorschlägen auf. Wenn ihr jetzt weiter |
---|
| 41 | * tippt, werden die Vorschläge entsprechend gefiltert. |
---|
| 42 | * |
---|
| 43 | * |
---|
| 44 | *TIPP: schaut euch mal Tetris::createStone() an. Dort wird ein TetrisStone-Objekt (ControllableEntity) erzeugt, |
---|
| 45 | * ihm ein Template zugewiesen (welches vorher im Level definiert wurde und dem CenterPoint übergeben wurde) |
---|
| 46 | * Ähnlich könnt ihr vorgehen, um einen Turm zu erzeugen. (Zusätzlich braucht ein Turm noch einen Controller) |
---|
| 47 | * z.B: WaypointPatrolController. Wenn kein Team zugewiesen wurde bekämpft ein WaypointPatrolController alles, |
---|
| 48 | * was in seiner Reichweite liegt. |
---|
| 49 | * |
---|
[9143] | 50 | * |
---|
| 51 | *HUD: |
---|
| 52 | * Ein Gametype kann ein HUD (Head up Display haben.) Z.B: hat Pong eine Anzeige welcher Spieler wieviele Punkte hat. |
---|
| 53 | * Generell kann man a) Grafiken oder b) Zeichen in einer HUD anzeigen. |
---|
| 54 | * Fuer den ersten Schritt reicht reiner Text. |
---|
| 55 | * |
---|
| 56 | * a) |
---|
| 57 | * PongScore.cc uebernehmen und eigene Klasse draus machen. |
---|
| 58 | * Wenn ihr bloss anzeigen wollt wieviele Punkte der Spieler bereits erspielt hat (Punkte = Kapital fuer neue Tuerme) dann orientiert ihr euch an |
---|
| 59 | * TetrisScore.cc (im pCuts branch): http://www.orxonox.net/browser/code/branches/pCuts/src/modules/tetris/TetrisScore.cc |
---|
| 60 | * Ich habe TetrisScore lediglich dazu gebraucht, um eine Variable auf dem HUD auszugeben. Ein Objekt fuer statischen Text gibt es bereits. |
---|
| 61 | * |
---|
| 62 | * b) |
---|
| 63 | * Im naesten Schritt erstellt man die Vorlage fuer das HUD-Objekt: siehe /data/overlays/pongHUD |
---|
| 64 | * OverlayText ist eine Vorlage fuer statischen text zb: "Points Scored:". Aus mir nicht erklaerlichen Gruenden sollte man die OverlayText |
---|
| 65 | * Objekte immer erst nach dem PongScore anlegen. |
---|
| 66 | * |
---|
[9180] | 67 | * c) Im TowerDefense gamtype muss im Constructor noch das HUD-Template gesetzt werden. |
---|
[9143] | 68 | * |
---|
| 69 | * d) in CMakeLists.txt noch das Module includen das fuer die Overlays zustaendig ist. Siehe das gleiche File im Pong module. |
---|
| 70 | * |
---|
| 71 | * |
---|
| 72 | * |
---|
[9098] | 73 | */ |
---|
[9112] | 74 | #include "TowerDefense.h" |
---|
[10258] | 75 | #include "TowerDefenseTower.h" |
---|
[9137] | 76 | #include "TowerDefenseCenterpoint.h" |
---|
[9122] | 77 | #include "worldentities/SpawnPoint.h" |
---|
[9142] | 78 | #include "controllers/WaypointController.h" |
---|
| 79 | #include "graphics/Model.h" |
---|
| 80 | #include "infos/PlayerInfo.h" |
---|
[9138] | 81 | #include "chat/ChatManager.h" |
---|
[9667] | 82 | #include "core/CoreIncludes.h" |
---|
[11356] | 83 | #include "Highscore.h" |
---|
[9136] | 84 | |
---|
[9112] | 85 | namespace orxonox |
---|
| 86 | { |
---|
[9667] | 87 | RegisterUnloadableClass(TowerDefense); |
---|
[9272] | 88 | |
---|
[10622] | 89 | TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context) |
---|
[9112] | 90 | { |
---|
| 91 | RegisterObject(TowerDefense); |
---|
[9272] | 92 | |
---|
[11071] | 93 | selecter = nullptr; |
---|
| 94 | this->player_ = nullptr; |
---|
[9180] | 95 | this->setHUDTemplate("TowerDefenseHUD"); |
---|
[10629] | 96 | this->waveNumber_ = 0; |
---|
| 97 | this->timeSinceLastSpawn_ = 0.0; |
---|
| 98 | this->timeUntilNextWave_ = 0.0; |
---|
[10622] | 99 | this->credit_ = 0; |
---|
| 100 | this->lifes_ = 0; |
---|
[10629] | 101 | this->waveSize_ = 0; |
---|
| 102 | offset_ = Vector3(0,0,10); |
---|
[9112] | 103 | } |
---|
[9272] | 104 | |
---|
[9136] | 105 | TowerDefense::~TowerDefense() |
---|
| 106 | { |
---|
| 107 | if (this->isInitialized()) |
---|
| 108 | { |
---|
[10629] | 109 | |
---|
[9136] | 110 | } |
---|
| 111 | } |
---|
[9272] | 112 | |
---|
| 113 | void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint) |
---|
| 114 | { |
---|
| 115 | this->center_ = centerpoint; |
---|
| 116 | } |
---|
| 117 | |
---|
[9112] | 118 | void TowerDefense::start() |
---|
[10629] | 119 | { |
---|
[11071] | 120 | if (center_ != nullptr) // There needs to be a TowerDefenseCenterpoint, i.e. the area the game takes place. |
---|
[10622] | 121 | { |
---|
[11071] | 122 | if (selecter == nullptr) |
---|
[10622] | 123 | { |
---|
| 124 | selecter = new TowerDefenseSelecter(this->center_->getContext()); |
---|
| 125 | } |
---|
| 126 | selecter->addTemplate(center_->getSelecterTemplate()); |
---|
| 127 | center_->attach(selecter); |
---|
| 128 | } |
---|
| 129 | else // If no centerpoint was specified, an error is thrown and the level is exited. |
---|
| 130 | { |
---|
| 131 | orxout(internal_error) << "Jump: No Centerpoint specified." << endl; |
---|
| 132 | return; |
---|
| 133 | } |
---|
[10258] | 134 | |
---|
[10629] | 135 | enemies_.clear(); |
---|
| 136 | |
---|
| 137 | createFields(); |
---|
[10622] | 138 | TeamDeathmatch::start(); |
---|
[9143] | 139 | |
---|
[10258] | 140 | //set initial credits, lifes and WaveNumber |
---|
[10629] | 141 | this->setCredit(1000); |
---|
| 142 | this->setLifes(100); |
---|
| 143 | this->timeSinceLastSpawn_ = 0.0; |
---|
[12018] | 144 | // Veraenderung1: timeuntilnextwave von 5.0 auf 1.0 abgesetzt |
---|
| 145 | this->timeUntilNextWave_ = 1.0; |
---|
[10629] | 146 | this->waveSize_ = 0; |
---|
[10258] | 147 | this->setWaveNumber(0); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | // Generates a TowerDefenseEnemy. Uses Template "enemytowerdefense". Sets position at first waypoint of path. |
---|
[10629] | 151 | void TowerDefense::addTowerDefenseEnemy(int templatenr) |
---|
| 152 | { |
---|
[10258] | 153 | TowerDefenseEnemy* en1 = new TowerDefenseEnemy(this->center_->getContext()); |
---|
| 154 | |
---|
| 155 | switch(templatenr) |
---|
[9272] | 156 | { |
---|
[10258] | 157 | case 1 : |
---|
| 158 | en1->addTemplate("enemytowerdefense1"); |
---|
| 159 | en1->setScale(3); |
---|
[10629] | 160 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 161 | break; |
---|
| 162 | |
---|
| 163 | case 2 : |
---|
| 164 | en1->addTemplate("enemytowerdefense2"); |
---|
| 165 | en1->setScale(2); |
---|
[10629] | 166 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 167 | break; |
---|
| 168 | |
---|
| 169 | case 3 : |
---|
| 170 | en1->addTemplate("enemytowerdefense3"); |
---|
| 171 | en1->setScale(1); |
---|
[10629] | 172 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 173 | break; |
---|
[10629] | 174 | } |
---|
[9272] | 175 | |
---|
[10622] | 176 | en1->setTeam(2); |
---|
[9272] | 177 | |
---|
[10629] | 178 | WaypointController* controller = (WaypointController*)(en1->getXMLController()); |
---|
| 179 | |
---|
[11071] | 180 | if (controller != nullptr && waypoints_.size() > 1) |
---|
[10258] | 181 | { |
---|
[10629] | 182 | en1->setPosition(waypoints_.at(0)->getPosition() + offset_); |
---|
| 183 | en1->setOrientation(Vector3(0,0,10), Degree(0)); |
---|
| 184 | en1->setDirection(Vector3(0,1,0)); |
---|
| 185 | en1->lookAt(waypoints_.at(1)->getPosition() + offset_); |
---|
| 186 | |
---|
[11071] | 187 | for (TowerDefenseField* field : waypoints_) |
---|
[10629] | 188 | { |
---|
| 189 | orxonox::WeakPtr<MovableEntity> waypoint = new MovableEntity(this->center_->getContext()); |
---|
[11071] | 190 | waypoint->setPosition(field->getPosition() + offset_); |
---|
[10629] | 191 | controller->addWaypoint(waypoint); |
---|
| 192 | } |
---|
[10258] | 193 | } |
---|
[10629] | 194 | |
---|
| 195 | enemies_.push_back(en1); |
---|
[9272] | 196 | } |
---|
| 197 | |
---|
[10258] | 198 | |
---|
[9272] | 199 | void TowerDefense::end() |
---|
[9123] | 200 | { |
---|
[11716] | 201 | if (Highscore::exists()) |
---|
| 202 | { |
---|
| 203 | int score = this->getWaveNumber(); |
---|
| 204 | Highscore::getInstance().storeScore("Tower Defense", score, this->getPlayer()); |
---|
| 205 | } |
---|
[10622] | 206 | TeamDeathmatch::end(); |
---|
[10258] | 207 | ChatManager::message("Match is over! Gameover!"); |
---|
[9272] | 208 | |
---|
| 209 | } |
---|
| 210 | |
---|
[10622] | 211 | void TowerDefense::spawnPlayer(PlayerInfo* player) |
---|
| 212 | { |
---|
| 213 | assert(player); |
---|
[10629] | 214 | player_ = player; |
---|
[10622] | 215 | |
---|
[11071] | 216 | if (selecter->getPlayer() == nullptr) |
---|
[10622] | 217 | { |
---|
[10629] | 218 | player_->startControl(selecter); |
---|
[10622] | 219 | players_[player].state_ = PlayerState::Alive; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | @brief |
---|
| 225 | Get the player. |
---|
| 226 | @return |
---|
[11071] | 227 | Returns a pointer to the player. If there is no player, nullptr is returned. |
---|
[10622] | 228 | */ |
---|
| 229 | PlayerInfo* TowerDefense::getPlayer(void) const |
---|
| 230 | { |
---|
| 231 | return this->player_; |
---|
| 232 | } |
---|
| 233 | |
---|
[10258] | 234 | //not working yet |
---|
[10629] | 235 | void TowerDefense::addTower(int x,int y) |
---|
[10622] | 236 | { |
---|
| 237 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
| 238 | x = coord->GetX(); |
---|
| 239 | y = coord->GetY(); |
---|
[10258] | 240 | |
---|
[10629] | 241 | int cost = center_->getTowerCost(1); |
---|
[10258] | 242 | |
---|
[10629] | 243 | if (fields_[x][y]->isFree() == true && getCredit() >= cost) |
---|
[10258] | 244 | { |
---|
[12018] | 245 | // Veraenderung 2 Paycredit von cost auf 0 gesetzt |
---|
| 246 | payCredit(0); |
---|
[10629] | 247 | fields_[x][y]->createTower(1); |
---|
[10258] | 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /*adds Tower at Position (x,y) and reduces credit and adds the point to the towermatrix. template ("towerturret") |
---|
| 252 | so towers have ability if the turrets |
---|
[10622] | 253 | */ |
---|
[10258] | 254 | |
---|
[10629] | 255 | void TowerDefense::upgradeTower(int x, int y) |
---|
| 256 | { |
---|
[10622] | 257 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
| 258 | x = coord->GetX(); |
---|
[10629] | 259 | y = coord->GetY(); |
---|
[9272] | 260 | |
---|
[10629] | 261 | int cost = center_->getTowerCost(fields_[x][y]->getUpgrade() + 1); |
---|
[10622] | 262 | |
---|
[10629] | 263 | if (fields_[x][y]->isFree() == false && fields_[x][y]->canUpgrade() == true && getCredit() >= cost) |
---|
[9272] | 264 | { |
---|
[12018] | 265 | // Veraenderung 3 Paycredit von cost auf 0 gesetzt |
---|
| 266 | payCredit(0); |
---|
[10629] | 267 | fields_[x][y]->upgrade(); |
---|
[9272] | 268 | } |
---|
[10622] | 269 | } |
---|
| 270 | |
---|
[10258] | 271 | void TowerDefense::tick(float dt) |
---|
[9272] | 272 | { |
---|
[10258] | 273 | SUPER(TowerDefense, tick, dt); |
---|
| 274 | |
---|
[11071] | 275 | if (hasStarted() == false || player_ == nullptr) |
---|
[10622] | 276 | { |
---|
[10629] | 277 | return; |
---|
| 278 | } |
---|
| 279 | timeUntilNextWave_ -= dt; |
---|
| 280 | timeSinceLastSpawn_ += dt; |
---|
[10622] | 281 | |
---|
[10629] | 282 | //build/upgrade tower at selecter position |
---|
[11071] | 283 | if (selecter != nullptr && selecter->buildTower_ == true) |
---|
[10629] | 284 | { |
---|
| 285 | selecter->buildTower_ = false; |
---|
[10622] | 286 | |
---|
[10629] | 287 | if (getField(selecter->selectedPos_)->canUpgrade() == true) |
---|
| 288 | { |
---|
| 289 | upgradeTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
| 290 | } |
---|
| 291 | else |
---|
| 292 | { |
---|
| 293 | addTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
| 294 | } |
---|
[10622] | 295 | } |
---|
[10629] | 296 | |
---|
[11071] | 297 | for (std::list<WeakPtr<TowerDefenseEnemy>>::iterator it = enemies_.begin(); it != enemies_.end(); ) |
---|
[10258] | 298 | { |
---|
[11071] | 299 | if (*it == nullptr) |
---|
[10258] | 300 | { |
---|
[10629] | 301 | // the enemy was destroyed by a tower - remove it from the list |
---|
| 302 | enemies_.erase(it++); |
---|
| 303 | addCredit(1); |
---|
| 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
| 307 | //if ships are at the end they get destroyed |
---|
| 308 | Vector3 ship = (*it)->getRVWorldPosition(); |
---|
| 309 | float distance = ship.distance(endpoint_); |
---|
| 310 | if(distance < 40) |
---|
| 311 | { |
---|
| 312 | (*it)->destroy(); |
---|
| 313 | enemies_.erase(it++); |
---|
[10258] | 314 | this->reduceLifes(1); |
---|
[10629] | 315 | if (this->getLifes() == 0) |
---|
[10258] | 316 | { |
---|
| 317 | this->end(); |
---|
| 318 | } |
---|
| 319 | } |
---|
[10629] | 320 | else |
---|
| 321 | { |
---|
| 322 | ++ it; |
---|
| 323 | } |
---|
[10258] | 324 | } |
---|
[10629] | 325 | } |
---|
[10622] | 326 | |
---|
[10629] | 327 | // Add new enemy? |
---|
| 328 | if (timeSinceLastSpawn_ >= 1.0 && waveSize_ > 0) |
---|
[10258] | 329 | { |
---|
[10629] | 330 | // Add new enemy |
---|
| 331 | timeSinceLastSpawn_ -= 1.0; |
---|
| 332 | -- waveSize_; |
---|
| 333 | addTowerDefenseEnemy(this->getWaveNumber() % 3 + 1); |
---|
[10258] | 334 | } |
---|
[10629] | 335 | else if (timeUntilNextWave_ <= 0.0 && waveSize_ <= 0) |
---|
| 336 | { |
---|
| 337 | //New wave |
---|
| 338 | ++ waveNumber_; |
---|
| 339 | timeSinceLastSpawn_ = 0.0; |
---|
[10258] | 340 | |
---|
[10727] | 341 | timeUntilNextWave_ = waveNumber_+20.0f; |
---|
[10629] | 342 | waveSize_ = waveNumber_+5; |
---|
| 343 | } |
---|
[10622] | 344 | |
---|
[10629] | 345 | } |
---|
| 346 | |
---|
| 347 | void TowerDefense::createFields() |
---|
| 348 | { |
---|
| 349 | assert(center_); |
---|
| 350 | TDCoordinate coord(0,0); |
---|
| 351 | TDCoordinate startCoord(0,0); |
---|
| 352 | std::string fields = center_->getFields(); |
---|
| 353 | int pos = 0; |
---|
| 354 | for (int j = 15; j >= 0; --j) |
---|
[10258] | 355 | { |
---|
[10629] | 356 | for (int i = 0; i < 16; ++i) |
---|
[10258] | 357 | { |
---|
[10629] | 358 | coord.Set(i,j); |
---|
| 359 | fields_[i][j] = new TowerDefenseField(center_->getContext()); |
---|
| 360 | fields_[i][j]->setCenterpoint(center_); |
---|
| 361 | center_->attach(fields_[i][j]); |
---|
| 362 | fields_[i][j]->setPosition(coord.get3dcoordinate()); |
---|
| 363 | fields_[i][j]->create(fields.at(pos), fields.at(pos+1)); |
---|
| 364 | pos += 2; |
---|
[11071] | 365 | if (fields_[i][j]->getType() == TowerDefenseFieldType::START) |
---|
[10629] | 366 | { |
---|
| 367 | startCoord.Set(i,j); |
---|
| 368 | waypoints_.push_back(fields_[i][j]); |
---|
| 369 | } |
---|
[10258] | 370 | } |
---|
| 371 | } |
---|
[10629] | 372 | //Place waypoints along the street for the waypoint controllers of the enemies |
---|
| 373 | TDCoordinate* thisCoord = &startCoord; |
---|
| 374 | TDCoordinate* nextCoord; |
---|
[11071] | 375 | while ((nextCoord = getNextStreetCoord(thisCoord)) != nullptr) |
---|
[10629] | 376 | { |
---|
| 377 | waypoints_.push_back(fields_[nextCoord->GetX()][nextCoord->GetY()]); |
---|
| 378 | thisCoord = nextCoord; |
---|
| 379 | endpoint_ = nextCoord->get3dcoordinate(); |
---|
| 380 | } |
---|
[10258] | 381 | |
---|
[9123] | 382 | } |
---|
[9148] | 383 | |
---|
[10629] | 384 | TDCoordinate* TowerDefense::getNextStreetCoord(TDCoordinate* thisCoord) |
---|
| 385 | { |
---|
| 386 | TowerDefenseField* thisField = fields_[thisCoord->GetX()][thisCoord->GetY()]; |
---|
[10622] | 387 | |
---|
[11071] | 388 | if (thisField->getType() != TowerDefenseFieldType::STREET && thisField->getType() != TowerDefenseFieldType::START) |
---|
[10629] | 389 | { |
---|
[11071] | 390 | return nullptr; |
---|
[10629] | 391 | } |
---|
[9272] | 392 | |
---|
[11083] | 393 | TDCoordinate* nextCoord = new TDCoordinate(0, 0); |
---|
| 394 | |
---|
[10629] | 395 | if (thisField->getAngle() == 0) |
---|
[9272] | 396 | { |
---|
[10629] | 397 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() - 1); |
---|
[9272] | 398 | } |
---|
[10629] | 399 | else if (thisField->getAngle() == 1) |
---|
| 400 | { |
---|
| 401 | nextCoord->Set(thisCoord->GetX() + 1, thisCoord->GetY()); |
---|
| 402 | } |
---|
| 403 | else if (thisField->getAngle() == 2) |
---|
| 404 | { |
---|
| 405 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() + 1); |
---|
| 406 | } |
---|
| 407 | else if (thisField->getAngle() == 3) |
---|
| 408 | { |
---|
| 409 | nextCoord->Set(thisCoord->GetX() - 1, thisCoord->GetY()); |
---|
| 410 | } |
---|
[9272] | 411 | |
---|
[10629] | 412 | if (thisCoord->GetX() != nextCoord->GetX() || thisCoord->GetY() != nextCoord->GetY()) |
---|
[9272] | 413 | { |
---|
[10629] | 414 | return nextCoord; |
---|
[9272] | 415 | } |
---|
| 416 | |
---|
[11083] | 417 | delete nextCoord; |
---|
[11071] | 418 | return nullptr; |
---|
[9272] | 419 | } |
---|
[9098] | 420 | } |
---|