Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/gametypes/SpaceRaceManager.cc @ 9348

Last change on this file since 9348 was 9348, checked in by landauf, 12 years ago

merged branch presentation2012merge back to trunk

  • Property svn:eol-style set to native
File size: 5.9 KB
RevLine 
[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
[8968]40
41
[8911]42namespace orxonox
43{
[8940]44    CreateFactory(SpaceRaceManager);
[8911]45
46    SpaceRaceManager::SpaceRaceManager(BaseObject* creator) : BaseObject(creator)
47    {
48        RegisterObject(SpaceRaceManager);
[9260]49
50        this->firstcheckpointvisible_ = false;
[8911]51    }
52
[8970]53    SpaceRaceManager::~SpaceRaceManager()
[8915]54    {
[9260]55        for (size_t i = 0; i < this->checkpoints_.size(); ++i)
56            this->checkpoints_[i]->destroy();
57    }
58
59    void SpaceRaceManager::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(SpaceRaceManager, XMLPort, xmlelement, mode);
62
63        XMLPortObject(SpaceRaceManager, RaceCheckPoint, "checkpoints", addCheckpoint, getCheckpoint,  xmlelement, mode);
64    }
65
66    void SpaceRaceManager::tick(float dt)
67    {
68        SUPER(SpaceRaceManager,tick,dt);
69
70        if (this->checkpoints_[0] != NULL && !this->firstcheckpointvisible_)
[8915]71        {
[9260]72            this->checkpoints_[0]->setRadarVisibility(true);
73            this->firstcheckpointvisible_ = true;
[8915]74        }
[9260]75
76        for (size_t i = 0; i < this->checkpoints_.size(); ++i)
77        {
78            if (this->checkpoints_[i]->getPlayer() != NULL)
79                this->checkpointReached(this->checkpoints_[i], this->checkpoints_[i]->getPlayer());
80        }
[8915]81    }
[9260]82
[8970]83    void SpaceRaceManager::addCheckpoint(RaceCheckPoint* checkpoint)
[8915]84    {
85        this->checkpoints_.push_back(checkpoint);
86    }
87
88    RaceCheckPoint* SpaceRaceManager::getCheckpoint(unsigned int index) const
89    {
90        if (index < this->checkpoints_.size())
91            return this->checkpoints_[index];
92        else
93            return 0;
94    }
[9260]95
[9262]96    /**
97        @brief Returns the checkpoint with the given checkpoint-index (@see RaceCheckPoint::getCheckpointIndex).
98    */
99    RaceCheckPoint* SpaceRaceManager::findCheckpoint(int index) const
[8959]100    {
[8970]101        for (size_t i = 0; i < this->checkpoints_.size(); ++i)
[9262]102            if (this->checkpoints_[i]->getCheckpointIndex() == index)
103                return this->checkpoints_[i];
104        return 0;
[8959]105    }
[8911]106
[9263]107    bool SpaceRaceManager::reachedValidCheckpoint(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint, PlayerInfo* player) const
[8970]108    {
[9263]109        if (oldCheckpoint)
[8970]110        {
[9263]111            // the player already visited an old checkpoint; see which checkpoints are possible now
112            const std::set<int>& possibleCheckpoints = oldCheckpoint->getNextCheckpoints();
113            for (std::set<int>::const_iterator it = possibleCheckpoints.begin(); it != possibleCheckpoints.end(); ++it)
114                if (this->findCheckpoint(*it) == newCheckpoint)
115                    return true;
116            return false;
[8970]117        }
118        else
119        {
[9263]120            // the player hasn't visited a checkpoint yet, so he must reach the checkpoint with index 0 (hack?)
121            return (newCheckpoint->getCheckpointIndex() == 0);
[8970]122        }
[9263]123    }
[9260]124
[9263]125    void SpaceRaceManager::checkpointReached(RaceCheckPoint* newCheckpoint, PlayerInfo* player)
126    {
127        SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
128        assert(gametype);
129        if (!gametype)
130            return;
131
132        RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player);
133
134        if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player))
[8959]135        {
[9263]136            // the player reached a valid checkpoint
[9260]137            gametype->getClock().capture();
138            float time = gametype->getClock().getSecondsPrecise();
[9263]139            if (newCheckpoint->getTimeLimit() != 0 && time > newCheckpoint->getTimeLimit())
[8959]140            {
[9263]141                // time's up - the player has lost the game
[9260]142                gametype->setTimeIsUp();
[8959]143                gametype->end();
144            }
[9263]145            else if (newCheckpoint->isLast())
146            {
147                // the last checkpoint was reached - the player has won the game
[8959]148                gametype->end();
[9263]149            }
[8959]150            else
[9348]151            {
[9263]152                // adjust the radarvisibility
153                gametype->newCheckpointReached(newCheckpoint, player);
154                this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
[8959]155            }
156        }
[9260]157
[9263]158        newCheckpoint->resetPlayer();
[8959]159    }
[9260]160
[9263]161    void SpaceRaceManager::updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const
[8970]162    {
[9263]163        if (oldCheckpoint)
[8970]164        {
[9263]165            const std::set<int>& oldVisible = oldCheckpoint->getNextCheckpoints();
166            for (std::set<int>::const_iterator it = oldVisible.begin(); it != oldVisible.end(); ++it)
167                this->findCheckpoint(*it)->setRadarVisibility(false);
[8970]168        }
[9263]169
170        if (newCheckpoint)
[8970]171        {
[9263]172            newCheckpoint->setRadarVisibility(false);
173
174            const std::set<int>& newVisible = newCheckpoint->getNextCheckpoints();
175            for (std::set<int>::const_iterator it = newVisible.begin(); it != newVisible.end(); ++it)
176                this->findCheckpoint(*it)->setRadarVisibility(true);
[8970]177        }
[8968]178    }
[8970]179}
Note: See TracBrowser for help on using the repository browser.