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 "OrxonoxStableHeaders.h" |
---|
30 | #include "LevelInfo.h" |
---|
31 | |
---|
32 | #include <OgreSceneManager.h> |
---|
33 | #include <OgreLight.h> |
---|
34 | |
---|
35 | #include "core/CoreIncludes.h" |
---|
36 | #include "core/XMLPort.h" |
---|
37 | #include "core/Core.h" |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "core/Loader.h" |
---|
40 | #include "core/XMLFile.h" |
---|
41 | #include "core/Template.h" |
---|
42 | |
---|
43 | #include "GraphicsEngine.h" |
---|
44 | #include "Settings.h" |
---|
45 | #include "PlayerInfo.h" |
---|
46 | |
---|
47 | #include "util/Math.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | SetConsoleCommand(LevelInfo, listPlayers, true); |
---|
52 | |
---|
53 | CreateFactory(LevelInfo); |
---|
54 | |
---|
55 | LevelInfo::LevelInfo() |
---|
56 | { |
---|
57 | RegisterObject(LevelInfo); |
---|
58 | |
---|
59 | this->rootGametype_ = 0; |
---|
60 | this->registerVariables(); |
---|
61 | |
---|
62 | // test test test |
---|
63 | { |
---|
64 | Ogre::Light* light; |
---|
65 | light = GraphicsEngine::getInstance().getLevelSceneManager()->createLight("Light0"); |
---|
66 | light->setType(Ogre::Light::LT_DIRECTIONAL); |
---|
67 | light->setDiffuseColour(ColourValue(1.0, 0.9, 0.6, 1.0)); |
---|
68 | light->setSpecularColour(ColourValue(1.0, 0.9, 0.6, 1.0)); |
---|
69 | light->setDirection(1, -0.2, 0.2); |
---|
70 | } |
---|
71 | // test test test |
---|
72 | |
---|
73 | COUT(0) << "created LevelInfo" << std::endl; |
---|
74 | } |
---|
75 | |
---|
76 | LevelInfo* LevelInfo::getActiveLevelInfo() |
---|
77 | { |
---|
78 | for (ObjectList<LevelInfo>::iterator it = ObjectList<LevelInfo>::begin(); it != ObjectList<LevelInfo>::end(); ++it) |
---|
79 | if (it->isActive()) |
---|
80 | return (*it); |
---|
81 | |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | PlayerInfo* LevelInfo::getClient(unsigned int clientID) |
---|
86 | { |
---|
87 | LevelInfo* levelinfo = LevelInfo::getActiveLevelInfo(); |
---|
88 | |
---|
89 | if (levelinfo) |
---|
90 | { |
---|
91 | std::map<unsigned int, PlayerInfo*>::const_iterator it = levelinfo->clients_.find(clientID); |
---|
92 | if (it != levelinfo->clients_.end()) |
---|
93 | return it->second; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it) |
---|
98 | if (it->getClientID() == clientID) |
---|
99 | return (*it); |
---|
100 | } |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | void LevelInfo::listPlayers() |
---|
105 | { |
---|
106 | LevelInfo* levelinfo = LevelInfo::getActiveLevelInfo(); |
---|
107 | |
---|
108 | if (levelinfo->getGametype()) |
---|
109 | { |
---|
110 | for (std::set<PlayerInfo*>::const_iterator it = levelinfo->getGametype()->getPlayers().begin(); it != levelinfo->getGametype()->getPlayers().end(); ++it) |
---|
111 | COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it) |
---|
116 | COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void LevelInfo::clientConnected(unsigned int clientID) |
---|
121 | { |
---|
122 | COUT(0) << "client connected" << std::endl; |
---|
123 | |
---|
124 | // create new PlayerInfo instance |
---|
125 | PlayerInfo* player = new PlayerInfo(); |
---|
126 | player->setGametype(this->getGametype()); |
---|
127 | player->setClientID(clientID); |
---|
128 | |
---|
129 | // add to clients-map |
---|
130 | assert(!this->clients_[clientID]); |
---|
131 | this->clients_[clientID] = player; |
---|
132 | } |
---|
133 | |
---|
134 | void LevelInfo::clientDisconnected(unsigned int clientID) |
---|
135 | { |
---|
136 | COUT(0) << "client disconnected" << std::endl; |
---|
137 | |
---|
138 | // remove from clients-map |
---|
139 | PlayerInfo* player = this->clients_[clientID]; |
---|
140 | this->clients_.erase(clientID); |
---|
141 | |
---|
142 | // delete PlayerInfo instance |
---|
143 | delete player; |
---|
144 | } |
---|
145 | |
---|
146 | void LevelInfo::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
147 | { |
---|
148 | SUPER(LevelInfo, XMLPort, xmlelement, mode); |
---|
149 | |
---|
150 | XMLPortParam(LevelInfo, "description", setDescription, getDescription, xmlelement, mode); |
---|
151 | XMLPortParam(LevelInfo, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); |
---|
152 | XMLPortParam(LevelInfo, "skybox", setSkybox, getSkybox, xmlelement, mode); |
---|
153 | XMLPortParam(LevelInfo, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1)); |
---|
154 | |
---|
155 | this->xmlfile_ = this->getFilename(); |
---|
156 | } |
---|
157 | |
---|
158 | void LevelInfo::registerVariables() |
---|
159 | { |
---|
160 | REGISTERSTRING(this->xmlfile_, network::direction::toclient, new network::NetworkCallback<LevelInfo>(this, &LevelInfo::applyXMLFile)); |
---|
161 | REGISTERSTRING(this->name_, network::direction::toclient, new network::NetworkCallback<LevelInfo>(this, &LevelInfo::changedName)); |
---|
162 | REGISTERSTRING(this->description_, network::direction::toclient); |
---|
163 | REGISTERSTRING(this->skybox_, network::direction::toclient, new network::NetworkCallback<LevelInfo>(this, &LevelInfo::applySkybox)); |
---|
164 | REGISTERDATA(this->ambientLight_, network::direction::toclient, new network::NetworkCallback<LevelInfo>(this, &LevelInfo::applyAmbientLight)); |
---|
165 | } |
---|
166 | |
---|
167 | void LevelInfo::applyXMLFile() |
---|
168 | { |
---|
169 | COUT(0) << "Loading level \"" << this->xmlfile_ << "\"..." << std::endl; |
---|
170 | |
---|
171 | ClassTreeMask mask; |
---|
172 | mask.exclude(Class(BaseObject)); |
---|
173 | mask.include(Class(Template)); |
---|
174 | |
---|
175 | XMLFile* file = new XMLFile(Settings::getDataPath() + this->xmlfile_, mask); |
---|
176 | |
---|
177 | Loader::open(file); |
---|
178 | } |
---|
179 | |
---|
180 | void LevelInfo::setSkybox(const std::string& skybox) |
---|
181 | { |
---|
182 | if (Core::showsGraphics()) |
---|
183 | if (GraphicsEngine::getInstance().getLevelSceneManager()) |
---|
184 | GraphicsEngine::getInstance().getLevelSceneManager()->setSkyBox(true, skybox); |
---|
185 | |
---|
186 | this->skybox_ = skybox; |
---|
187 | } |
---|
188 | |
---|
189 | void LevelInfo::setAmbientLight(const ColourValue& colour) |
---|
190 | { |
---|
191 | if (Core::showsGraphics()) |
---|
192 | GraphicsEngine::getInstance().getLevelSceneManager()->setAmbientLight(colour); |
---|
193 | |
---|
194 | this->ambientLight_ = colour; |
---|
195 | } |
---|
196 | |
---|
197 | void LevelInfo::setGametypeString(const std::string& gametype) |
---|
198 | { |
---|
199 | Identifier* identifier = ClassByString(gametype); |
---|
200 | if (identifier) |
---|
201 | { |
---|
202 | this->gametype_ = gametype; |
---|
203 | this->gametypeIdentifier_ = identifier; |
---|
204 | this->rootGametype_ = this->gametypeIdentifier_.fabricate(); |
---|
205 | this->getConnectedClients(); |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|