Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10325 was 10325, checked in by erbj, 9 years ago

tower templates verwaltet und tower direkt turret, nicht eigenes objekt

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