Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/newlevel2012/src/modules/towerdefense/TowerDefense.cc @ 9211

Last change on this file since 9211 was 9211, checked in by mentzerf, 12 years ago

+ Now only adding tower if there is enough credit

File size: 10.0 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
76#include "TowerDefense.h"
77#include "Tower.h"
78#include "TowerDefenseCenterpoint.h"
79
80#include "worldentities/SpawnPoint.h"
81#include "worldentities/pawns/Pawn.h"
82#include "worldentities/pawns/SpaceShip.h"
83#include "controllers/WaypointController.h"
84         
85#include "graphics/Model.h"
86#include "infos/PlayerInfo.h"
87         
88#include "chat/ChatManager.h"
89
90/* Part of a temporary hack to allow the player to add towers */
91#include "core/command/ConsoleCommand.h"
92
93namespace orxonox
94{
95    CreateUnloadableFactory(TowerDefense);
96       
97        TowerDefense::TowerDefense(BaseObject* creator) : Deathmatch(creator)
98    {
99        RegisterObject(TowerDefense);
100               
101        this->setHUDTemplate("TowerDefenseHUD");
102
103                this->stats_ = new TowerDefensePlayerStats();
104
105                /* Temporary hack to allow the player to add towers */
106                this->dedicatedAddTower_ = createConsoleCommand( "addTower", createExecutor( createFunctor(&TowerDefense::addTower, this) ) );
107       
108                // Quick hack to test waypoints
109                createConsoleCommand( "aw", createExecutor( createFunctor(&TowerDefense::addWaypointsAndFirstEnemy, this) ) );
110    }
111       
112    TowerDefense::~TowerDefense()
113    {
114                /* Part of a temporary hack to allow the player to add towers */
115        if (this->isInitialized())
116        {
117            if( this->dedicatedAddTower_ )
118                delete this->dedicatedAddTower_;
119        }
120    }
121       
122        void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint)
123        {
124                orxout() << "Centerpoint now setting..." << endl;
125                this->center_ = centerpoint;
126                orxout() << "Centerpoint now set..." << endl;
127        }
128       
129    void TowerDefense::start()
130    {
131        Deathmatch::start();
132               
133        orxout() << "Adding towers for debug..." << endl;
134               
135                // Mark corners
136                addTower(0,15); addTower(15,0);
137               
138                // Mark diagonal line
139                for (int i = 0 ; i <= 15; i++)
140                        addTower(i,i);
141               
142                orxout() << "Done" << endl;
143               
144                ChatManager::message("Use the console command addTower x y to add towers");
145
146                //TODO: let the player control his controllable entity && TODO: create a new ControllableEntity for the player
147
148        }
149       
150        void TowerDefense::end()
151        {
152                Deathmatch::end();
153         
154                ChatManager::message("Match is over");
155        }
156       
157        void TowerDefense::addTower(int x, int y)
158        {
159                const TowerCost towerCost = TDDefaultTowerCost;
160               
161                if (!this->hasEnoughCreditForTower(towerCost))
162                {
163                        orxout() << "not enough credit: " << (this->stats_->getCredit()) << " available, " << TDDefaultTowerCost << " needed.";
164                        return;
165                }
166               
167                if (this->towerExists(x,y))
168                {
169                        orxout() << "tower exists!!" << endl;
170                        return;
171                }               
172               
173                /*
174                unsigned int width = this->center_->getWidth();
175                unsigned int height = this->center_->getHeight();
176                */
177               
178                int tileScale = (int) this->center_->getTileScale();
179                       
180                if (x > 15 || y > 15 || x < 0 || y < 0)
181                {
182                        //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet)
183                        orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl;
184                        return;
185                }
186               
187                orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
188               
189                // Add tower to coordinatesStack
190                Coordinate newTowerCoordinates = {x, y};
191                addedTowersCoordinates_.push_back(newTowerCoordinates);
192               
193                // Reduce credit
194                this->stats_->buyTower(towerCost);
195               
196                // Create tower
197                Tower* newTower = new Tower(this->center_);
198                newTower->addTemplate(this->center_->getTowerTemplate());
199
200                //this->center_->attach(newTower);
201
202                newTower->setPosition((x-8) * tileScale, (y-8) * tileScale, 100);
203                newTower->setGame(this);
204                //TODO: Save the Tower in a Vector. I would suggest std::vector< std::vector<Tower*> > towers_ as a protected member variable;
205               
206                // TODO: create Tower mesh
207                // TODO: load Tower mesh
208        }
209       
210        bool TowerDefense::hasEnoughCreditForTower(TowerCost towerCost)
211        {
212                return ((this->stats_->getCredit()) >= towerCost);
213        }
214       
215        bool TowerDefense::towerExists(int x, int y)
216        {
217                for(std::vector<Coordinate>::iterator it = addedTowersCoordinates_.begin(); it != addedTowersCoordinates_.end(); ++it) 
218                {
219                        Coordinate currentCoordinates = (Coordinate) (*it);
220                        if (currentCoordinates.x == x && currentCoordinates.y == y)
221                                return true;
222                }
223               
224                return false;
225        }
226       
227       
228        void TowerDefense::tick(float dt)
229    {
230        SUPER(TowerDefense, tick, dt);
231               
232        static int test = 0;
233        if (++test == 10)
234        {
235                        orxout()<< "10th tick." <<endl;
236                        /*
237                        for (std::set<SpawnPoint*>::iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); it++)
238                        {
239                                orxout() << "checking spawnpoint with name " << (*it)->getSpawnClass()->getName() << endl;
240                        }
241                        */
242                       
243                        //addWaypointsAndFirstEnemy();
244                       
245        }
246    }
247       
248        // Function to test if we can add waypoints using code only. Doesn't work yet
249       
250        // THE PROBLEM: WaypointController's getControllableEntity() returns null, so it won't track. How do we get the controlableEntity to NOT BE NULL???
251       
252        void TowerDefense::addWaypointsAndFirstEnemy()
253        {
254                SpaceShip *newShip = new SpaceShip(this->center_);
255                newShip->addTemplate("spaceshipassff");
256               
257                WaypointController *newController = new WaypointController(newShip);
258                newController->setAccuracy(3);
259               
260                Model *wayPoint1 = new Model(newController);
261                wayPoint1->setMeshSource("crate.mesh"); 
262                wayPoint1->setPosition(7,-7,5);
263                wayPoint1->setScale(0.2);
264                       
265                Model *wayPoint2 = new Model(newController);
266                wayPoint2->setMeshSource("crate.mesh");
267                wayPoint2->setPosition(7,7,5);
268                wayPoint2->setScale(0.2);
269                       
270                newController->addWaypoint(wayPoint1);
271                newController->addWaypoint(wayPoint2);
272                       
273                // The following line causes the game to crash
274
275                newShip->setController(newController);
276//              newController -> getPlayer() -> startControl(newShip);
277                newShip->setPosition(-7,-7,5);
278                newShip->setScale(0.1);
279                //newShip->addSpeed(1);
280               
281               
282               
283//              this->center_->attach(newShip);
284        }
285       
286        /*
287         void TowerDefense::playerEntered(PlayerInfo* player)
288         {
289         Deathmatch::playerEntered(player);
290         
291         const std::string& message = player->getName() + " entered the game";
292         ChatManager::message(message);
293         }
294         
295         bool TowerDefense::playerLeft(PlayerInfo* player)
296         {
297         bool valid_player = Deathmatch::playerLeft(player);
298         
299         if (valid_player)
300         {
301         const std::string& message = player->getName() + " left the game";
302         ChatManager::message(message);
303         }
304         
305         return valid_player;
306         }
307         
308         
309         void TowerDefense::pawnKilled(Pawn* victim, Pawn* killer)
310         {
311         if (victim && victim->getPlayer())
312         {
313         std::string message;
314         if (killer)
315         {
316         if (killer->getPlayer())
317         message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
318         else
319         message = victim->getPlayer()->getName() + " was killed";
320         }
321         else
322         message = victim->getPlayer()->getName() + " died";
323         
324         ChatManager::message(message);
325         }
326         
327         Deathmatch::pawnKilled(victim, killer);
328         }
329         
330         void TowerDefense::playerScored(PlayerInfo* player)
331         {
332         Gametype::playerScored(player);
333         
334         }*/
335}
Note: See TracBrowser for help on using the repository browser.