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 | |
---|
31 | #include <map> |
---|
32 | #include <boost/filesystem.hpp> |
---|
33 | |
---|
34 | #include "core/CommandLine.h" |
---|
35 | #include "core/ConfigValueIncludes.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/Loader.h" |
---|
38 | #include "PlayerManager.h" |
---|
39 | #include "objects/Level.h" |
---|
40 | #include "objects/infos/HumanPlayer.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | SetCommandLineArgument(level, "").shortcut("l").information("Default level file (overrides LevelManager::defaultLevelName_ configValue)"); |
---|
45 | |
---|
46 | LevelManager* LevelManager::singletonRef_s = 0; |
---|
47 | |
---|
48 | LevelManager::LevelManager() |
---|
49 | { |
---|
50 | assert(singletonRef_s == 0); |
---|
51 | singletonRef_s = this; |
---|
52 | |
---|
53 | RegisterRootObject(LevelManager); |
---|
54 | this->setConfigValues(); |
---|
55 | |
---|
56 | // check override |
---|
57 | if (!CommandLine::getArgument("level")->hasDefaultValue()) |
---|
58 | { |
---|
59 | ModifyConfigValue(defaultLevelName_, tset, CommandLine::getValue("level").getString()); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | LevelManager::~LevelManager() |
---|
64 | { |
---|
65 | assert(singletonRef_s != 0); |
---|
66 | singletonRef_s = 0; |
---|
67 | } |
---|
68 | |
---|
69 | void LevelManager::setConfigValues() |
---|
70 | { |
---|
71 | SetConfigValue(defaultLevelName_, "presentation_dm.oxw") |
---|
72 | .description("Sets the preselection of the level in the main menu."); |
---|
73 | } |
---|
74 | |
---|
75 | void LevelManager::requestActivity(Level* level) |
---|
76 | { |
---|
77 | this->levels_s.push_back(level); |
---|
78 | if (this->levels_s.size() == 1) |
---|
79 | this->activateNextLevel(); |
---|
80 | } |
---|
81 | |
---|
82 | void LevelManager::releaseActivity(Level* level) |
---|
83 | { |
---|
84 | if (this->levels_s.size() > 0) |
---|
85 | { |
---|
86 | if (this->levels_s.front() == level) |
---|
87 | { |
---|
88 | level->setActive(false); |
---|
89 | this->levels_s.pop_front(); |
---|
90 | this->activateNextLevel(); |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | for (std::list<Level*>::iterator it = this->levels_s.begin(); it != this->levels_s.end(); ++it) |
---|
95 | if ((*it) == level) |
---|
96 | this->levels_s.erase(it); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | Level* LevelManager::getActiveLevel() |
---|
102 | { |
---|
103 | if (this->levels_s.size() > 0) |
---|
104 | return this->levels_s.front(); |
---|
105 | else |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | void LevelManager::activateNextLevel() |
---|
110 | { |
---|
111 | if (this->levels_s.size() > 0) |
---|
112 | { |
---|
113 | this->levels_s.front()->setActive(true); |
---|
114 | for (std::map<unsigned int, PlayerInfo*>::const_iterator it = PlayerManager::getInstance().getClients().begin(); it != PlayerManager::getInstance().getClients().end(); ++it) |
---|
115 | this->levels_s.front()->playerEntered(it->second); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | void LevelManager::setDefaultLevel(const std::string& levelName) |
---|
120 | { |
---|
121 | ModifyConfigValue(defaultLevelName_, set, levelName); |
---|
122 | } |
---|
123 | |
---|
124 | const std::string& LevelManager::getDefaultLevel() const |
---|
125 | { |
---|
126 | return defaultLevelName_; |
---|
127 | } |
---|
128 | |
---|
129 | std::string LevelManager::getAvailableLevelListItem(unsigned int index) const |
---|
130 | { |
---|
131 | if (index >= availableLevels_.size()) |
---|
132 | return std::string(); |
---|
133 | else |
---|
134 | return availableLevels_[index]; |
---|
135 | } |
---|
136 | |
---|
137 | void LevelManager::compileAvailableLevelList() |
---|
138 | { |
---|
139 | availableLevels_.clear(); |
---|
140 | |
---|
141 | boost::filesystem::directory_iterator file(Core::getMediaPathString() + "levels"); |
---|
142 | boost::filesystem::directory_iterator end; |
---|
143 | |
---|
144 | while (file != end) |
---|
145 | { |
---|
146 | if (!boost::filesystem::is_directory(*file) && file->string()[file->string().length()-1] != '~') |
---|
147 | { |
---|
148 | std::string filename = file->path().leaf(); |
---|
149 | if (filename.length() > 4) |
---|
150 | availableLevels_.push_back(filename.substr(0,filename.length()-4)); |
---|
151 | } |
---|
152 | ++file; |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|