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
RevLine 
[1940]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
[1947]29#include "OrxonoxStableHeaders.h"
[2012]30#include "Level.h"
[1940]31
32#include <OgreSceneManager.h>
[2006]33#include <OgreLight.h>
[1940]34
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
[1949]37#include "core/Core.h"
[2006]38#include "core/ConsoleCommand.h"
39#include "core/Loader.h"
[2010]40#include "core/XMLFile.h"
[2006]41#include "core/Template.h"
[1940]42
43#include "GraphicsEngine.h"
[2006]44#include "Settings.h"
45#include "PlayerInfo.h"
[1940]46
[2006]47#include "util/Math.h"
48
[1940]49namespace orxonox
50{
[2012]51    SetConsoleCommand(Level, listPlayers, true);
[2006]52
[2012]53    CreateFactory(Level);
[1940]54
[2012]55    Level::Level()
[1940]56    {
[2012]57        RegisterObject(Level);
[1940]58
[2006]59        this->rootGametype_ = 0;
[1940]60        this->registerVariables();
61
[2006]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
[2012]73        COUT(0) << "created Level" << std::endl;
[1940]74    }
75
[2012]76    Level* Level::getActiveLevel()
[2006]77    {
[2012]78        for (ObjectList<Level>::iterator it = ObjectList<Level>::begin(); it != ObjectList<Level>::end(); ++it)
[2006]79            if (it->isActive())
80                return (*it);
81
82        return 0;
83    }
84
[2012]85    PlayerInfo* Level::getClient(unsigned int clientID)
[2006]86    {
[2012]87        Level* level = Level::getActiveLevel();
[2006]88
[2012]89        if (level)
[2006]90        {
[2012]91            std::map<unsigned int, PlayerInfo*>::const_iterator it = level->clients_.find(clientID);
92            if (it != level->clients_.end())
[2006]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
[2012]104    void Level::listPlayers()
[2006]105    {
[2012]106        Level* level = Level::getActiveLevel();
[2006]107
[2012]108        if (level->getGametype())
[2006]109        {
[2012]110            for (std::set<PlayerInfo*>::const_iterator it = level->getGametype()->getPlayers().begin(); it != level->getGametype()->getPlayers().end(); ++it)
[2006]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
[2012]120    void Level::clientConnected(unsigned int clientID)
[2006]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
[2012]134    void Level::clientDisconnected(unsigned int clientID)
[2006]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
[2012]146    void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[1940]147    {
[2012]148        SUPER(Level, XMLPort, xmlelement, mode);
[1940]149
[2012]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));
[2006]154
[2010]155        this->xmlfile_ = this->getFilename();
[1940]156    }
157
[2012]158    void Level::registerVariables()
[1940]159    {
[2012]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));
[2010]162        REGISTERSTRING(this->description_, network::direction::toclient);
[2012]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));
[1940]165    }
166
[2012]167    void Level::networkcallback_applyXMLFile()
[2006]168    {
[2010]169        COUT(0) << "Loading level \"" << this->xmlfile_ << "\"..." << std::endl;
[2006]170
171        ClassTreeMask mask;
172        mask.exclude(Class(BaseObject));
173        mask.include(Class(Template));
174
[2010]175        XMLFile* file = new XMLFile(Settings::getDataPath() + this->xmlfile_, mask);
[2006]176
[2010]177        Loader::open(file);
[2006]178    }
179
[2012]180    void Level::setSkybox(const std::string& skybox)
[1940]181    {
[1949]182        if (Core::showsGraphics())
[1940]183            if (GraphicsEngine::getInstance().getLevelSceneManager())
184                GraphicsEngine::getInstance().getLevelSceneManager()->setSkyBox(true, skybox);
185
186        this->skybox_ = skybox;
187    }
188
[2012]189    void Level::setAmbientLight(const ColourValue& colour)
[1940]190    {
[1949]191        if (Core::showsGraphics())
[1940]192            GraphicsEngine::getInstance().getLevelSceneManager()->setAmbientLight(colour);
193
194        this->ambientLight_ = colour;
195    }
196
[2012]197    void Level::setGametypeString(const std::string& gametype)
[1940]198    {
199        Identifier* identifier = ClassByString(gametype);
200        if (identifier)
201        {
202            this->gametype_ = gametype;
203            this->gametypeIdentifier_ = identifier;
[2006]204            this->rootGametype_ = this->gametypeIdentifier_.fabricate();
205            this->getConnectedClients();
[1940]206        }
207    }
208}
Note: See TracBrowser for help on using the repository browser.