[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" |
---|
[9136] | 83 | |
---|
[9112] | 84 | namespace orxonox |
---|
| 85 | { |
---|
[9667] | 86 | RegisterUnloadableClass(TowerDefense); |
---|
[9272] | 87 | |
---|
[10622] | 88 | TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context) |
---|
[9112] | 89 | { |
---|
| 90 | RegisterObject(TowerDefense); |
---|
[9272] | 91 | |
---|
[10622] | 92 | selecter = NULL; |
---|
| 93 | this->player_ = NULL; |
---|
[9180] | 94 | this->setHUDTemplate("TowerDefenseHUD"); |
---|
[10629] | 95 | this->waveNumber_ = 0; |
---|
| 96 | this->timeSinceLastSpawn_ = 0.0; |
---|
| 97 | this->timeUntilNextWave_ = 0.0; |
---|
[10622] | 98 | this->credit_ = 0; |
---|
| 99 | this->lifes_ = 0; |
---|
[10629] | 100 | this->waveSize_ = 0; |
---|
| 101 | offset_ = Vector3(0,0,10); |
---|
[9112] | 102 | } |
---|
[9272] | 103 | |
---|
[9136] | 104 | TowerDefense::~TowerDefense() |
---|
| 105 | { |
---|
| 106 | if (this->isInitialized()) |
---|
| 107 | { |
---|
[10629] | 108 | |
---|
[9136] | 109 | } |
---|
| 110 | } |
---|
[9272] | 111 | |
---|
| 112 | void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint) |
---|
| 113 | { |
---|
| 114 | this->center_ = centerpoint; |
---|
| 115 | } |
---|
| 116 | |
---|
[9112] | 117 | void TowerDefense::start() |
---|
[10629] | 118 | { |
---|
[10622] | 119 | if (center_ != NULL) // There needs to be a TowerDefenseCenterpoint, i.e. the area the game takes place. |
---|
| 120 | { |
---|
| 121 | if (selecter == NULL) |
---|
| 122 | { |
---|
| 123 | selecter = new TowerDefenseSelecter(this->center_->getContext()); |
---|
| 124 | } |
---|
| 125 | selecter->addTemplate(center_->getSelecterTemplate()); |
---|
| 126 | center_->attach(selecter); |
---|
| 127 | } |
---|
| 128 | else // If no centerpoint was specified, an error is thrown and the level is exited. |
---|
| 129 | { |
---|
| 130 | orxout(internal_error) << "Jump: No Centerpoint specified." << endl; |
---|
| 131 | return; |
---|
| 132 | } |
---|
[10258] | 133 | |
---|
[10629] | 134 | enemies_.clear(); |
---|
| 135 | |
---|
| 136 | createFields(); |
---|
[10622] | 137 | TeamDeathmatch::start(); |
---|
[9143] | 138 | |
---|
[10258] | 139 | //set initial credits, lifes and WaveNumber |
---|
[10629] | 140 | this->setCredit(1000); |
---|
| 141 | this->setLifes(100); |
---|
| 142 | this->timeSinceLastSpawn_ = 0.0; |
---|
| 143 | this->timeUntilNextWave_ = 5.0; |
---|
| 144 | this->waveSize_ = 0; |
---|
[10258] | 145 | this->setWaveNumber(0); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | // Generates a TowerDefenseEnemy. Uses Template "enemytowerdefense". Sets position at first waypoint of path. |
---|
[10629] | 149 | void TowerDefense::addTowerDefenseEnemy(int templatenr) |
---|
| 150 | { |
---|
[10258] | 151 | TowerDefenseEnemy* en1 = new TowerDefenseEnemy(this->center_->getContext()); |
---|
| 152 | |
---|
| 153 | switch(templatenr) |
---|
[9272] | 154 | { |
---|
[10258] | 155 | case 1 : |
---|
| 156 | en1->addTemplate("enemytowerdefense1"); |
---|
| 157 | en1->setScale(3); |
---|
[10629] | 158 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 159 | break; |
---|
| 160 | |
---|
| 161 | case 2 : |
---|
| 162 | en1->addTemplate("enemytowerdefense2"); |
---|
| 163 | en1->setScale(2); |
---|
[10629] | 164 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 165 | break; |
---|
| 166 | |
---|
| 167 | case 3 : |
---|
| 168 | en1->addTemplate("enemytowerdefense3"); |
---|
| 169 | en1->setScale(1); |
---|
[10629] | 170 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
[10258] | 171 | break; |
---|
[10629] | 172 | } |
---|
[9272] | 173 | |
---|
[10622] | 174 | en1->setTeam(2); |
---|
[9272] | 175 | |
---|
[10629] | 176 | WaypointController* controller = (WaypointController*)(en1->getXMLController()); |
---|
| 177 | |
---|
| 178 | if (controller != NULL && waypoints_.size() > 1) |
---|
[10258] | 179 | { |
---|
[10629] | 180 | en1->setPosition(waypoints_.at(0)->getPosition() + offset_); |
---|
| 181 | en1->setOrientation(Vector3(0,0,10), Degree(0)); |
---|
| 182 | en1->setDirection(Vector3(0,1,0)); |
---|
| 183 | en1->lookAt(waypoints_.at(1)->getPosition() + offset_); |
---|
| 184 | |
---|
| 185 | for (unsigned int i = 0; i < waypoints_.size(); ++ i) |
---|
| 186 | { |
---|
| 187 | orxonox::WeakPtr<MovableEntity> waypoint = new MovableEntity(this->center_->getContext()); |
---|
| 188 | waypoint->setPosition(waypoints_.at(i)->getPosition() + offset_); |
---|
| 189 | controller->addWaypoint(waypoint); |
---|
| 190 | } |
---|
[10258] | 191 | } |
---|
[10629] | 192 | |
---|
| 193 | enemies_.push_back(en1); |
---|
[9272] | 194 | } |
---|
| 195 | |
---|
[10258] | 196 | |
---|
[9272] | 197 | void TowerDefense::end() |
---|
[9123] | 198 | { |
---|
[10258] | 199 | |
---|
[10622] | 200 | TeamDeathmatch::end(); |
---|
[10258] | 201 | ChatManager::message("Match is over! Gameover!"); |
---|
[9272] | 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
[10622] | 205 | void TowerDefense::spawnPlayer(PlayerInfo* player) |
---|
| 206 | { |
---|
| 207 | assert(player); |
---|
[10629] | 208 | player_ = player; |
---|
[10622] | 209 | |
---|
| 210 | if (selecter->getPlayer() == NULL) |
---|
| 211 | { |
---|
[10629] | 212 | player_->startControl(selecter); |
---|
[10622] | 213 | players_[player].state_ = PlayerState::Alive; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | @brief |
---|
| 219 | Get the player. |
---|
| 220 | @return |
---|
| 221 | Returns a pointer to the player. If there is no player, NULL is returned. |
---|
| 222 | */ |
---|
| 223 | PlayerInfo* TowerDefense::getPlayer(void) const |
---|
| 224 | { |
---|
| 225 | return this->player_; |
---|
| 226 | } |
---|
| 227 | |
---|
[10258] | 228 | //not working yet |
---|
[10629] | 229 | void TowerDefense::addTower(int x,int y) |
---|
[10622] | 230 | { |
---|
| 231 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
| 232 | x = coord->GetX(); |
---|
| 233 | y = coord->GetY(); |
---|
[10258] | 234 | |
---|
[10629] | 235 | int cost = center_->getTowerCost(1); |
---|
[10258] | 236 | |
---|
[10629] | 237 | if (fields_[x][y]->isFree() == true && getCredit() >= cost) |
---|
[10258] | 238 | { |
---|
[10629] | 239 | payCredit(cost); |
---|
| 240 | fields_[x][y]->createTower(1); |
---|
[10258] | 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /*adds Tower at Position (x,y) and reduces credit and adds the point to the towermatrix. template ("towerturret") |
---|
| 245 | so towers have ability if the turrets |
---|
[10622] | 246 | */ |
---|
[10258] | 247 | |
---|
[10629] | 248 | void TowerDefense::upgradeTower(int x, int y) |
---|
| 249 | { |
---|
[10622] | 250 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
| 251 | x = coord->GetX(); |
---|
[10629] | 252 | y = coord->GetY(); |
---|
[9272] | 253 | |
---|
[10629] | 254 | int cost = center_->getTowerCost(fields_[x][y]->getUpgrade() + 1); |
---|
[10622] | 255 | |
---|
[10629] | 256 | if (fields_[x][y]->isFree() == false && fields_[x][y]->canUpgrade() == true && getCredit() >= cost) |
---|
[9272] | 257 | { |
---|
[10629] | 258 | payCredit(cost); |
---|
| 259 | fields_[x][y]->upgrade(); |
---|
[9272] | 260 | } |
---|
[10622] | 261 | } |
---|
| 262 | |
---|
[10258] | 263 | void TowerDefense::tick(float dt) |
---|
[9272] | 264 | { |
---|
[10258] | 265 | SUPER(TowerDefense, tick, dt); |
---|
| 266 | |
---|
[10629] | 267 | if (hasStarted() == false || player_ == NULL) |
---|
[10622] | 268 | { |
---|
[10629] | 269 | return; |
---|
| 270 | } |
---|
| 271 | timeUntilNextWave_ -= dt; |
---|
| 272 | timeSinceLastSpawn_ += dt; |
---|
[10622] | 273 | |
---|
[10629] | 274 | //build/upgrade tower at selecter position |
---|
| 275 | if (selecter != NULL && selecter->buildTower_ == true) |
---|
| 276 | { |
---|
| 277 | selecter->buildTower_ = false; |
---|
[10622] | 278 | |
---|
[10629] | 279 | if (getField(selecter->selectedPos_)->canUpgrade() == true) |
---|
| 280 | { |
---|
| 281 | upgradeTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | addTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
| 286 | } |
---|
[10622] | 287 | } |
---|
[10629] | 288 | |
---|
| 289 | for (std::list<WeakPtr<TowerDefenseEnemy> >::iterator it = enemies_.begin(); it != enemies_.end(); ) |
---|
[10258] | 290 | { |
---|
[10629] | 291 | if (*it == NULL) |
---|
[10258] | 292 | { |
---|
[10629] | 293 | // the enemy was destroyed by a tower - remove it from the list |
---|
| 294 | enemies_.erase(it++); |
---|
| 295 | addCredit(1); |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | { |
---|
| 299 | //if ships are at the end they get destroyed |
---|
| 300 | Vector3 ship = (*it)->getRVWorldPosition(); |
---|
| 301 | float distance = ship.distance(endpoint_); |
---|
| 302 | if(distance < 40) |
---|
| 303 | { |
---|
| 304 | (*it)->destroy(); |
---|
| 305 | enemies_.erase(it++); |
---|
[10258] | 306 | this->reduceLifes(1); |
---|
[10629] | 307 | if (this->getLifes() == 0) |
---|
[10258] | 308 | { |
---|
| 309 | this->end(); |
---|
| 310 | } |
---|
| 311 | } |
---|
[10629] | 312 | else |
---|
| 313 | { |
---|
| 314 | ++ it; |
---|
| 315 | } |
---|
[10258] | 316 | } |
---|
[10629] | 317 | } |
---|
[10622] | 318 | |
---|
[10629] | 319 | // Add new enemy? |
---|
| 320 | if (timeSinceLastSpawn_ >= 1.0 && waveSize_ > 0) |
---|
[10258] | 321 | { |
---|
[10629] | 322 | // Add new enemy |
---|
| 323 | timeSinceLastSpawn_ -= 1.0; |
---|
| 324 | -- waveSize_; |
---|
| 325 | addTowerDefenseEnemy(this->getWaveNumber() % 3 + 1); |
---|
[10258] | 326 | } |
---|
[10629] | 327 | else if (timeUntilNextWave_ <= 0.0 && waveSize_ <= 0) |
---|
| 328 | { |
---|
| 329 | //New wave |
---|
| 330 | ++ waveNumber_; |
---|
| 331 | timeSinceLastSpawn_ = 0.0; |
---|
[10258] | 332 | |
---|
[10629] | 333 | timeUntilNextWave_ = waveNumber_+20; |
---|
| 334 | waveSize_ = waveNumber_+5; |
---|
| 335 | } |
---|
[10622] | 336 | |
---|
[10629] | 337 | } |
---|
| 338 | |
---|
| 339 | void TowerDefense::createFields() |
---|
| 340 | { |
---|
| 341 | assert(center_); |
---|
| 342 | TDCoordinate coord(0,0); |
---|
| 343 | TDCoordinate startCoord(0,0); |
---|
| 344 | std::string fields = center_->getFields(); |
---|
| 345 | int pos = 0; |
---|
| 346 | for (int j = 15; j >= 0; --j) |
---|
[10258] | 347 | { |
---|
[10629] | 348 | for (int i = 0; i < 16; ++i) |
---|
[10258] | 349 | { |
---|
[10629] | 350 | coord.Set(i,j); |
---|
| 351 | fields_[i][j] = new TowerDefenseField(center_->getContext()); |
---|
| 352 | fields_[i][j]->setCenterpoint(center_); |
---|
| 353 | center_->attach(fields_[i][j]); |
---|
| 354 | fields_[i][j]->setPosition(coord.get3dcoordinate()); |
---|
| 355 | fields_[i][j]->create(fields.at(pos), fields.at(pos+1)); |
---|
| 356 | pos += 2; |
---|
| 357 | if (fields_[i][j]->getType() == START) |
---|
| 358 | { |
---|
| 359 | startCoord.Set(i,j); |
---|
| 360 | waypoints_.push_back(fields_[i][j]); |
---|
| 361 | } |
---|
[10258] | 362 | } |
---|
| 363 | } |
---|
[10629] | 364 | //Place waypoints along the street for the waypoint controllers of the enemies |
---|
| 365 | TDCoordinate* thisCoord = &startCoord; |
---|
| 366 | TDCoordinate* nextCoord; |
---|
| 367 | while ((nextCoord = getNextStreetCoord(thisCoord)) != NULL) |
---|
| 368 | { |
---|
| 369 | waypoints_.push_back(fields_[nextCoord->GetX()][nextCoord->GetY()]); |
---|
| 370 | thisCoord = nextCoord; |
---|
| 371 | endpoint_ = nextCoord->get3dcoordinate(); |
---|
| 372 | } |
---|
[10258] | 373 | |
---|
[9123] | 374 | } |
---|
[9148] | 375 | |
---|
[10629] | 376 | TDCoordinate* TowerDefense::getNextStreetCoord(TDCoordinate* thisCoord) |
---|
| 377 | { |
---|
| 378 | TowerDefenseField* thisField = fields_[thisCoord->GetX()][thisCoord->GetY()]; |
---|
| 379 | TDCoordinate* nextCoord = new TDCoordinate(0,0); |
---|
[10622] | 380 | |
---|
[10629] | 381 | if (thisField->getType() != STREET && thisField->getType() != START) |
---|
| 382 | { |
---|
| 383 | return NULL; |
---|
| 384 | } |
---|
[9272] | 385 | |
---|
[10629] | 386 | if (thisField->getAngle() == 0) |
---|
[9272] | 387 | { |
---|
[10629] | 388 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() - 1); |
---|
[9272] | 389 | } |
---|
[10629] | 390 | else if (thisField->getAngle() == 1) |
---|
| 391 | { |
---|
| 392 | nextCoord->Set(thisCoord->GetX() + 1, thisCoord->GetY()); |
---|
| 393 | } |
---|
| 394 | else if (thisField->getAngle() == 2) |
---|
| 395 | { |
---|
| 396 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() + 1); |
---|
| 397 | } |
---|
| 398 | else if (thisField->getAngle() == 3) |
---|
| 399 | { |
---|
| 400 | nextCoord->Set(thisCoord->GetX() - 1, thisCoord->GetY()); |
---|
| 401 | } |
---|
[9272] | 402 | |
---|
[10629] | 403 | if (thisCoord->GetX() != nextCoord->GetX() || thisCoord->GetY() != nextCoord->GetY()) |
---|
[9272] | 404 | { |
---|
[10629] | 405 | return nextCoord; |
---|
[9272] | 406 | } |
---|
| 407 | |
---|
[10629] | 408 | return NULL; |
---|
[9272] | 409 | } |
---|
[9098] | 410 | } |
---|