Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/infos/Level.cc @ 2012

Last change on this file since 2012 was 2012, checked in by landauf, 16 years ago

renamed LevelInfo as Level

File size: 7.0 KB
Line 
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 "Level.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
49namespace orxonox
50{
51    SetConsoleCommand(Level, listPlayers, true);
52
53    CreateFactory(Level);
54
55    Level::Level()
56    {
57        RegisterObject(Level);
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 Level" << std::endl;
74    }
75
76    Level* Level::getActiveLevel()
77    {
78        for (ObjectList<Level>::iterator it = ObjectList<Level>::begin(); it != ObjectList<Level>::end(); ++it)
79            if (it->isActive())
80                return (*it);
81
82        return 0;
83    }
84
85    PlayerInfo* Level::getClient(unsigned int clientID)
86    {
87        Level* level = Level::getActiveLevel();
88
89        if (level)
90        {
91            std::map<unsigned int, PlayerInfo*>::const_iterator it = level->clients_.find(clientID);
92            if (it != level->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 Level::listPlayers()
105    {
106        Level* level = Level::getActiveLevel();
107
108        if (level->getGametype())
109        {
110            for (std::set<PlayerInfo*>::const_iterator it = level->getGametype()->getPlayers().begin(); it != level->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 Level::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 Level::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 Level::XMLPort(Element& xmlelement, XMLPort::Mode mode)
147    {
148        SUPER(Level, XMLPort, xmlelement, mode);
149
150        XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode);
151        XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype");
152        XMLPortParam(Level, "skybox", setSkybox, getSkybox, xmlelement, mode);
153        XMLPortParam(Level, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1));
154
155        this->xmlfile_ = this->getFilename();
156    }
157
158    void Level::registerVariables()
159    {
160        REGISTERSTRING(this->xmlfile_,     network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));
161        REGISTERSTRING(this->name_,        network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::changedName));
162        REGISTERSTRING(this->description_, network::direction::toclient);
163        REGISTERSTRING(this->skybox_,      network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applySkybox));
164        REGISTERDATA(this->ambientLight_,  network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyAmbientLight));
165    }
166
167    void Level::networkcallback_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 Level::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 Level::setAmbientLight(const ColourValue& colour)
190    {
191        if (Core::showsGraphics())
192            GraphicsEngine::getInstance().getLevelSceneManager()->setAmbientLight(colour);
193
194        this->ambientLight_ = colour;
195    }
196
197    void Level::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}
Note: See TracBrowser for help on using the repository browser.