Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/infos/LevelInfo.cc @ 2006

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

many changes, can't remember everything, but these are the most important:

  • attaching entities to other entities works
  • displaying models, lights and shadows works
  • controlling a spectator works
  • removed an update hack in PositionableEntity because I've found a much better solution

and with "works" I mean: it works on client, server and standalone

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