Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseHS14/src/modules/towerdefense/TowerDefense.cc @ 10121

Last change on this file since 10121 was 10109, checked in by erbj, 10 years ago

EnemyAdd implementierung

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