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