[8911] | 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: |
---|
[9260] | 23 | * Celine Eggenberger |
---|
[8911] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "SpaceRaceManager.h" |
---|
[8934] | 30 | #include "SpaceRace.h" |
---|
[8940] | 31 | #include "infos/PlayerInfo.h" |
---|
[8911] | 32 | |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | |
---|
| 35 | #include "core/CoreIncludes.h" |
---|
| 36 | |
---|
| 37 | #include "util/Convert.h" |
---|
| 38 | #include "util/Math.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[9667] | 42 | RegisterClass(SpaceRaceManager); |
---|
[8911] | 43 | |
---|
[9667] | 44 | SpaceRaceManager::SpaceRaceManager(Context* context) : |
---|
| 45 | BaseObject(context) |
---|
[8911] | 46 | { |
---|
| 47 | RegisterObject(SpaceRaceManager); |
---|
[9526] | 48 | this->race_ = orxonox_cast<SpaceRace*>(this->getGametype().get()); |
---|
| 49 | assert(race_); |
---|
| 50 | //amountOfPlayers=(race_->getPlayers()).size(); |
---|
[9260] | 51 | this->firstcheckpointvisible_ = false; |
---|
[8911] | 52 | } |
---|
| 53 | |
---|
[8970] | 54 | SpaceRaceManager::~SpaceRaceManager() |
---|
[8915] | 55 | { |
---|
[9260] | 56 | for (size_t i = 0; i < this->checkpoints_.size(); ++i) |
---|
[9526] | 57 | this->checkpoints_[i]->destroy(); |
---|
[9260] | 58 | } |
---|
| 59 | |
---|
| 60 | void SpaceRaceManager::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 61 | { |
---|
| 62 | SUPER(SpaceRaceManager, XMLPort, xmlelement, mode); |
---|
| 63 | |
---|
[9526] | 64 | XMLPortObject(SpaceRaceManager, RaceCheckPoint, "checkpoints", addCheckpoint, getCheckpoint, xmlelement, mode); |
---|
[9260] | 65 | } |
---|
| 66 | |
---|
| 67 | void SpaceRaceManager::tick(float dt) |
---|
| 68 | { |
---|
| 69 | SUPER(SpaceRaceManager,tick,dt); |
---|
| 70 | |
---|
[9526] | 71 | this->players_ = this->race_->getPlayers(); |
---|
| 72 | |
---|
[9260] | 73 | if (this->checkpoints_[0] != NULL && !this->firstcheckpointvisible_) |
---|
[8915] | 74 | { |
---|
[9260] | 75 | this->checkpoints_[0]->setRadarVisibility(true); |
---|
| 76 | this->firstcheckpointvisible_ = true; |
---|
[8915] | 77 | } |
---|
[9260] | 78 | |
---|
[9526] | 79 | for ( std::map< PlayerInfo*, Player>::iterator it = players_.begin(); it != players_.end(); ++it) |
---|
[9260] | 80 | { |
---|
[9526] | 81 | |
---|
| 82 | for (size_t i = 0; i < this->checkpoints_.size(); ++i) |
---|
| 83 | { |
---|
| 84 | if (this->checkpoints_[i]->playerWasHere(it->first)){ |
---|
| 85 | this->checkpointReached(this->checkpoints_[i], it->first /*this->checkpoints_[i]->getPlayer()*/); |
---|
| 86 | } |
---|
| 87 | } |
---|
[9260] | 88 | } |
---|
[9526] | 89 | |
---|
[8915] | 90 | } |
---|
[9260] | 91 | |
---|
[8970] | 92 | void SpaceRaceManager::addCheckpoint(RaceCheckPoint* checkpoint) |
---|
[8915] | 93 | { |
---|
| 94 | this->checkpoints_.push_back(checkpoint); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | RaceCheckPoint* SpaceRaceManager::getCheckpoint(unsigned int index) const |
---|
| 98 | { |
---|
| 99 | if (index < this->checkpoints_.size()) |
---|
[9526] | 100 | return this->checkpoints_[index]; |
---|
[8915] | 101 | else |
---|
[9526] | 102 | return 0; |
---|
[8915] | 103 | } |
---|
[9260] | 104 | |
---|
[9526] | 105 | std::vector<RaceCheckPoint*> SpaceRaceManager::getAllCheckpoints() |
---|
| 106 | { |
---|
| 107 | return checkpoints_; |
---|
| 108 | } |
---|
| 109 | |
---|
[9262] | 110 | /** |
---|
[9526] | 111 | @brief Returns the checkpoint with the given checkpoint-index (@see RaceCheckPoint::getCheckpointIndex). |
---|
| 112 | */ |
---|
[9262] | 113 | RaceCheckPoint* SpaceRaceManager::findCheckpoint(int index) const |
---|
[8959] | 114 | { |
---|
[8970] | 115 | for (size_t i = 0; i < this->checkpoints_.size(); ++i) |
---|
[9526] | 116 | if (this->checkpoints_[i]->getCheckpointIndex() == index) |
---|
| 117 | return this->checkpoints_[i]; |
---|
[9262] | 118 | return 0; |
---|
[8959] | 119 | } |
---|
[8911] | 120 | |
---|
[9263] | 121 | bool SpaceRaceManager::reachedValidCheckpoint(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint, PlayerInfo* player) const |
---|
[8970] | 122 | { |
---|
[9526] | 123 | if (oldCheckpoint != NULL) |
---|
[8970] | 124 | { |
---|
[9263] | 125 | // the player already visited an old checkpoint; see which checkpoints are possible now |
---|
| 126 | const std::set<int>& possibleCheckpoints = oldCheckpoint->getNextCheckpoints(); |
---|
| 127 | for (std::set<int>::const_iterator it = possibleCheckpoints.begin(); it != possibleCheckpoints.end(); ++it) |
---|
[9526] | 128 | if (this->findCheckpoint(*it) == newCheckpoint) |
---|
| 129 | return true; |
---|
[9263] | 130 | return false; |
---|
[8970] | 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
[9263] | 134 | // the player hasn't visited a checkpoint yet, so he must reach the checkpoint with index 0 (hack?) |
---|
| 135 | return (newCheckpoint->getCheckpointIndex() == 0); |
---|
[8970] | 136 | } |
---|
[9263] | 137 | } |
---|
[9260] | 138 | |
---|
[9263] | 139 | void SpaceRaceManager::checkpointReached(RaceCheckPoint* newCheckpoint, PlayerInfo* player) |
---|
| 140 | { |
---|
| 141 | SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get()); |
---|
| 142 | assert(gametype); |
---|
| 143 | if (!gametype) |
---|
[9526] | 144 | return; |
---|
[9263] | 145 | |
---|
[9526] | 146 | RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player); // returns the last from player reached checkpoint |
---|
[9263] | 147 | |
---|
| 148 | if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player)) |
---|
[8959] | 149 | { |
---|
[9263] | 150 | // the player reached a valid checkpoint |
---|
[9260] | 151 | gametype->getClock().capture(); |
---|
| 152 | float time = gametype->getClock().getSecondsPrecise(); |
---|
[9263] | 153 | if (newCheckpoint->getTimeLimit() != 0 && time > newCheckpoint->getTimeLimit()) |
---|
[8959] | 154 | { |
---|
[9263] | 155 | // time's up - the player has lost the game |
---|
[9260] | 156 | gametype->setTimeIsUp(); |
---|
[8959] | 157 | gametype->end(); |
---|
| 158 | } |
---|
[9263] | 159 | else if (newCheckpoint->isLast()) |
---|
| 160 | { |
---|
| 161 | // the last checkpoint was reached - the player has won the game |
---|
[8959] | 162 | gametype->end(); |
---|
[9263] | 163 | } |
---|
[8959] | 164 | else |
---|
[9348] | 165 | { |
---|
[9263] | 166 | // adjust the radarvisibility |
---|
| 167 | gametype->newCheckpointReached(newCheckpoint, player); |
---|
[9526] | 168 | if(player->isHumanPlayer()) |
---|
| 169 | this->updateRadarVisibility(oldCheckpoint, newCheckpoint); |
---|
[8959] | 170 | } |
---|
| 171 | } |
---|
[9260] | 172 | |
---|
[9526] | 173 | // newCheckpoint->resetPlayer(); loescht playerpointer TODO: check if problems occur |
---|
[8959] | 174 | } |
---|
[9260] | 175 | |
---|
[9263] | 176 | void SpaceRaceManager::updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const |
---|
[8970] | 177 | { |
---|
[9263] | 178 | if (oldCheckpoint) |
---|
[8970] | 179 | { |
---|
[9263] | 180 | const std::set<int>& oldVisible = oldCheckpoint->getNextCheckpoints(); |
---|
| 181 | for (std::set<int>::const_iterator it = oldVisible.begin(); it != oldVisible.end(); ++it) |
---|
[9526] | 182 | this->findCheckpoint(*it)->setRadarVisibility(false); |
---|
[8970] | 183 | } |
---|
[9263] | 184 | |
---|
| 185 | if (newCheckpoint) |
---|
[8970] | 186 | { |
---|
[9263] | 187 | newCheckpoint->setRadarVisibility(false); |
---|
| 188 | |
---|
| 189 | const std::set<int>& newVisible = newCheckpoint->getNextCheckpoints(); |
---|
| 190 | for (std::set<int>::const_iterator it = newVisible.begin(); it != newVisible.end(); ++it) |
---|
[9526] | 191 | this->findCheckpoint(*it)->setRadarVisibility(true); |
---|
[8970] | 192 | } |
---|
[8968] | 193 | } |
---|
[8970] | 194 | } |
---|