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
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
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 *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 *
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 *
67 * c)  Im TowerDefense gamtype muss im Constructor noch das HUD-Template gesetzt werden.
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 *
73 */
74#include "TowerDefense.h"
75#include "TowerDefenseTower.h"
76#include "TowerDefenseCenterpoint.h"
77//#include "TDCoordinate.h"
78#include "worldentities/SpawnPoint.h"
79#include "worldentities/pawns/Pawn.h"
80#include "worldentities/pawns/SpaceShip.h"
81#include "controllers/WaypointController.h"
82#include "graphics/Model.h"
83#include "infos/PlayerInfo.h"
84#include "chat/ChatManager.h"
85#include "core/CoreIncludes.h"
86/* Part of a temporary hack to allow the player to add towers */
87#include "core/command/ConsoleCommand.h"
88
89
90namespace orxonox
91{
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
98    RegisterUnloadableClass(TowerDefense);
99
100    TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context)
101    {
102        RegisterObject(TowerDefense);
103/*
104        for (int i=0; i < 16 ; i++){
105            for (int j = 0; j< 16 ; j++){
106                towermatrix[i][j] = NULL;
107            }
108        }*/
109
110        //Timer for the waves (10 seconds between the waves)
111        selecter = NULL;
112        this->player_ = NULL;       
113        this->setHUDTemplate("TowerDefenseHUD");
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;
120        this->timeSetTower_ = 0;
121
122
123        ModifyConsoleCommand(__CC_addTower_name).setObject(this);
124        ModifyConsoleCommand(__CC_upgradeTower_name).setObject(this);
125    }
126
127    TowerDefense::~TowerDefense()
128    {
129        /* Part of a temporary hack to allow the player to add towers */
130        if (this->isInitialized())
131        {
132            ModifyConsoleCommand(__CC_addTower_name).setObject(NULL);
133            ModifyConsoleCommand(__CC_upgradeTower_name).setObject(NULL);
134        }
135    }
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
144    void TowerDefense::start()
145    {
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        }
160
161        TeamDeathmatch::start();
162
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
164        for (int i=0; i < 16 ; i++)
165        {
166            for (int j = 0; j< 16 ; j++)
167            {
168                towerModelMatrix[i][j] = NULL;
169                towerTurretMatrix[i][j] = NULL;
170            }
171        }
172
173       
174
175        if (player_ != NULL)
176        {
177            //this->player_->startControl(selecter);
178        }
179        else
180        {
181            orxout() << "player=NULL" << endl;
182        }
183
184
185        Model* dummyModel = new Model(this->center_->getContext());
186
187        //the path of the spacehips has to be blocked, so that no towers can be build there
188        for (int k=0; k<3; k++)
189            towerModelMatrix[1][k]=dummyModel;
190        for (int l=1; l<11; l++)
191                towerModelMatrix[l][3]=dummyModel;
192        for (int m=3; m<12; m++)
193                towerModelMatrix[10][m]=dummyModel;
194        for (int n=10; n<14; n++)
195                towerModelMatrix[n][11]=dummyModel;
196        for (int o=13; o<16; o++)
197                towerModelMatrix[13][o]=dummyModel;
198
199
200        //set initial credits, lifes and WaveNumber
201        this->setCredit(1000);
202        this->setLifes(100);
203        this->setWaveNumber(0);
204        time=0.0;
205
206        /*
207        //adds initial towers
208        for (int i=0; i <7; i++){
209            addTower(i+3,4);
210        }
211                */
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)
221        {
222        case 1 :
223            en1->addTemplate("enemytowerdefense1");
224            en1->setScale(3);
225            en1->setHealth(en1->getHealth() +50 + this->getWaveNumber()*4);
226            break;
227
228        case 2 :
229            en1->addTemplate("enemytowerdefense2");
230            en1->setScale(2);
231            en1->setHealth(en1->getHealth() -30 + this->getWaveNumber()*4);
232            //  en1->setShieldHealth(en1->getShield() = this->getWaveNumber()*2))
233            break;
234
235        case 3 :
236            en1->addTemplate("enemytowerdefense3");
237            en1->setScale(1);
238            en1->setHealth(en1->getHealth() -10 + this->getWaveNumber()*4);
239            break;
240        }
241
242        en1->setTeam(2);
243        en1->getController();
244        en1->setPosition(path.at(0)->get3dcoordinate());       
245        TowerDefenseEnemyvector.push_back(en1);
246
247        for(unsigned int i = 0; i < path.size(); ++i)
248        {
249            en1->addWaypoint((path.at(i)));
250        }
251    }
252
253
254    void TowerDefense::end()
255    {
256
257        TeamDeathmatch::end();
258        ChatManager::message("Match is over! Gameover!");
259
260    }
261
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
286    //not working yet
287    void TowerDefense::upgradeTower(int x,int y)
288    {
289        TDCoordinate* coord = new TDCoordinate(x,y);
290        x = coord->GetX();
291        y = coord->GetY();
292       
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
301
302        Model* dummyModel2 = new Model(this->center_->getContext());
303
304        if (towerModelMatrix [x][y] == NULL || (towerModelMatrix [x][y])->getMeshSource() == dummyModel2->getMeshSource())
305        {
306            orxout() << "no tower on this position" << endl;
307            return;
308        }
309
310        else
311        {
312            (towerTurretMatrix [x][y])->upgradeTower();
313            this->buyTower(upgradeCost);
314        }
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
319    */
320
321    void TowerDefense::addTower(int x, int y)
322    {       
323        TDCoordinate* coord = new TDCoordinate(x,y);
324        x = coord->GetX();
325        y = coord->GetY();
326
327        const int towerCost = 20;
328
329        if (!this->hasEnoughCreditForTower(towerCost))
330        {
331            orxout() << "not enough credit: " << (this->getCredit()) << " available, " << towerCost << " needed.";
332            return;
333        }
334
335        if (towerModelMatrix [x][y]!=NULL)
336        {
337            orxout() << "not possible to put tower here!!" << endl;
338            return;
339        }
340
341/*
342        unsigned int width = this->center_->getWidth();
343        unsigned int height = this->center_->getHeight();
344*/
345
346        int tileScale = (int) this->center_->getTileScale();
347
348        /*if (x > 15 || y > 15 || x < 0 || y < 0)
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;
353        }*/
354
355        //orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
356        orxout() << "Will add tower at (" << x << "," << y << ")" << endl;
357
358
359        //Create Model
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);
364
365        //Creates tower
366        TowerDefenseTower* towernew = new TowerDefenseTower(this->center_->getContext());
367        towernew->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 275);
368        towernew->setGame(this);
369        towernew->setTeam(1);
370
371        //Reduce credit
372         this->buyTower(towerCost);
373         towerModelMatrix [x][y]= newTowerModel;
374         towerTurretMatrix [x][y]= towernew;
375    }   
376
377    bool TowerDefense::hasEnoughCreditForTower(int towerCost)
378    {
379        return ((this->getCredit()) >= towerCost);
380    }
381
382
383    bool TowerDefense::hasEnoughCreditForUpgrade()
384    {
385        return true;
386    }
387
388 
389    void TowerDefense::tick(float dt)
390    {
391        SUPER(TowerDefense, tick, dt);
392        time +=dt;
393        timeSetTower_ +=dt;
394        orxout() << dt << "time" << endl;
395
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
424        TDCoordinate* coord1 = new TDCoordinate(1,1);
425        std::vector<TDCoordinate*> path;
426        path.push_back(coord1);
427
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
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            {
454                //destroys enemys at the end of the path and reduces the life by 1. no credits gifted
455
456                Vector3 ship = TowerDefenseEnemyvector.at(i)->getRVWorldPosition();
457                float distance = ship.distance(*endpoint);
458
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        }
470
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
481        if (count == 0 && !this->nextwaveTimer_.isActive())
482            this->nextwaveTimer_.startTimer();
483
484/*            time2 +=dt;
485        if(count== 0)
486        {
487            if(time2 > 10)
488            {
489                TowerDefenseEnemyvector.clear();
490                this->nextwave();
491                time=0;
492                time2=0;
493            }
494        }
495*/
496
497    }
498
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    {
540        TeamDeathmatch::playerEntered(player);
541
542        const std::string& message = player->getName() + " entered the game";
543        ChatManager::message(message);
544    }
545
546    bool TowerDefense::playerLeft(PlayerInfo* player)
547    {
548        bool valid_player = TeamDeathmatch::playerLeft(player);
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
578        TeamDeathmatch::pawnKilled(victim, killer);
579    }
580
581    void TowerDefense::playerScored(PlayerInfo* player, int score)
582    {
583        Gametype::playerScored(player, score);
584    }*/
585}
Note: See TracBrowser for help on using the repository browser.