Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/modules/towerdefense/TowerDefense.cc @ 10527

Last change on this file since 10527 was 10350, checked in by landauf, 10 years ago

removed hack

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