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