Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/LevelManager.cc @ 7660

Last change on this file since 7660 was 7648, checked in by dafrick, 14 years ago

Merged releasetodo, containing a new way to describe and tag levels, back to trunk.

  • Property svn:eol-style set to native
File size: 6.2 KB
RevLine 
[2072]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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LevelManager.h"
30
[3196]31#include <map>
[3280]32
[7284]33#include "util/ScopedSingletonManager.h"
[7648]34#include "core/ClassTreeMask.h"
[6021]35#include "core/CommandLineParser.h"
[3280]36#include "core/ConfigValueIncludes.h"
37#include "core/CoreIncludes.h"
[3370]38#include "core/Loader.h"
[6417]39#include "core/Resource.h"
[7648]40#include "core/XMLFile.h"
[2171]41#include "PlayerManager.h"
[5738]42#include "Level.h"
[7648]43#include "LevelInfo.h"
[2072]44
45namespace orxonox
46{
[3280]47    SetCommandLineArgument(level, "").shortcut("l").information("Default level file (overrides LevelManager::defaultLevelName_ configValue)");
48
[5929]49    ManageScopedSingleton(LevelManager, ScopeID::Root, false);
[2072]50
51    LevelManager::LevelManager()
52    {
[3280]53        RegisterRootObject(LevelManager);
54        this->setConfigValues();
55
56        // check override
[6021]57        if (!CommandLineParser::getArgument("level")->hasDefaultValue())
[3280]58        {
[6021]59            ModifyConfigValue(defaultLevelName_, tset, CommandLineParser::getValue("level").getString());
[3280]60        }
[7648]61
62        this->compileAvailableLevelList();
[2072]63    }
64
65    LevelManager::~LevelManager()
66    {
67    }
68
[3280]69    void LevelManager::setConfigValues()
70    {
71        SetConfigValue(defaultLevelName_, "presentation_dm.oxw")
[6417]72            .description("Sets the pre selection of the level in the main menu.");
[3280]73    }
74
[2072]75    void LevelManager::requestActivity(Level* level)
76    {
[7163]77        assert( std::find(this->levels_s.begin(), this->levels_s.end(), level)==this->levels_s.end() );
78        if( std::find(this->levels_s.begin(), this->levels_s.end(), level)!=this->levels_s.end() )
79            return; // level is already in list
[2072]80        this->levels_s.push_back(level);
81        if (this->levels_s.size() == 1)
82            this->activateNextLevel();
83    }
84
85    void LevelManager::releaseActivity(Level* level)
86    {
87        if (this->levels_s.size() > 0)
88        {
89            if (this->levels_s.front() == level)
90            {
91                level->setActive(false);
92                this->levels_s.pop_front();
93                this->activateNextLevel();
94            }
95            else
96            {
97                for (std::list<Level*>::iterator it = this->levels_s.begin(); it != this->levels_s.end(); ++it)
98                    if ((*it) == level)
99                        this->levels_s.erase(it);
100            }
101        }
102    }
103
104    Level* LevelManager::getActiveLevel()
105    {
106        if (this->levels_s.size() > 0)
107            return this->levels_s.front();
108        else
109            return 0;
110    }
111
112    void LevelManager::activateNextLevel()
113    {
114        if (this->levels_s.size() > 0)
115        {
116            this->levels_s.front()->setActive(true);
[2171]117            for (std::map<unsigned int, PlayerInfo*>::const_iterator it = PlayerManager::getInstance().getClients().begin(); it != PlayerManager::getInstance().getClients().end(); ++it)
[2072]118                this->levels_s.front()->playerEntered(it->second);
119        }
120    }
[3280]121
122    void LevelManager::setDefaultLevel(const std::string& levelName)
123    {
124        ModifyConfigValue(defaultLevelName_, set, levelName);
125    }
126
[3370]127    const std::string& LevelManager::getDefaultLevel() const
[3280]128    {
129        return defaultLevelName_;
130    }
[3370]131
[7648]132    unsigned int LevelManager::getNumberOfLevels()
[3370]133    {
[7648]134        this->updateAvailableLevelList();
135
136        return this->availableLevels_.size();
137    }
138
139    LevelInfoItem* LevelManager::getAvailableLevelListItem(unsigned int index) const
140    {
141        if (index >= this->availableLevels_.size())
142            return NULL;
[3370]143        else
[7648]144        {
145            std::map<std::string, LevelInfoItem*>::const_iterator it = this->infos_.find(this->availableLevels_[index]);
146            return it->second;
147        }
[3370]148    }
149
150    void LevelManager::compileAvailableLevelList()
151    {
[6501]152        Ogre::StringVectorPtr levels = Resource::findResourceNames("*.oxw");
[7648]153        // Iterate over all *.oxw level files.
[6501]154        for (Ogre::StringVector::const_iterator it = levels->begin(); it != levels->end(); ++it)
155        {
[7648]156            //TODO: Replace with tag,
[6501]157            if (it->find("old/") != 0)
[3370]158            {
[5695]159                size_t pos = it->find(".oxw");
[7648]160
161                bool infoExists = false;
162                // Load the LevelInfo object from the level file.
163                XMLFile file = XMLFile(*it);
164                ClassTreeMask mask = ClassTreeMask();
165                mask.exclude(ClassIdentifier<BaseObject>::getIdentifier());
166                mask.include(ClassIdentifier<LevelInfo>::getIdentifier());
167                Loader::load(&file, mask, false);
168                for(ObjectList<LevelInfo>::iterator item = ObjectList<LevelInfo>::begin(); item != ObjectList<LevelInfo>::end(); ++item)
169                {
170                    LevelInfoItem* info = item->copy();
171                    if(info->getXMLFilename() == *it)
172                    {
173                        this->infos_.insert(std::pair<std::string, LevelInfoItem*>(it->substr(0, pos),info));
174                        infoExists = true;
175                    }
176                }
177                Loader::unload(&file, mask);
178                if(!infoExists)
179                {
180                    this->infos_.insert(std::pair<std::string, LevelInfoItem*>(it->substr(0, pos), new LevelInfoItem(it->substr(0, pos), *it)));
181                }
182
[6501]183                this->availableLevels_.push_back(it->substr(0, pos));
[3370]184            }
[6501]185        }
[3370]186    }
[7648]187
188    void LevelManager::updateAvailableLevelList(void)
189    {
190        //TODO: Implement some kind of update?
191    }
[2072]192}
Note: See TracBrowser for help on using the repository browser.