Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10422 was 10408, checked in by erbj, 10 years ago

added functionality to the TowerDefenseSelecter

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