Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

store the next checkpoints in a set instead of Vector3
+ some refactoring in SpaceRaceManager

File size: 5.9 KB
Line 
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
41
42namespace orxonox
43{
44    CreateFactory(SpaceRaceManager);
45
46    SpaceRaceManager::SpaceRaceManager(BaseObject* creator) : BaseObject(creator)
47    {
48        RegisterObject(SpaceRaceManager);
49
50        this->firstcheckpointvisible_ = false;
51    }
52
53    SpaceRaceManager::~SpaceRaceManager()
54    {
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_)
71        {
72            this->checkpoints_[0]->setRadarVisibility(true);
73            this->firstcheckpointvisible_ = true;
74        }
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        }
81    }
82
83    void SpaceRaceManager::addCheckpoint(RaceCheckPoint* checkpoint)
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    }
95
96    /**
97        @brief Returns the checkpoint with the given checkpoint-index (@see RaceCheckPoint::getCheckpointIndex).
98    */
99    RaceCheckPoint* SpaceRaceManager::findCheckpoint(int index) const
100    {
101        for (size_t i = 0; i < this->checkpoints_.size(); ++i)
102            if (this->checkpoints_[i]->getCheckpointIndex() == index)
103                return this->checkpoints_[i];
104        return 0;
105    }
106
107    bool SpaceRaceManager::reachedValidCheckpoint(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint, PlayerInfo* player) const
108    {
109        if (oldCheckpoint)
110        {
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;
117        }
118        else
119        {
120            // the player hasn't visited a checkpoint yet, so he must reach the checkpoint with index 0 (hack?)
121            return (newCheckpoint->getCheckpointIndex() == 0);
122        }
123    }
124
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))
135        {
136            // the player reached a valid checkpoint
137            gametype->getClock().capture();
138            float time = gametype->getClock().getSecondsPrecise();
139            if (newCheckpoint->getTimeLimit() != 0 && time > newCheckpoint->getTimeLimit())
140            {
141                // time's up - the player has lost the game
142                gametype->setTimeIsUp();
143                gametype->end();
144            }
145            else if (newCheckpoint->isLast())
146            {
147                // the last checkpoint was reached - the player has won the game
148                gametype->end();
149            }
150            else
151                        {
152                // adjust the radarvisibility
153                gametype->newCheckpointReached(newCheckpoint, player);
154                this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
155            }
156        }
157
158        newCheckpoint->resetPlayer();
159    }
160
161    void SpaceRaceManager::updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const
162    {
163        if (oldCheckpoint)
164        {
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);
168        }
169
170        if (newCheckpoint)
171        {
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);
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.