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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Gametype.h" |
---|
30 | |
---|
31 | #include "util/Math.h" |
---|
32 | #include "core/Core.h" |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/config/ConfigValueIncludes.h" |
---|
35 | #include "core/GameMode.h" |
---|
36 | #include "core/command/ConsoleCommandIncludes.h" |
---|
37 | #include "gamestates/GSLevel.h" |
---|
38 | |
---|
39 | #include "infos/PlayerInfo.h" |
---|
40 | #include "infos/Bot.h" |
---|
41 | #include "graphics/Camera.h" |
---|
42 | #include "worldentities/ControllableEntity.h" |
---|
43 | #include "worldentities/SpawnPoint.h" |
---|
44 | #include "worldentities/pawns/Spectator.h" |
---|
45 | #include "worldentities/pawns/Pawn.h" |
---|
46 | #include "overlays/OverlayGroup.h" |
---|
47 | #include "Scene.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | static const std::string __CC_addBots_name = "addBots"; |
---|
52 | static const std::string __CC_killBots_name = "killBots"; |
---|
53 | |
---|
54 | SetConsoleCommand("Gametype", __CC_addBots_name, &Gametype::addBots ).addShortcut().defaultValues(1); |
---|
55 | SetConsoleCommand("Gametype", __CC_killBots_name, &Gametype::killBots).addShortcut().defaultValues(0); |
---|
56 | |
---|
57 | RegisterUnloadableClass(Gametype); |
---|
58 | |
---|
59 | Gametype::Gametype(Context* context) : BaseObject(context) |
---|
60 | { |
---|
61 | RegisterObject(Gametype); |
---|
62 | |
---|
63 | this->setGametype(WeakPtr<Gametype>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency) |
---|
64 | |
---|
65 | this->gtinfo_ = new GametypeInfo(context); |
---|
66 | |
---|
67 | this->defaultControllableEntity_ = Class(Spectator); |
---|
68 | this->scoreboard_ = 0; |
---|
69 | |
---|
70 | this->bAutoStart_ = false; |
---|
71 | this->bForceSpawn_ = false; |
---|
72 | this->bAutoEnd_ = true; |
---|
73 | this->numberOfBots_ = 0; |
---|
74 | |
---|
75 | this->timeLimit_ = 0; |
---|
76 | this->time_ = 0; |
---|
77 | this->timerIsActive_ = false; |
---|
78 | |
---|
79 | this->initialStartCountdown_ = 3; |
---|
80 | |
---|
81 | this->setConfigValues(); |
---|
82 | |
---|
83 | ModifyConsoleCommand(__CC_addBots_name).setObject(this); |
---|
84 | ModifyConsoleCommand(__CC_killBots_name).setObject(this); |
---|
85 | } |
---|
86 | |
---|
87 | Gametype::~Gametype() |
---|
88 | { |
---|
89 | if (this->isInitialized()) |
---|
90 | { |
---|
91 | if (this->gtinfo_) |
---|
92 | this->gtinfo_->destroy(); |
---|
93 | |
---|
94 | ModifyConsoleCommand(__CC_addBots_name).setObject(NULL); |
---|
95 | ModifyConsoleCommand(__CC_killBots_name).setObject(NULL); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @brief Initializes sub-objects of the Gametype. This must be called after the constructor. |
---|
101 | * At this point, the context is expected to have the current gametype. This allows to pass the current gametype to the sub-objects via constructor. |
---|
102 | */ |
---|
103 | void Gametype::init() |
---|
104 | { |
---|
105 | // load the corresponding score board |
---|
106 | if (GameMode::showsGraphics() && !this->scoreboardTemplate_.empty()) |
---|
107 | { |
---|
108 | this->scoreboard_ = new OverlayGroup(this->getContext()); |
---|
109 | this->scoreboard_->addTemplate(this->scoreboardTemplate_); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void Gametype::setConfigValues() |
---|
114 | { |
---|
115 | SetConfigValue(initialStartCountdown_, 3.0f); |
---|
116 | SetConfigValue(bAutoStart_, false); |
---|
117 | SetConfigValue(bForceSpawn_, false); |
---|
118 | SetConfigValue(bAutoEnd_, true); |
---|
119 | SetConfigValue(numberOfBots_, 0); |
---|
120 | SetConfigValue(scoreboardTemplate_, "defaultScoreboard"); |
---|
121 | } |
---|
122 | |
---|
123 | void Gametype::tick(float dt) |
---|
124 | { |
---|
125 | SUPER(Gametype, tick, dt); |
---|
126 | |
---|
127 | //count timer |
---|
128 | if (timerIsActive_) |
---|
129 | { |
---|
130 | if (this->timeLimit_ == 0) |
---|
131 | this->time_ += dt; |
---|
132 | else |
---|
133 | this->time_ -= dt; |
---|
134 | } |
---|
135 | |
---|
136 | if (this->gtinfo_->isStartCountdownRunning() && !this->gtinfo_->hasStarted()) |
---|
137 | this->gtinfo_->countdownStartCountdown(dt); |
---|
138 | |
---|
139 | if (!this->gtinfo_->hasStarted()) |
---|
140 | { |
---|
141 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
142 | { |
---|
143 | // Inform the GametypeInfo that the player is ready to spawn. |
---|
144 | if(it->first->isHumanPlayer() && it->first->isReadyToSpawn()) |
---|
145 | this->gtinfo_->playerReadyToSpawn(it->first); |
---|
146 | } |
---|
147 | |
---|
148 | this->checkStart(); |
---|
149 | } |
---|
150 | else if (!this->gtinfo_->hasEnded()) |
---|
151 | this->spawnDeadPlayersIfRequested(); |
---|
152 | |
---|
153 | this->assignDefaultPawnsIfNeeded(); |
---|
154 | } |
---|
155 | |
---|
156 | void Gametype::start() |
---|
157 | { |
---|
158 | this->addBots(this->numberOfBots_); |
---|
159 | this->gtinfo_->start(); |
---|
160 | this->spawnPlayersIfRequested(); |
---|
161 | } |
---|
162 | |
---|
163 | void Gametype::end() |
---|
164 | { |
---|
165 | this->gtinfo_->end(); |
---|
166 | if (this->bAutoEnd_) |
---|
167 | { |
---|
168 | this->showMenuTimer_.setTimer(3.0f, true, createExecutor(createFunctor(&Gametype::showMenu, this))); |
---|
169 | } |
---|
170 | |
---|
171 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
172 | { |
---|
173 | if (it->first->getControllableEntity()) |
---|
174 | { |
---|
175 | ControllableEntity* oldentity = it->first->getControllableEntity(); |
---|
176 | |
---|
177 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity->getContext()); |
---|
178 | if (oldentity->getCamera()) |
---|
179 | { |
---|
180 | entity->setPosition(oldentity->getCamera()->getWorldPosition()); |
---|
181 | entity->setOrientation(oldentity->getCamera()->getWorldOrientation()); |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | entity->setPosition(oldentity->getWorldPosition()); |
---|
186 | entity->setOrientation(oldentity->getWorldOrientation()); |
---|
187 | } |
---|
188 | it->first->startControl(entity); |
---|
189 | } |
---|
190 | else |
---|
191 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | void Gametype::playerEntered(PlayerInfo* player) |
---|
196 | { |
---|
197 | this->players_[player].state_ = PlayerState::Joined; |
---|
198 | this->gtinfo_->playerEntered(player); |
---|
199 | } |
---|
200 | |
---|
201 | bool Gametype::playerLeft(PlayerInfo* player) |
---|
202 | { |
---|
203 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
204 | if (it != this->players_.end()) |
---|
205 | { |
---|
206 | this->players_.erase(it); |
---|
207 | return true; |
---|
208 | } |
---|
209 | return false; |
---|
210 | } |
---|
211 | |
---|
212 | void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) |
---|
213 | { |
---|
214 | } |
---|
215 | |
---|
216 | void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) |
---|
217 | { |
---|
218 | } |
---|
219 | |
---|
220 | bool Gametype::playerChangedName(PlayerInfo* player) |
---|
221 | { |
---|
222 | if (this->players_.find(player) != this->players_.end()) |
---|
223 | { |
---|
224 | if (player->getName() != player->getOldName()) |
---|
225 | { |
---|
226 | return true; |
---|
227 | } |
---|
228 | } |
---|
229 | return false; |
---|
230 | } |
---|
231 | |
---|
232 | void Gametype::pawnPreSpawn(Pawn* pawn) |
---|
233 | { |
---|
234 | } |
---|
235 | |
---|
236 | void Gametype::pawnPostSpawn(Pawn* pawn) |
---|
237 | { |
---|
238 | } |
---|
239 | |
---|
240 | void Gametype::playerPreSpawn(PlayerInfo* player) |
---|
241 | { |
---|
242 | } |
---|
243 | |
---|
244 | void Gametype::playerPostSpawn(PlayerInfo* player) |
---|
245 | { |
---|
246 | } |
---|
247 | |
---|
248 | void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
249 | { |
---|
250 | } |
---|
251 | |
---|
252 | void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
253 | { |
---|
254 | } |
---|
255 | |
---|
256 | bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
257 | { |
---|
258 | return true; |
---|
259 | } |
---|
260 | |
---|
261 | bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
262 | { |
---|
263 | return true; |
---|
264 | } |
---|
265 | |
---|
266 | bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
267 | { |
---|
268 | return true; |
---|
269 | } |
---|
270 | |
---|
271 | void Gametype::pawnKilled(Pawn* victim, Pawn* killer) |
---|
272 | { |
---|
273 | if (victim && victim->getPlayer()) |
---|
274 | { |
---|
275 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer()); |
---|
276 | if (it != this->players_.end()) |
---|
277 | { |
---|
278 | it->second.state_ = PlayerState::Dead; |
---|
279 | it->second.killed_++; |
---|
280 | |
---|
281 | // Reward killer |
---|
282 | if (killer && killer->getPlayer()) |
---|
283 | { |
---|
284 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer()); |
---|
285 | if (it != this->players_.end()) |
---|
286 | { |
---|
287 | it->second.frags_++; |
---|
288 | |
---|
289 | if (killer->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN) |
---|
290 | this->gtinfo_->sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID()); |
---|
291 | if (victim->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN) |
---|
292 | this->gtinfo_->sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID()); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | if(victim->getPlayer()->isHumanPlayer()) |
---|
297 | this->gtinfo_->pawnKilled(victim->getPlayer()); |
---|
298 | |
---|
299 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getContext()); |
---|
300 | if (victim->getCamera()) |
---|
301 | { |
---|
302 | entity->setPosition(victim->getCamera()->getWorldPosition()); |
---|
303 | entity->setOrientation(victim->getCamera()->getWorldOrientation()); |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | entity->setPosition(victim->getWorldPosition()); |
---|
308 | entity->setOrientation(victim->getWorldOrientation()); |
---|
309 | } |
---|
310 | it->first->startControl(entity); |
---|
311 | } |
---|
312 | else |
---|
313 | orxout(internal_warning) << "Killed Pawn was not in the playerlist" << endl; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void Gametype::playerScored(PlayerInfo* player, int score) |
---|
318 | { |
---|
319 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
320 | if (it != this->players_.end()) |
---|
321 | it->second.frags_ += score; |
---|
322 | } |
---|
323 | |
---|
324 | int Gametype::getScore(PlayerInfo* player) const |
---|
325 | { |
---|
326 | std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player); |
---|
327 | if (it != this->players_.end()) |
---|
328 | return it->second.frags_; |
---|
329 | else |
---|
330 | return 0; |
---|
331 | } |
---|
332 | |
---|
333 | SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
334 | { |
---|
335 | // If there is at least one SpawnPoint. |
---|
336 | if (this->spawnpoints_.size() > 0) |
---|
337 | { |
---|
338 | // Fallback spawn point if there is no active one, choose a random one. |
---|
339 | SpawnPoint* fallbackSpawnPoint = NULL; |
---|
340 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size()))); |
---|
341 | unsigned int index = 0; |
---|
342 | std::vector<SpawnPoint*> activeSpawnPoints; |
---|
343 | for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) |
---|
344 | { |
---|
345 | if (index == randomspawn) |
---|
346 | fallbackSpawnPoint = (*it); |
---|
347 | |
---|
348 | if (*it != NULL && (*it)->isActive()) |
---|
349 | activeSpawnPoints.push_back(*it); |
---|
350 | |
---|
351 | ++index; |
---|
352 | } |
---|
353 | |
---|
354 | if(activeSpawnPoints.size() > 0) |
---|
355 | { |
---|
356 | randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(activeSpawnPoints.size()))); |
---|
357 | return activeSpawnPoints[randomspawn]; |
---|
358 | } |
---|
359 | |
---|
360 | orxout(internal_warning) << "Fallback SpawnPoint was used because there were no active SpawnPoints." << endl; |
---|
361 | return fallbackSpawnPoint; |
---|
362 | } |
---|
363 | return 0; |
---|
364 | } |
---|
365 | |
---|
366 | void Gametype::assignDefaultPawnsIfNeeded() |
---|
367 | { |
---|
368 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
369 | { |
---|
370 | if (!it->first->getControllableEntity()) |
---|
371 | { |
---|
372 | it->second.state_ = PlayerState::Dead; |
---|
373 | |
---|
374 | if (!it->first->isReadyToSpawn() || !this->gtinfo_->hasStarted()) |
---|
375 | { |
---|
376 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
377 | it->second.state_ = PlayerState::Dead; |
---|
378 | } |
---|
379 | } |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | void Gametype::checkStart() |
---|
384 | { |
---|
385 | if (!this->gtinfo_->hasStarted()) |
---|
386 | { |
---|
387 | if (this->gtinfo_->isStartCountdownRunning()) |
---|
388 | { |
---|
389 | if (this->gtinfo_->getStartCountdown() <= 0.0f) |
---|
390 | { |
---|
391 | this->gtinfo_->stopStartCountdown(); |
---|
392 | this->gtinfo_->setStartCountdown(0.0f); |
---|
393 | this->start(); |
---|
394 | } |
---|
395 | } |
---|
396 | else if (this->players_.size() > 0) |
---|
397 | { |
---|
398 | if (this->bAutoStart_) |
---|
399 | { |
---|
400 | this->start(); |
---|
401 | } |
---|
402 | else |
---|
403 | { |
---|
404 | bool allplayersready = true; |
---|
405 | bool hashumanplayers = false; |
---|
406 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
407 | { |
---|
408 | if (!it->first->isReadyToSpawn()) |
---|
409 | allplayersready = false; |
---|
410 | if (it->first->isHumanPlayer()) |
---|
411 | hashumanplayers = true; |
---|
412 | } |
---|
413 | |
---|
414 | if (allplayersready && hashumanplayers) |
---|
415 | { |
---|
416 | // If in developer's mode, there is no start countdown. |
---|
417 | if(Core::getInstance().getConfig()->inDevMode()) |
---|
418 | this->start(); |
---|
419 | else |
---|
420 | { |
---|
421 | this->gtinfo_->setStartCountdown(this->initialStartCountdown_); |
---|
422 | this->gtinfo_->startStartCountdown(); |
---|
423 | } |
---|
424 | } |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | void Gametype::spawnPlayersIfRequested() |
---|
431 | { |
---|
432 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
433 | { |
---|
434 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
435 | this->spawnPlayer(it->first); |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | void Gametype::spawnDeadPlayersIfRequested() |
---|
440 | { |
---|
441 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
442 | if (it->second.state_ == PlayerState::Dead) |
---|
443 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
444 | this->spawnPlayer(it->first); |
---|
445 | } |
---|
446 | |
---|
447 | void Gametype::spawnPlayer(PlayerInfo* player) |
---|
448 | { |
---|
449 | SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); |
---|
450 | if (spawnpoint) |
---|
451 | { |
---|
452 | this->playerPreSpawn(player); |
---|
453 | player->startControl(spawnpoint->spawn()); |
---|
454 | this->players_[player].state_ = PlayerState::Alive; |
---|
455 | |
---|
456 | if(player->isHumanPlayer()) |
---|
457 | this->gtinfo_->playerSpawned(player); |
---|
458 | |
---|
459 | this->playerPostSpawn(player); |
---|
460 | } |
---|
461 | else |
---|
462 | { |
---|
463 | orxout(user_error) << "No SpawnPoints in current Gametype" << endl; |
---|
464 | abort(); |
---|
465 | } |
---|
466 | } |
---|
467 | |
---|
468 | void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player) |
---|
469 | { |
---|
470 | SpawnPoint* spawn = this->getBestSpawnPoint(player); |
---|
471 | if (spawn) |
---|
472 | { |
---|
473 | // force spawn at spawnpoint with default pawn |
---|
474 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn->getContext()); |
---|
475 | spawn->spawn(entity); |
---|
476 | player->startControl(entity); |
---|
477 | } |
---|
478 | else |
---|
479 | { |
---|
480 | orxout(user_error) << "No SpawnPoints in current Gametype" << endl; |
---|
481 | abort(); |
---|
482 | } |
---|
483 | } |
---|
484 | |
---|
485 | void Gametype::addBots(unsigned int amount) |
---|
486 | { |
---|
487 | for (unsigned int i = 0; i < amount; ++i) |
---|
488 | this->botclass_.fabricate(this->getContext()); |
---|
489 | } |
---|
490 | |
---|
491 | void Gametype::killBots(unsigned int amount) |
---|
492 | { |
---|
493 | unsigned int i = 0; |
---|
494 | for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); ) |
---|
495 | { |
---|
496 | if (it->getGametype() == this) |
---|
497 | { |
---|
498 | (it++)->destroy(); |
---|
499 | ++i; |
---|
500 | } |
---|
501 | else |
---|
502 | ++it; |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | void Gametype::addTime(float t) |
---|
507 | { |
---|
508 | if (this->timeLimit_ == 0) |
---|
509 | this->time_ -= t; |
---|
510 | else |
---|
511 | this->time_ += t; |
---|
512 | } |
---|
513 | |
---|
514 | void Gametype::removeTime(float t) |
---|
515 | { |
---|
516 | if (this->timeLimit_ == 0) |
---|
517 | this->time_ += t; |
---|
518 | else |
---|
519 | this->time_ -= t; |
---|
520 | } |
---|
521 | |
---|
522 | void Gametype::resetTimer() |
---|
523 | { |
---|
524 | this->resetTimer(timeLimit_); |
---|
525 | } |
---|
526 | |
---|
527 | void Gametype::resetTimer(float t) |
---|
528 | { |
---|
529 | this->timeLimit_ = t; |
---|
530 | this->time_ = t; |
---|
531 | } |
---|
532 | |
---|
533 | void Gametype::showMenu() |
---|
534 | { |
---|
535 | GSLevel::startMainMenu(); |
---|
536 | } |
---|
537 | |
---|
538 | GSLevelMementoState* Gametype::exportMementoState() |
---|
539 | { |
---|
540 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
541 | { |
---|
542 | if (it->first->isHumanPlayer() && it->first->getControllableEntity() && it->first->getControllableEntity()->getCamera()) |
---|
543 | { |
---|
544 | Camera* camera = it->first->getControllableEntity()->getCamera(); |
---|
545 | |
---|
546 | GametypeMementoState* state = new GametypeMementoState(); |
---|
547 | state->cameraPosition_ = camera->getWorldPosition(); |
---|
548 | state->cameraOrientation_ = camera->getWorldOrientation(); |
---|
549 | state->sceneName_ = camera->getScene()->getName(); |
---|
550 | return state; |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | return NULL; |
---|
555 | } |
---|
556 | |
---|
557 | void Gametype::importMementoState(const std::vector<GSLevelMementoState*>& states) |
---|
558 | { |
---|
559 | // find correct memento state |
---|
560 | GametypeMementoState* state = NULL; |
---|
561 | for (size_t i = 0; i < states.size(); ++i) |
---|
562 | { |
---|
563 | state = dynamic_cast<GametypeMementoState*>(states[i]); |
---|
564 | if (state) |
---|
565 | break; |
---|
566 | } |
---|
567 | |
---|
568 | if (!state) |
---|
569 | return; |
---|
570 | |
---|
571 | // find correct scene |
---|
572 | Scene* scene = NULL; |
---|
573 | for (ObjectList<Scene>::iterator it = ObjectList<Scene>::begin(); it != ObjectList<Scene>::end(); ++it) |
---|
574 | { |
---|
575 | if (it->getName() == state->sceneName_) |
---|
576 | { |
---|
577 | scene = *it; |
---|
578 | break; |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | if (!scene) |
---|
583 | { |
---|
584 | orxout(internal_warning) << "Could not find scene with name " << state->sceneName_ << endl; |
---|
585 | return; |
---|
586 | } |
---|
587 | |
---|
588 | // find correct player and assign default entity with original position & orientation |
---|
589 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
590 | { |
---|
591 | if (it->first->isHumanPlayer()) |
---|
592 | { |
---|
593 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(scene->getContext()); |
---|
594 | entity->setPosition(state->cameraPosition_); |
---|
595 | entity->setOrientation(state->cameraOrientation_); |
---|
596 | it->first->startControl(entity); |
---|
597 | break; |
---|
598 | } |
---|
599 | } |
---|
600 | } |
---|
601 | } |
---|