[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 | * |
---|
| 22 | * Author: |
---|
| 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 | * |
---|
| 40 | *TIPP: Eclipse hilft euch schnell auf bereits vorhanden Funktionen zuzugreifen: |
---|
| 41 | * einfach "this->" eingeben und kurz warten. Dann tauch eine Liste mit Vorschlägen auf. Wenn ihr jetzt weiter |
---|
| 42 | * tippt, werden die Vorschläge entsprechend gefiltert. |
---|
| 43 | * |
---|
| 44 | * |
---|
| 45 | *TIPP: schaut euch mal Tetris::createStone() an. Dort wird ein TetrisStone-Objekt (ControllableEntity) erzeugt, |
---|
| 46 | * ihm ein Template zugewiesen (welches vorher im Level definiert wurde und dem CenterPoint übergeben wurde) |
---|
| 47 | * Ähnlich könnt ihr vorgehen, um einen Turm zu erzeugen. (Zusätzlich braucht ein Turm noch einen Controller) |
---|
| 48 | * z.B: WaypointPatrolController. Wenn kein Team zugewiesen wurde bekämpft ein WaypointPatrolController alles, |
---|
| 49 | * was in seiner Reichweite liegt. |
---|
| 50 | * |
---|
[9143] | 51 | * |
---|
| 52 | *HUD: |
---|
| 53 | * Ein Gametype kann ein HUD (Head up Display haben.) Z.B: hat Pong eine Anzeige welcher Spieler wieviele Punkte hat. |
---|
| 54 | * Generell kann man a) Grafiken oder b) Zeichen in einer HUD anzeigen. |
---|
| 55 | * Fuer den ersten Schritt reicht reiner Text. |
---|
| 56 | * |
---|
| 57 | * a) |
---|
| 58 | * PongScore.cc uebernehmen und eigene Klasse draus machen. |
---|
| 59 | * Wenn ihr bloss anzeigen wollt wieviele Punkte der Spieler bereits erspielt hat (Punkte = Kapital fuer neue Tuerme) dann orientiert ihr euch an |
---|
| 60 | * TetrisScore.cc (im pCuts branch): http://www.orxonox.net/browser/code/branches/pCuts/src/modules/tetris/TetrisScore.cc |
---|
| 61 | * Ich habe TetrisScore lediglich dazu gebraucht, um eine Variable auf dem HUD auszugeben. Ein Objekt fuer statischen Text gibt es bereits. |
---|
| 62 | * |
---|
| 63 | * b) |
---|
| 64 | * Im naesten Schritt erstellt man die Vorlage fuer das HUD-Objekt: siehe /data/overlays/pongHUD |
---|
| 65 | * OverlayText ist eine Vorlage fuer statischen text zb: "Points Scored:". Aus mir nicht erklaerlichen Gruenden sollte man die OverlayText |
---|
| 66 | * Objekte immer erst nach dem PongScore anlegen. |
---|
| 67 | * |
---|
[9180] | 68 | * c) Im TowerDefense gamtype muss im Constructor noch das HUD-Template gesetzt werden. |
---|
[9143] | 69 | * |
---|
| 70 | * d) in CMakeLists.txt noch das Module includen das fuer die Overlays zustaendig ist. Siehe das gleiche File im Pong module. |
---|
| 71 | * |
---|
| 72 | * |
---|
| 73 | * |
---|
[9098] | 74 | */ |
---|
| 75 | |
---|
[9112] | 76 | #include "TowerDefense.h" |
---|
[10142] | 77 | #include "TowerDefenseTower.h" |
---|
[10091] | 78 | #include "TowerTurret.h" |
---|
[9137] | 79 | #include "TowerDefenseCenterpoint.h" |
---|
[10105] | 80 | //#include "TDCoordinate.h" |
---|
[9122] | 81 | #include "worldentities/SpawnPoint.h" |
---|
| 82 | #include "worldentities/pawns/Pawn.h" |
---|
[9129] | 83 | #include "worldentities/pawns/SpaceShip.h" |
---|
[9142] | 84 | #include "controllers/WaypointController.h" |
---|
[9272] | 85 | |
---|
[9142] | 86 | #include "graphics/Model.h" |
---|
| 87 | #include "infos/PlayerInfo.h" |
---|
[9138] | 88 | #include "chat/ChatManager.h" |
---|
[9667] | 89 | #include "core/CoreIncludes.h" |
---|
[9138] | 90 | |
---|
[9136] | 91 | /* Part of a temporary hack to allow the player to add towers */ |
---|
| 92 | #include "core/command/ConsoleCommand.h" |
---|
| 93 | |
---|
[9112] | 94 | namespace orxonox |
---|
| 95 | { |
---|
[9667] | 96 | RegisterUnloadableClass(TowerDefense); |
---|
[9272] | 97 | |
---|
[9667] | 98 | TowerDefense::TowerDefense(Context* context) : Deathmatch(context) |
---|
[9112] | 99 | { |
---|
| 100 | RegisterObject(TowerDefense); |
---|
[9272] | 101 | |
---|
[9180] | 102 | this->setHUDTemplate("TowerDefenseHUD"); |
---|
| 103 | |
---|
[9272] | 104 | this->stats_ = new TowerDefensePlayerStats(); |
---|
[9207] | 105 | |
---|
[9272] | 106 | /* Temporary hack to allow the player to add towers */ |
---|
| 107 | this->dedicatedAddTower_ = createConsoleCommand( "addTower", createExecutor( createFunctor(&TowerDefense::addTower, this) ) ); |
---|
[9112] | 108 | } |
---|
[9272] | 109 | |
---|
[9136] | 110 | TowerDefense::~TowerDefense() |
---|
| 111 | { |
---|
[9272] | 112 | /* Part of a temporary hack to allow the player to add towers */ |
---|
[9136] | 113 | if (this->isInitialized()) |
---|
| 114 | { |
---|
| 115 | if( this->dedicatedAddTower_ ) |
---|
| 116 | delete this->dedicatedAddTower_; |
---|
| 117 | } |
---|
| 118 | } |
---|
[9272] | 119 | |
---|
| 120 | void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint) |
---|
| 121 | { |
---|
| 122 | orxout() << "Centerpoint now setting..." << endl; |
---|
| 123 | this->center_ = centerpoint; |
---|
| 124 | orxout() << "Centerpoint now set..." << endl; |
---|
| 125 | } |
---|
| 126 | |
---|
[9112] | 127 | void TowerDefense::start() |
---|
| 128 | { |
---|
[10109] | 129 | orxout() << "test0" << endl; |
---|
| 130 | |
---|
[9112] | 131 | Deathmatch::start(); |
---|
[10142] | 132 | |
---|
| 133 | // Waypoints: [1,3] [10,3] [10,11] [13,11] |
---|
| 134 | for (int i=0; i < 16 ; i++){ |
---|
| 135 | for (int j = 0; j< 16 ; j++){ |
---|
| 136 | towermatrix[i][j] = false; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | for (int k=0; k<3; k++) |
---|
| 140 | towermatrix[1][k]=true; |
---|
| 141 | for (int l=1; l<11; l++) |
---|
| 142 | towermatrix[l][3]=true; |
---|
| 143 | for (int m=3; m<12; m++) |
---|
| 144 | towermatrix[10][m]=true; |
---|
| 145 | for (int n=10; n<14; n++) |
---|
| 146 | towermatrix[n][11]=true; |
---|
| 147 | for (int o=13; o<16; o++) |
---|
| 148 | towermatrix[13][o]=true; |
---|
| 149 | |
---|
| 150 | |
---|
[10140] | 151 | credits = 5000; |
---|
| 152 | life = 20; |
---|
| 153 | waves = 0; |
---|
[10132] | 154 | time=0.0; |
---|
| 155 | |
---|
[10142] | 156 | const int kInitialTowerCount = 3; |
---|
[9175] | 157 | |
---|
[9272] | 158 | for (int i = 0; i < kInitialTowerCount; i++) |
---|
| 159 | { |
---|
[10144] | 160 | addTower(i+4,i+5); |
---|
[9272] | 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
[10142] | 164 | |
---|
[10109] | 165 | //add some TowerDefenseEnemys |
---|
| 166 | |
---|
| 167 | |
---|
[10123] | 168 | |
---|
| 169 | |
---|
| 170 | |
---|
[10109] | 171 | //ChatManager::message("Use the console command addTower x y to add towers"); |
---|
| 172 | |
---|
[9272] | 173 | //TODO: let the player control his controllable entity && TODO: create a new ControllableEntity for the player |
---|
| 174 | } |
---|
| 175 | |
---|
[10125] | 176 | // Generates a TowerDefenseEnemy. Uses Template "enemytowerdefense". Sets position at first waypoint of path. |
---|
| 177 | |
---|
[10141] | 178 | void TowerDefense::addTowerDefenseEnemy(std::vector<TDCoordinate*> path, int templatenr){ |
---|
[10105] | 179 | |
---|
[10109] | 180 | TowerDefenseEnemy* en1 = new TowerDefenseEnemy(this->center_->getContext()); |
---|
[10141] | 181 | |
---|
| 182 | switch(templatenr) |
---|
| 183 | { |
---|
| 184 | case 1 : |
---|
| 185 | en1->addTemplate("enemytowerdefense1"); |
---|
| 186 | en1->setScale(3); |
---|
| 187 | break; |
---|
| 188 | case 2 : |
---|
| 189 | en1->addTemplate("enemytowerdefense2"); |
---|
| 190 | en1->setScale(2); |
---|
| 191 | break; |
---|
| 192 | case 3 : |
---|
| 193 | en1->addTemplate("enemytowerdefense3"); |
---|
| 194 | en1->setScale(1); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | |
---|
[10126] | 198 | en1->getController(); |
---|
[10125] | 199 | |
---|
| 200 | en1->setPosition(path.at(0)->get3dcoordinate()); |
---|
[10123] | 201 | |
---|
| 202 | |
---|
| 203 | TowerDefenseEnemyvector.push_back(en1); |
---|
| 204 | |
---|
[10109] | 205 | |
---|
[10123] | 206 | for(unsigned int i = 0; i < path.size(); ++i) |
---|
[10109] | 207 | { |
---|
[10123] | 208 | en1->addWaypoint((path.at(i))); |
---|
| 209 | } |
---|
[10109] | 210 | |
---|
[10123] | 211 | |
---|
[10132] | 212 | /* |
---|
[10123] | 213 | WaypointController *newController = new WaypointController(en1->getContext());// |
---|
[10109] | 214 | newController->setAccuracy(3); |
---|
| 215 | |
---|
| 216 | for(int i =0; i < path.size(); ++i) |
---|
| 217 | { |
---|
| 218 | |
---|
[10123] | 219 | Model *wayPoint = new Model(en1->getContext()); |
---|
| 220 | wayPoint->setMeshSource("cube.mesh"); |
---|
| 221 | wayPoint->setPosition(path.at(i)->get3dcoordinate()); |
---|
[10109] | 222 | wayPoint->setScale(0.2); |
---|
| 223 | newController->addWaypoint(wayPoint); |
---|
| 224 | }*/ |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
[9272] | 228 | void TowerDefense::end() |
---|
[10123] | 229 | |
---|
[9123] | 230 | { |
---|
[9272] | 231 | Deathmatch::end(); |
---|
| 232 | |
---|
| 233 | ChatManager::message("Match is over"); |
---|
| 234 | } |
---|
| 235 | |
---|
[10105] | 236 | |
---|
| 237 | |
---|
[9272] | 238 | void TowerDefense::addTower(int x, int y) |
---|
[10142] | 239 | { |
---|
[10123] | 240 | const TowerCost towerCost = TDDefaultTowerCost; |
---|
[9272] | 241 | |
---|
| 242 | if (!this->hasEnoughCreditForTower(towerCost)) |
---|
| 243 | { |
---|
| 244 | orxout() << "not enough credit: " << (this->stats_->getCredit()) << " available, " << TDDefaultTowerCost << " needed."; |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | |
---|
[10142] | 248 | if (towermatrix [x][y]==true) |
---|
[9272] | 249 | { |
---|
[10142] | 250 | orxout() << "not possible to put tower here!!" << endl; |
---|
[9272] | 251 | return; |
---|
| 252 | } |
---|
| 253 | |
---|
[10109] | 254 | |
---|
[9272] | 255 | unsigned int width = this->center_->getWidth(); |
---|
| 256 | unsigned int height = this->center_->getHeight(); |
---|
| 257 | |
---|
[10109] | 258 | |
---|
[9272] | 259 | int tileScale = (int) this->center_->getTileScale(); |
---|
| 260 | |
---|
| 261 | if (x > 15 || y > 15 || x < 0 || y < 0) |
---|
| 262 | { |
---|
| 263 | //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet) |
---|
| 264 | orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl; |
---|
| 265 | return; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl; |
---|
| 269 | |
---|
[10142] | 270 | |
---|
[10105] | 271 | |
---|
[10142] | 272 | //Reduce credit |
---|
| 273 | this->stats_->buyTower(towerCost); |
---|
[10105] | 274 | |
---|
[10142] | 275 | orxout() << "Credit: " << this->stats_->getCredit() << endl; |
---|
[9272] | 276 | |
---|
[10142] | 277 | // Create tower |
---|
| 278 | TowerDefenseTower* towernew = new TowerDefenseTower(this->center_->getContext()); |
---|
[10144] | 279 | towernew->addTemplate("towerturret"); |
---|
[10142] | 280 | towernew->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 75); |
---|
| 281 | towernew->setGame(this); |
---|
[9272] | 282 | |
---|
[10142] | 283 | towermatrix[x][y]=true; |
---|
[9272] | 284 | } |
---|
| 285 | |
---|
| 286 | bool TowerDefense::hasEnoughCreditForTower(TowerCost towerCost) |
---|
| 287 | { |
---|
| 288 | return ((this->stats_->getCredit()) >= towerCost); |
---|
| 289 | } |
---|
| 290 | |
---|
[10142] | 291 | |
---|
[9272] | 292 | void TowerDefense::tick(float dt) |
---|
| 293 | { |
---|
[10123] | 294 | SUPER(TowerDefense, tick, dt); |
---|
[10132] | 295 | time +=dt; |
---|
[10123] | 296 | |
---|
[10132] | 297 | TDCoordinate* coord1 = new TDCoordinate(1,1); |
---|
| 298 | std::vector<TDCoordinate*> path; |
---|
| 299 | path.push_back(coord1); |
---|
[10123] | 300 | |
---|
[10132] | 301 | if(time>1 && TowerDefenseEnemyvector.size() < 30) |
---|
| 302 | { |
---|
[10141] | 303 | addTowerDefenseEnemy(path, rand() %3 +1 ); |
---|
[10132] | 304 | time = time-1; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | |
---|
[9123] | 308 | } |
---|
[9148] | 309 | |
---|
[9272] | 310 | // Function to test if we can add waypoints using code only. Doesn't work yet |
---|
| 311 | |
---|
| 312 | // THE PROBLEM: WaypointController's getControllableEntity() returns null, so it won't track. How do we get the controlableEntity to NOT BE NULL??? |
---|
| 313 | /* |
---|
| 314 | void TowerDefense::addWaypointsAndFirstEnemy() |
---|
| 315 | { |
---|
| 316 | SpaceShip *newShip = new SpaceShip(this->center_); |
---|
| 317 | newShip->addTemplate("spaceshipassff"); |
---|
| 318 | |
---|
| 319 | WaypointController *newController = new WaypointController(newShip); |
---|
| 320 | newController->setAccuracy(3); |
---|
| 321 | |
---|
| 322 | Model *wayPoint1 = new Model(newController); |
---|
| 323 | wayPoint1->setMeshSource("crate.mesh"); |
---|
| 324 | wayPoint1->setPosition(7,-7,5); |
---|
| 325 | wayPoint1->setScale(0.2); |
---|
| 326 | |
---|
| 327 | Model *wayPoint2 = new Model(newController); |
---|
| 328 | wayPoint2->setMeshSource("crate.mesh"); |
---|
| 329 | wayPoint2->setPosition(7,7,5); |
---|
| 330 | wayPoint2->setScale(0.2); |
---|
| 331 | |
---|
| 332 | newController->addWaypoint(wayPoint1); |
---|
| 333 | newController->addWaypoint(wayPoint2); |
---|
| 334 | |
---|
| 335 | // The following line causes the game to crash |
---|
| 336 | |
---|
| 337 | newShip->setController(newController); |
---|
| 338 | // newController -> getPlayer() -> startControl(newShip); |
---|
| 339 | newShip->setPosition(-7,-7,5); |
---|
| 340 | newShip->setScale(0.1); |
---|
| 341 | //newShip->addSpeed(1); |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | |
---|
| 345 | // this->center_->attach(newShip); |
---|
| 346 | } |
---|
| 347 | */ |
---|
| 348 | /* |
---|
| 349 | void TowerDefense::playerEntered(PlayerInfo* player) |
---|
| 350 | { |
---|
| 351 | Deathmatch::playerEntered(player); |
---|
| 352 | |
---|
| 353 | const std::string& message = player->getName() + " entered the game"; |
---|
| 354 | ChatManager::message(message); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | bool TowerDefense::playerLeft(PlayerInfo* player) |
---|
| 358 | { |
---|
| 359 | bool valid_player = Deathmatch::playerLeft(player); |
---|
| 360 | |
---|
| 361 | if (valid_player) |
---|
| 362 | { |
---|
| 363 | const std::string& message = player->getName() + " left the game"; |
---|
| 364 | ChatManager::message(message); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | return valid_player; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | void TowerDefense::pawnKilled(Pawn* victim, Pawn* killer) |
---|
| 372 | { |
---|
| 373 | if (victim && victim->getPlayer()) |
---|
| 374 | { |
---|
| 375 | std::string message; |
---|
| 376 | if (killer) |
---|
| 377 | { |
---|
| 378 | if (killer->getPlayer()) |
---|
| 379 | message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName(); |
---|
| 380 | else |
---|
| 381 | message = victim->getPlayer()->getName() + " was killed"; |
---|
| 382 | } |
---|
| 383 | else |
---|
| 384 | message = victim->getPlayer()->getName() + " died"; |
---|
| 385 | |
---|
| 386 | ChatManager::message(message); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | Deathmatch::pawnKilled(victim, killer); |
---|
| 390 | } |
---|
| 391 | |
---|
[9347] | 392 | void TowerDefense::playerScored(PlayerInfo* player, int score) |
---|
[9272] | 393 | { |
---|
[9347] | 394 | Gametype::playerScored(player, score); |
---|
[9272] | 395 | }*/ |
---|
[9098] | 396 | } |
---|