Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed bug in SpaceRaceManager - it used the wrong index to identify Checkpoints (the order in which they appear in the XML file instead of RaceCheckPoint::getCheckpointIndex())
+ some more cleanup

File size: 5.6 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    void SpaceRaceManager::checkpointReached(RaceCheckPoint* check, PlayerInfo* player)
108    {
109        SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
110        assert(gametype);
111
112        bool reachedValidCheckpoint = false;
113
114        int index = gametype->getCheckpointReached(player);
115        if (index > -1)
116        {
117            Vector3 v = this->findCheckpoint(index)->getNextcheckpoint();
118
119            if (this->findCheckpoint(v.x) == check)
120            {
121                reachedValidCheckpoint = true;
122            }
123            if (this->findCheckpoint(v.y) == check)
124            {
125                reachedValidCheckpoint = true;
126            }
127            if (this->findCheckpoint(v.z) == check)
128            {
129                reachedValidCheckpoint = true;
130            }
131        }
132        else
133        {
134            reachedValidCheckpoint = (check->getCheckpointIndex() == 0);
135        }
136
137        if (gametype && reachedValidCheckpoint)
138        {
139            gametype->getClock().capture();
140            float time = gametype->getClock().getSecondsPrecise();
141            if (check->getTimeLimit() != 0 && time > check->getTimeLimit())
142            {
143                gametype->setTimeIsUp();
144                gametype->end();
145            }
146            else if (check->isLast())
147                gametype->end();
148            else
149                        {
150                if (index > -1)
151                    this->setRadarVisibility(player, false);
152                else
153                    this->findCheckpoint(0)->setRadarVisibility(false);
154
155                gametype->newCheckpointReached(check, player);
156                this->setRadarVisibility(player, true);
157            }
158        }
159
160        check->resetPlayer();
161    }
162
163    void SpaceRaceManager::setRadarVisibility(PlayerInfo* player, bool bVisible)
164    {
165        SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
166        assert(gametype);
167        int index = gametype->getCheckpointReached(player);
168        Vector3 v = this->findCheckpoint(index)->getNextcheckpoint();
169
170        if (v.x > -1)
171        {
172            this->findCheckpoint(v.x)->setRadarVisibility(bVisible);
173            this->findCheckpoint(v.x)->settingsChanged();
174        }
175        if (v.y > -1)
176        {
177            this->findCheckpoint(v.y)->setRadarVisibility(bVisible);
178            this->findCheckpoint(v.y)->settingsChanged();
179        }
180        if (v.z > -1)
181        {
182            this->findCheckpoint(v.z)->setRadarVisibility(bVisible);
183            this->findCheckpoint(v.z)->settingsChanged();
184        }
185    }
186}
Note: See TracBrowser for help on using the repository browser.