Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 3, 2012, 12:04:35 AM (12 years ago)
Author:
landauf
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/gametypes/SpaceRaceManager.cc

    r9262 r9263  
    105105    }
    106106
    107     void SpaceRaceManager::checkpointReached(RaceCheckPoint* check, PlayerInfo* player)
     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)
    108126    {
    109127        SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
    110128        assert(gametype);
     129        if (!gametype)
     130            return;
    111131
    112         bool reachedValidCheckpoint = false;
     132        RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player);
    113133
    114         int index = gametype->getCheckpointReached(player);
    115         if (index > -1)
     134        if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player))
    116135        {
    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         {
     136            // the player reached a valid checkpoint
    139137            gametype->getClock().capture();
    140138            float time = gametype->getClock().getSecondsPrecise();
    141             if (check->getTimeLimit() != 0 && time > check->getTimeLimit())
     139            if (newCheckpoint->getTimeLimit() != 0 && time > newCheckpoint->getTimeLimit())
    142140            {
     141                // time's up - the player has lost the game
    143142                gametype->setTimeIsUp();
    144143                gametype->end();
    145144            }
    146             else if (check->isLast())
     145            else if (newCheckpoint->isLast())
     146            {
     147                // the last checkpoint was reached - the player has won the game
    147148                gametype->end();
     149            }
    148150            else
    149151                        {
    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);
     152                // adjust the radarvisibility
     153                gametype->newCheckpointReached(newCheckpoint, player);
     154                this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
    157155            }
    158156        }
    159157
    160         check->resetPlayer();
     158        newCheckpoint->resetPlayer();
    161159    }
    162160
    163     void SpaceRaceManager::setRadarVisibility(PlayerInfo* player, bool bVisible)
     161    void SpaceRaceManager::updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const
    164162    {
    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();
     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        }
    169169
    170         if (v.x > -1)
     170        if (newCheckpoint)
    171171        {
    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();
     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);
    184177        }
    185178    }
Note: See TracChangeset for help on using the changeset viewer.