Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseFS15/src/modules/towerdefense/TowerDefense.cc @ 10397

Last change on this file since 10397 was 10397, checked in by fvultier, 9 years ago

Added missmissing #include _.h Now the program compiles without any error.

  • Property svn:eol-style set to native
File size: 15.7 KB
RevLine 
[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 *
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 */
[9112]75#include "TowerDefense.h"
[10258]76#include "TowerDefenseTower.h"
[9137]77#include "TowerDefenseCenterpoint.h"
[10258]78//#include "TDCoordinate.h"
[9122]79#include "worldentities/SpawnPoint.h"
80#include "worldentities/pawns/Pawn.h"
[9129]81#include "worldentities/pawns/SpaceShip.h"
[9142]82#include "controllers/WaypointController.h"
83#include "graphics/Model.h"
84#include "infos/PlayerInfo.h"
[9138]85#include "chat/ChatManager.h"
[9667]86#include "core/CoreIncludes.h"
[9136]87/* Part of a temporary hack to allow the player to add towers */
88#include "core/command/ConsoleCommand.h"
89
[10388]90
[9112]91namespace orxonox
92{
[10351]93    static const std::string __CC_addTower_name  = "addTower";
94    static const std::string __CC_upgradeTower_name = "upgradeTower";
95
96    SetConsoleCommand("TowerDefense", __CC_addTower_name,  &TowerDefense::addTower ).addShortcut().defaultValues(1);
97    SetConsoleCommand("TowerDefense", __CC_upgradeTower_name, &TowerDefense::upgradeTower).addShortcut().defaultValues(0);
98
[9667]99    RegisterUnloadableClass(TowerDefense);
[9272]100
[10368]101    TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context)
[9112]102    {
103        RegisterObject(TowerDefense);
[10258]104/*
105        for (int i=0; i < 16 ; i++){
106            for (int j = 0; j< 16 ; j++){
107                towermatrix[i][j] = NULL;
108            }
109        }*/
[9272]110
[10397]111        selecter = NULL;
[10340]112
113
[9180]114        this->setHUDTemplate("TowerDefenseHUD");
[10368]115        this->nextwaveTimer_.setTimer(10, false, createExecutor(createFunctor(&TowerDefense::nextwave, this)));
116        this->nextwaveTimer_.stopTimer();
117        this->waves_ = 0;
118        this->time = 0;
119        this->credit_ = 0;
120        this->lifes_ = 0;
[9180]121
[10258]122        //this->stats_ = new TowerDefensePlayerStats();
[9207]123
[10351]124        ModifyConsoleCommand(__CC_addTower_name).setObject(this);
125        ModifyConsoleCommand(__CC_upgradeTower_name).setObject(this);
[9112]126    }
[9272]127
[9136]128    TowerDefense::~TowerDefense()
129    {
[9272]130        /* Part of a temporary hack to allow the player to add towers */
[9136]131        if (this->isInitialized())
132        {
[10351]133            ModifyConsoleCommand(__CC_addTower_name).setObject(NULL);
134            ModifyConsoleCommand(__CC_upgradeTower_name).setObject(NULL);
[9136]135        }
136    }
[9272]137
138    void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint)
139    {
140        orxout() << "Centerpoint now setting..." << endl;
141        this->center_ = centerpoint;
142        orxout() << "Centerpoint now set..." << endl;
143    }
144
[9112]145    void TowerDefense::start()
146    {
[10258]147
[10368]148        TeamDeathmatch::start();
[9143]149
[10258]150// Waypoints: [1,3] [10,3] [10,11] [13,11] -> add the points to a matrix so the player cant place towers on the path
[10397]151        for (int i=0; i < 16 ; i++)
152        {
153            for (int j = 0; j< 16 ; j++)
154            {
[10340]155                towerModelMatrix[i][j] = NULL;
156                towerTurretMatrix[i][j] = NULL;
[10258]157            }
158        }
[9175]159
[10397]160        selecter = new TowerDefenseSelecter(this->center_->getContext());
161
[10340]162        Model* dummyModel = new Model(this->center_->getContext());
163
[10319]164        //the path of the spacehips has to be blocked, so that no towers can be build there
[10258]165        for (int k=0; k<3; k++)
[10340]166            towerModelMatrix[1][k]=dummyModel;
[10258]167        for (int l=1; l<11; l++)
[10340]168                towerModelMatrix[l][3]=dummyModel;
[10258]169        for (int m=3; m<12; m++)
[10340]170                towerModelMatrix[10][m]=dummyModel;
[10258]171        for (int n=10; n<14; n++)
[10340]172                towerModelMatrix[n][11]=dummyModel;
[10258]173        for (int o=13; o<16; o++)
[10340]174                towerModelMatrix[13][o]=dummyModel;
[10258]175
[10340]176
[10258]177        //set initial credits, lifes and WaveNumber
[10319]178        this->setCredit(1000);
[10335]179        this->setLifes(100);
[10258]180        this->setWaveNumber(0);
181        time=0.0;
182
[10340]183        /*
[10258]184        //adds initial towers
185        for (int i=0; i <7; i++){
186            addTower(i+3,4);
[10319]187        }
[10340]188                */
[10258]189    }
190
191    // Generates a TowerDefenseEnemy. Uses Template "enemytowerdefense". Sets position at first waypoint of path.
192    void TowerDefense::addTowerDefenseEnemy(std::vector<TDCoordinate*> path, int templatenr){
193
194
195        TowerDefenseEnemy* en1 = new TowerDefenseEnemy(this->center_->getContext());
196       
197        switch(templatenr)
[9272]198        {
[10258]199        case 1 :
200            en1->addTemplate("enemytowerdefense1");
201            en1->setScale(3);
202            en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
203            break;
204
205        case 2 :
206            en1->addTemplate("enemytowerdefense2");
207            en1->setScale(2);
208            en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
209            //  en1->setShieldHealth(en1->getShield() = this->getWaveNumber()*2))
210            break;
211
212        case 3 :
213            en1->addTemplate("enemytowerdefense3");
214            en1->setScale(1);
215            en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
216            break;
[9272]217        }
218
[10335]219        en1->setTeam(2);
[10258]220        en1->getController();
221        en1->setPosition(path.at(0)->get3dcoordinate());
222        TowerDefenseEnemyvector.push_back(en1);
[9272]223
[10258]224        for(unsigned int i = 0; i < path.size(); ++i)
225        {
226            en1->addWaypoint((path.at(i)));
227        }
[9272]228    }
229
[10258]230
[9272]231    void TowerDefense::end()
[9123]232    {
[10258]233
[10368]234        TeamDeathmatch::end();
[10258]235        ChatManager::message("Match is over! Gameover!");
[9272]236
237    }
238
[10258]239    //not working yet
240    void TowerDefense::upgradeTower(int x,int y)
[10340]241    {
[10258]242        const int upgradeCost = 20;
243
244        if (!this->hasEnoughCreditForTower(upgradeCost))
245        {
246            orxout() << "not enough credit: " << (this->getCredit()) << " available, " << upgradeCost << " needed.";
247            return;
248        }
249
[10340]250
251        Model* dummyModel2 = new Model(this->center_->getContext());
252
253        if (towerModelMatrix [x][y] == NULL || (towerModelMatrix [x][y])->getMeshSource() == dummyModel2->getMeshSource())
[10258]254        {
255            orxout() << "no tower on this position" << endl;
256            return;
257        }
258
259        else
260        {
[10340]261            (towerTurretMatrix [x][y])->upgradeTower();
262            this->buyTower(upgradeCost);
263        }
[10258]264    }
265
266    /*adds Tower at Position (x,y) and reduces credit and adds the point to the towermatrix. template ("towerturret")
267    so towers have ability if the turrets
268
269    */
[9272]270    void TowerDefense::addTower(int x, int y)
271    {
[10258]272        const int towerCost = 20;
[9272]273
274        if (!this->hasEnoughCreditForTower(towerCost))
275        {
[10258]276            orxout() << "not enough credit: " << (this->getCredit()) << " available, " << towerCost << " needed.";
[9272]277            return;
278        }
279
[10340]280        if (towerModelMatrix [x][y]!=NULL)
[9272]281        {
[10258]282            orxout() << "not possible to put tower here!!" << endl;
[9272]283            return;
284        }
285
[10258]286/*
[9272]287        unsigned int width = this->center_->getWidth();
288        unsigned int height = this->center_->getHeight();
[10258]289*/
[9272]290
291        int tileScale = (int) this->center_->getTileScale();
292
293        if (x > 15 || y > 15 || x < 0 || y < 0)
294        {
295            //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet)
296            orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl;
297            return;
298        }
299
300        orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
301
302
[10340]303
[10335]304        //Create Model
305        Model* newtowermodel = new Model(this->center_->getContext());
306        newtowermodel->setMeshSource("Tower.mesh");
307        newtowermodel->setScale(45);
308        newtowermodel->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 50);
309
310
311
[10258]312        //Creates tower
313        TowerDefenseTower* towernew = new TowerDefenseTower(this->center_->getContext());
[10335]314        towernew->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 275);
[10258]315        towernew->setGame(this);
[10335]316        towernew->setTeam(1);
[10340]317
318        //Reduce credit
319         this->buyTower(towerCost);
320         towerModelMatrix [x][y]= newtowermodel;
321         towerTurretMatrix [x][y]= towernew;
[10258]322    }
[9272]323
[10258]324    bool TowerDefense::hasEnoughCreditForTower(int towerCost)
325    {
326        return ((this->getCredit()) >= towerCost);
[9272]327    }
328
[10258]329
330    bool TowerDefense::hasEnoughCreditForUpgrade()
[9272]331    {
[10258]332        return true;
[9272]333    }
334
[10258]335 
336    void TowerDefense::tick(float dt)
[9272]337    {
[10258]338        SUPER(TowerDefense, tick, dt);
339        time +=dt;
340
341        TDCoordinate* coord1 = new TDCoordinate(1,1);
342        std::vector<TDCoordinate*> path;
343        path.push_back(coord1);
[10368]344        if(time>=TowerDefenseEnemyvector.size() && TowerDefenseEnemyvector.size() < 30)
[9272]345        {
[10258]346            //adds different types of enemys depending on the WaveNumber
347            addTowerDefenseEnemy(path, this->getWaveNumber() % 3 +1 );
[9272]348        }
349
[10258]350        Vector3* endpoint = new Vector3(500, 700, 150);
351        //if ships are at the end they get destroyed
352        for(unsigned int i =0; i < TowerDefenseEnemyvector.size(); ++i)
353        {
354            if(TowerDefenseEnemyvector.at(i) != NULL && TowerDefenseEnemyvector.at(i)->isAlive())
355            {
[10340]356                //destroys enemys at the end of the path and reduces the life by 1. no credits gifted
[9272]357
[10258]358                Vector3 ship = TowerDefenseEnemyvector.at(i)->getRVWorldPosition();
359                float distance = ship.distance(*endpoint);
[9272]360
[10258]361                if(distance <50){
362                    TowerDefenseEnemyvector.at(i)->destroy();
363                    this->reduceLifes(1);
364                    this->buyTower(1);
365                    if (this->getLifes()==0)
366                    {
367                        this->end();
368                    }
369                }
370            }
371        }
[10368]372
[10258]373        //goes thorugh vector to see if an enemy is still alive. if not next wave is launched
374        int count= 0;
375        for(unsigned int i =0; i < TowerDefenseEnemyvector.size(); ++i)
376        {
377            if(TowerDefenseEnemyvector.at(i)!= NULL)
378            {
379                ++count;
380            }
381        }
382
[10368]383        if (count == 0 && !this->nextwaveTimer_.isActive())
384            this->nextwaveTimer_.startTimer();
385
386/*            time2 +=dt;
[10258]387        if(count== 0)
388        {
389            if(time2 > 10)
390            {
391                TowerDefenseEnemyvector.clear();
392                this->nextwave();
393                time=0;
394                time2=0;
395            }
396        }
[10368]397*/
[10258]398
[9123]399    }
[9148]400
[9272]401    // Function to test if we can add waypoints using code only. Doesn't work yet
402
403    // THE PROBLEM: WaypointController's getControllableEntity() returns null, so it won't track. How do we get the controlableEntity to NOT BE NULL???
404    /*
405    void TowerDefense::addWaypointsAndFirstEnemy()
406    {
407        SpaceShip *newShip = new SpaceShip(this->center_);
408        newShip->addTemplate("spaceshipassff");
409
410        WaypointController *newController = new WaypointController(newShip);
411        newController->setAccuracy(3);
412
413        Model *wayPoint1 = new Model(newController);
414        wayPoint1->setMeshSource("crate.mesh");
415        wayPoint1->setPosition(7,-7,5);
416        wayPoint1->setScale(0.2);
417
418        Model *wayPoint2 = new Model(newController);
419        wayPoint2->setMeshSource("crate.mesh");
420        wayPoint2->setPosition(7,7,5);
421        wayPoint2->setScale(0.2);
422
423        newController->addWaypoint(wayPoint1);
424        newController->addWaypoint(wayPoint2);
425
426        // The following line causes the game to crash
427
428        newShip->setController(newController);
429//        newController -> getPlayer() -> startControl(newShip);
430        newShip->setPosition(-7,-7,5);
431        newShip->setScale(0.1);
432        //newShip->addSpeed(1);
433
434
435
436//      this->center_->attach(newShip);
437    }
438    */
439    /*
440    void TowerDefense::playerEntered(PlayerInfo* player)
441    {
[10368]442        TeamDeathmatch::playerEntered(player);
[9272]443
444        const std::string& message = player->getName() + " entered the game";
445        ChatManager::message(message);
446    }
447
448    bool TowerDefense::playerLeft(PlayerInfo* player)
449    {
[10368]450        bool valid_player = TeamDeathmatch::playerLeft(player);
[9272]451
452        if (valid_player)
453        {
454            const std::string& message = player->getName() + " left the game";
455            ChatManager::message(message);
456        }
457
458        return valid_player;
459    }
460
461
462    void TowerDefense::pawnKilled(Pawn* victim, Pawn* killer)
463    {
464        if (victim && victim->getPlayer())
465        {
466            std::string message;
467            if (killer)
468            {
469                if (killer->getPlayer())
470                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
471                else
472                    message = victim->getPlayer()->getName() + " was killed";
473            }
474            else
475                message = victim->getPlayer()->getName() + " died";
476
477            ChatManager::message(message);
478        }
479
[10368]480        TeamDeathmatch::pawnKilled(victim, killer);
[9272]481    }
482
[9347]483    void TowerDefense::playerScored(PlayerInfo* player, int score)
[9272]484    {
[9347]485        Gametype::playerScored(player, score);
[9272]486    }*/
[9098]487}
Note: See TracBrowser for help on using the repository browser.