- Timestamp:
- Oct 27, 2008, 4:08:51 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/objects/infos/Level.cc
r2012 r2019 30 30 #include "Level.h" 31 31 32 #include <OgreSceneManager.h>33 #include <OgreLight.h>34 35 32 #include "core/CoreIncludes.h" 36 33 #include "core/XMLPort.h" 37 #include "core/Core.h"38 #include "core/ConsoleCommand.h"39 34 #include "core/Loader.h" 40 35 #include "core/XMLFile.h" 41 36 #include "core/Template.h" 42 37 43 #include "GraphicsEngine.h"44 38 #include "Settings.h" 39 #include "LevelManager.h" 45 40 #include "PlayerInfo.h" 41 #include "objects/gametypes/Gametype.h" 46 42 47 43 #include "util/Math.h" … … 49 45 namespace orxonox 50 46 { 51 SetConsoleCommand(Level, listPlayers, true);52 53 47 CreateFactory(Level); 54 48 55 Level::Level( )49 Level::Level(BaseObject* creator) : Info(creator) 56 50 { 57 51 RegisterObject(Level); 58 52 59 this->rootGametype_ = 0;60 53 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 54 this->xmlfilename_ = this->getFilename(); 72 55 73 56 COUT(0) << "created Level" << std::endl; 74 57 } 75 58 76 Level * Level::getActiveLevel()59 Level::~Level() 77 60 { 78 for (ObjectList<Level>::iterator it = ObjectList<Level>::begin(); it != ObjectList<Level>::end(); ++it)79 if (it->isActive())80 return (*it);61 if (this->isInitialized()) 62 { 63 LevelManager::getInstance().releaseActivity(this); 81 64 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; 65 if (this->xmlfile_) 66 Loader::unload(this->xmlfile_); 94 67 } 95 else96 {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 else114 {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 instance125 PlayerInfo* player = new PlayerInfo();126 player->setGametype(this->getGametype());127 player->setClientID(clientID);128 129 // add to clients-map130 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-map139 PlayerInfo* player = this->clients_[clientID];140 this->clients_.erase(clientID);141 142 // delete PlayerInfo instance143 delete player;144 68 } 145 69 … … 150 74 XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode); 151 75 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 76 155 this->xmlfile_ = this->getFilename();77 XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); 156 78 } 157 79 158 80 void Level::registerVariables() 159 81 { 160 REGISTERSTRING(this->xmlfile _,network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));82 REGISTERSTRING(this->xmlfilename_, network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile)); 161 83 REGISTERSTRING(this->name_, network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::changedName)); 162 84 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 85 } 166 86 167 87 void Level::networkcallback_applyXMLFile() 168 88 { 169 COUT(0) << "Loading level \"" << this->xmlfile _ << "\"..." << std::endl;89 COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl; 170 90 171 91 ClassTreeMask mask; … … 173 93 mask.include(Class(Template)); 174 94 175 XMLFile* file = new XMLFile(Settings::getDataPath() + this->xmlfile_, mask);95 this->xmlfile_ = new XMLFile(Settings::getDataPath() + this->xmlfilename_, mask); 176 96 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; 97 Loader::open(this->xmlfile_); 195 98 } 196 99 … … 198 101 { 199 102 Identifier* identifier = ClassByString(gametype); 200 if (identifier )103 if (identifier && identifier->isA(Class(Gametype))) 201 104 { 202 105 this->gametype_ = gametype; 203 this->gametypeIdentifier_ = identifier; 204 this->rootGametype_ = this->gametypeIdentifier_.fabricate(); 205 this->getConnectedClients(); 106 107 Gametype* rootgametype = dynamic_cast<Gametype*>(identifier->fabricate(this)); 108 this->setGametype(rootgametype); 109 110 for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) 111 (*it)->setGametype(rootgametype); 112 113 LevelManager::getInstance().requestActivity(this); 206 114 } 207 115 } 116 117 118 void Level::addObject(BaseObject* object) 119 { 120 this->objects_.push_back(object); 121 object->setGametype(this->getGametype()); 122 } 123 124 BaseObject* Level::getObject(unsigned int index) const 125 { 126 unsigned int i = 0; 127 for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) 128 { 129 if (i == index) 130 return (*it); 131 ++i; 132 } 133 return 0; 134 } 135 136 void Level::playerEntered(PlayerInfo* player) 137 { 138 COUT(0) << "player entered level" << std::endl; 139 player->setGametype(this->getGametype()); 140 } 141 142 void Level::playerLeft(PlayerInfo* player) 143 { 144 player->setGametype(0); 145 } 208 146 }
Note: See TracChangeset
for help on using the changeset viewer.