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 "worldentities/SpawnPoint.h" |
---|
78 | #include "controllers/WaypointController.h" |
---|
79 | #include "graphics/Model.h" |
---|
80 | #include "infos/PlayerInfo.h" |
---|
81 | #include "chat/ChatManager.h" |
---|
82 | #include "core/CoreIncludes.h" |
---|
83 | #include "Highscore.h" |
---|
84 | |
---|
85 | namespace orxonox |
---|
86 | { |
---|
87 | RegisterUnloadableClass(TowerDefense); |
---|
88 | |
---|
89 | TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context) |
---|
90 | { |
---|
91 | RegisterObject(TowerDefense); |
---|
92 | |
---|
93 | selecter = nullptr; |
---|
94 | this->player_ = nullptr; |
---|
95 | this->setHUDTemplate("TowerDefenseHUD"); |
---|
96 | this->waveNumber_ = 0; |
---|
97 | this->timeSinceLastSpawn_ = 0.0; |
---|
98 | this->timeUntilNextWave_ = 0.0; |
---|
99 | this->credit_ = 0; |
---|
100 | this->lifes_ = 0; |
---|
101 | this->waveSize_ = 0; |
---|
102 | offset_ = Vector3(0,0,10); |
---|
103 | } |
---|
104 | |
---|
105 | TowerDefense::~TowerDefense() |
---|
106 | { |
---|
107 | if (this->isInitialized()) |
---|
108 | { |
---|
109 | |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint) |
---|
114 | { |
---|
115 | this->center_ = centerpoint; |
---|
116 | } |
---|
117 | |
---|
118 | void TowerDefense::start() |
---|
119 | { |
---|
120 | if (center_ != nullptr) // There needs to be a TowerDefenseCenterpoint, i.e. the area the game takes place. |
---|
121 | { |
---|
122 | if (selecter == nullptr) |
---|
123 | { |
---|
124 | selecter = new TowerDefenseSelecter(this->center_->getContext()); |
---|
125 | } |
---|
126 | selecter->addTemplate(center_->getSelecterTemplate()); |
---|
127 | center_->attach(selecter); |
---|
128 | } |
---|
129 | else // If no centerpoint was specified, an error is thrown and the level is exited. |
---|
130 | { |
---|
131 | orxout(internal_error) << "Jump: No Centerpoint specified." << endl; |
---|
132 | return; |
---|
133 | } |
---|
134 | |
---|
135 | enemies_.clear(); |
---|
136 | |
---|
137 | createFields(); |
---|
138 | TeamDeathmatch::start(); |
---|
139 | |
---|
140 | //set initial credits, lifes and WaveNumber |
---|
141 | this->setCredit(1000); |
---|
142 | this->setLifes(100); |
---|
143 | this->timeSinceLastSpawn_ = 0.0; |
---|
144 | this->timeUntilNextWave_ = 5.0; |
---|
145 | this->waveSize_ = 0; |
---|
146 | this->setWaveNumber(0); |
---|
147 | } |
---|
148 | |
---|
149 | // Generates a TowerDefenseEnemy. Uses Template "enemytowerdefense". Sets position at first waypoint of path. |
---|
150 | void TowerDefense::addTowerDefenseEnemy(int templatenr) |
---|
151 | { |
---|
152 | TowerDefenseEnemy* en1 = new TowerDefenseEnemy(this->center_->getContext()); |
---|
153 | |
---|
154 | switch(templatenr) |
---|
155 | { |
---|
156 | case 1 : |
---|
157 | en1->addTemplate("enemytowerdefense1"); |
---|
158 | en1->setScale(3); |
---|
159 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
160 | break; |
---|
161 | |
---|
162 | case 2 : |
---|
163 | en1->addTemplate("enemytowerdefense2"); |
---|
164 | en1->setScale(2); |
---|
165 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
166 | break; |
---|
167 | |
---|
168 | case 3 : |
---|
169 | en1->addTemplate("enemytowerdefense3"); |
---|
170 | en1->setScale(1); |
---|
171 | en1->setHealth(en1->getHealth() + this->getWaveNumber()*4); |
---|
172 | break; |
---|
173 | } |
---|
174 | |
---|
175 | en1->setTeam(2); |
---|
176 | |
---|
177 | WaypointController* controller = (WaypointController*)(en1->getXMLController()); |
---|
178 | |
---|
179 | if (controller != nullptr && waypoints_.size() > 1) |
---|
180 | { |
---|
181 | en1->setPosition(waypoints_.at(0)->getPosition() + offset_); |
---|
182 | en1->setOrientation(Vector3(0,0,10), Degree(0)); |
---|
183 | en1->setDirection(Vector3(0,1,0)); |
---|
184 | en1->lookAt(waypoints_.at(1)->getPosition() + offset_); |
---|
185 | |
---|
186 | for (TowerDefenseField* field : waypoints_) |
---|
187 | { |
---|
188 | orxonox::WeakPtr<MovableEntity> waypoint = new MovableEntity(this->center_->getContext()); |
---|
189 | waypoint->setPosition(field->getPosition() + offset_); |
---|
190 | controller->addWaypoint(waypoint); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | enemies_.push_back(en1); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | void TowerDefense::end() |
---|
199 | { |
---|
200 | if (Highscore::exists()) |
---|
201 | { |
---|
202 | int score = this->getWaveNumber(); |
---|
203 | Highscore::getInstance().storeScore("Tower Defense", score, this->getPlayer()); |
---|
204 | } |
---|
205 | TeamDeathmatch::end(); |
---|
206 | ChatManager::message("Match is over! Gameover!"); |
---|
207 | |
---|
208 | } |
---|
209 | |
---|
210 | void TowerDefense::spawnPlayer(PlayerInfo* player) |
---|
211 | { |
---|
212 | assert(player); |
---|
213 | player_ = player; |
---|
214 | |
---|
215 | if (selecter->getPlayer() == nullptr) |
---|
216 | { |
---|
217 | player_->startControl(selecter); |
---|
218 | players_[player].state_ = PlayerState::Alive; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | @brief |
---|
224 | Get the player. |
---|
225 | @return |
---|
226 | Returns a pointer to the player. If there is no player, nullptr is returned. |
---|
227 | */ |
---|
228 | PlayerInfo* TowerDefense::getPlayer(void) const |
---|
229 | { |
---|
230 | return this->player_; |
---|
231 | } |
---|
232 | |
---|
233 | //not working yet |
---|
234 | void TowerDefense::addTower(int x,int y) |
---|
235 | { |
---|
236 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
237 | x = coord->GetX(); |
---|
238 | y = coord->GetY(); |
---|
239 | |
---|
240 | int cost = center_->getTowerCost(1); |
---|
241 | |
---|
242 | if (fields_[x][y]->isFree() == true && getCredit() >= cost) |
---|
243 | { |
---|
244 | payCredit(cost); |
---|
245 | fields_[x][y]->createTower(1); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /*adds Tower at Position (x,y) and reduces credit and adds the point to the towermatrix. template ("towerturret") |
---|
250 | so towers have ability if the turrets |
---|
251 | */ |
---|
252 | |
---|
253 | void TowerDefense::upgradeTower(int x, int y) |
---|
254 | { |
---|
255 | TDCoordinate* coord = new TDCoordinate(x,y); |
---|
256 | x = coord->GetX(); |
---|
257 | y = coord->GetY(); |
---|
258 | |
---|
259 | int cost = center_->getTowerCost(fields_[x][y]->getUpgrade() + 1); |
---|
260 | |
---|
261 | if (fields_[x][y]->isFree() == false && fields_[x][y]->canUpgrade() == true && getCredit() >= cost) |
---|
262 | { |
---|
263 | payCredit(cost); |
---|
264 | fields_[x][y]->upgrade(); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | void TowerDefense::tick(float dt) |
---|
269 | { |
---|
270 | SUPER(TowerDefense, tick, dt); |
---|
271 | |
---|
272 | if (hasStarted() == false || player_ == nullptr) |
---|
273 | { |
---|
274 | return; |
---|
275 | } |
---|
276 | timeUntilNextWave_ -= dt; |
---|
277 | timeSinceLastSpawn_ += dt; |
---|
278 | |
---|
279 | //build/upgrade tower at selecter position |
---|
280 | if (selecter != nullptr && selecter->buildTower_ == true) |
---|
281 | { |
---|
282 | selecter->buildTower_ = false; |
---|
283 | |
---|
284 | if (getField(selecter->selectedPos_)->canUpgrade() == true) |
---|
285 | { |
---|
286 | upgradeTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | addTower(selecter->selectedPos_->GetX(), selecter->selectedPos_->GetY()); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | for (std::list<WeakPtr<TowerDefenseEnemy>>::iterator it = enemies_.begin(); it != enemies_.end(); ) |
---|
295 | { |
---|
296 | if (*it == nullptr) |
---|
297 | { |
---|
298 | // the enemy was destroyed by a tower - remove it from the list |
---|
299 | enemies_.erase(it++); |
---|
300 | addCredit(1); |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | //if ships are at the end they get destroyed |
---|
305 | Vector3 ship = (*it)->getRVWorldPosition(); |
---|
306 | float distance = ship.distance(endpoint_); |
---|
307 | if(distance < 40) |
---|
308 | { |
---|
309 | (*it)->destroy(); |
---|
310 | enemies_.erase(it++); |
---|
311 | this->reduceLifes(1); |
---|
312 | if (this->getLifes() == 0) |
---|
313 | { |
---|
314 | this->end(); |
---|
315 | } |
---|
316 | } |
---|
317 | else |
---|
318 | { |
---|
319 | ++ it; |
---|
320 | } |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | // Add new enemy? |
---|
325 | if (timeSinceLastSpawn_ >= 1.0 && waveSize_ > 0) |
---|
326 | { |
---|
327 | // Add new enemy |
---|
328 | timeSinceLastSpawn_ -= 1.0; |
---|
329 | -- waveSize_; |
---|
330 | addTowerDefenseEnemy(this->getWaveNumber() % 3 + 1); |
---|
331 | } |
---|
332 | else if (timeUntilNextWave_ <= 0.0 && waveSize_ <= 0) |
---|
333 | { |
---|
334 | //New wave |
---|
335 | ++ waveNumber_; |
---|
336 | timeSinceLastSpawn_ = 0.0; |
---|
337 | |
---|
338 | timeUntilNextWave_ = waveNumber_+20.0f; |
---|
339 | waveSize_ = waveNumber_+5; |
---|
340 | } |
---|
341 | |
---|
342 | } |
---|
343 | |
---|
344 | void TowerDefense::createFields() |
---|
345 | { |
---|
346 | assert(center_); |
---|
347 | TDCoordinate coord(0,0); |
---|
348 | TDCoordinate startCoord(0,0); |
---|
349 | std::string fields = center_->getFields(); |
---|
350 | int pos = 0; |
---|
351 | for (int j = 15; j >= 0; --j) |
---|
352 | { |
---|
353 | for (int i = 0; i < 16; ++i) |
---|
354 | { |
---|
355 | coord.Set(i,j); |
---|
356 | fields_[i][j] = new TowerDefenseField(center_->getContext()); |
---|
357 | fields_[i][j]->setCenterpoint(center_); |
---|
358 | center_->attach(fields_[i][j]); |
---|
359 | fields_[i][j]->setPosition(coord.get3dcoordinate()); |
---|
360 | fields_[i][j]->create(fields.at(pos), fields.at(pos+1)); |
---|
361 | pos += 2; |
---|
362 | if (fields_[i][j]->getType() == TowerDefenseFieldType::START) |
---|
363 | { |
---|
364 | startCoord.Set(i,j); |
---|
365 | waypoints_.push_back(fields_[i][j]); |
---|
366 | } |
---|
367 | } |
---|
368 | } |
---|
369 | //Place waypoints along the street for the waypoint controllers of the enemies |
---|
370 | TDCoordinate* thisCoord = &startCoord; |
---|
371 | TDCoordinate* nextCoord; |
---|
372 | while ((nextCoord = getNextStreetCoord(thisCoord)) != nullptr) |
---|
373 | { |
---|
374 | waypoints_.push_back(fields_[nextCoord->GetX()][nextCoord->GetY()]); |
---|
375 | thisCoord = nextCoord; |
---|
376 | endpoint_ = nextCoord->get3dcoordinate(); |
---|
377 | } |
---|
378 | |
---|
379 | } |
---|
380 | |
---|
381 | TDCoordinate* TowerDefense::getNextStreetCoord(TDCoordinate* thisCoord) |
---|
382 | { |
---|
383 | TowerDefenseField* thisField = fields_[thisCoord->GetX()][thisCoord->GetY()]; |
---|
384 | |
---|
385 | if (thisField->getType() != TowerDefenseFieldType::STREET && thisField->getType() != TowerDefenseFieldType::START) |
---|
386 | { |
---|
387 | return nullptr; |
---|
388 | } |
---|
389 | |
---|
390 | TDCoordinate* nextCoord = new TDCoordinate(0, 0); |
---|
391 | |
---|
392 | if (thisField->getAngle() == 0) |
---|
393 | { |
---|
394 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() - 1); |
---|
395 | } |
---|
396 | else if (thisField->getAngle() == 1) |
---|
397 | { |
---|
398 | nextCoord->Set(thisCoord->GetX() + 1, thisCoord->GetY()); |
---|
399 | } |
---|
400 | else if (thisField->getAngle() == 2) |
---|
401 | { |
---|
402 | nextCoord->Set(thisCoord->GetX(), thisCoord->GetY() + 1); |
---|
403 | } |
---|
404 | else if (thisField->getAngle() == 3) |
---|
405 | { |
---|
406 | nextCoord->Set(thisCoord->GetX() - 1, thisCoord->GetY()); |
---|
407 | } |
---|
408 | |
---|
409 | if (thisCoord->GetX() != nextCoord->GetX() || thisCoord->GetY() != nextCoord->GetY()) |
---|
410 | { |
---|
411 | return nextCoord; |
---|
412 | } |
---|
413 | |
---|
414 | delete nextCoord; |
---|
415 | return nullptr; |
---|
416 | } |
---|
417 | } |
---|