Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 27, 2008, 4:08:51 AM (16 years ago)
Author:
landauf
Message:

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/infos/Level.cc

    r2012 r2019  
    3030#include "Level.h"
    3131
    32 #include <OgreSceneManager.h>
    33 #include <OgreLight.h>
    34 
    3532#include "core/CoreIncludes.h"
    3633#include "core/XMLPort.h"
    37 #include "core/Core.h"
    38 #include "core/ConsoleCommand.h"
    3934#include "core/Loader.h"
    4035#include "core/XMLFile.h"
    4136#include "core/Template.h"
    4237
    43 #include "GraphicsEngine.h"
    4438#include "Settings.h"
     39#include "LevelManager.h"
    4540#include "PlayerInfo.h"
     41#include "objects/gametypes/Gametype.h"
    4642
    4743#include "util/Math.h"
     
    4945namespace orxonox
    5046{
    51     SetConsoleCommand(Level, listPlayers, true);
    52 
    5347    CreateFactory(Level);
    5448
    55     Level::Level()
     49    Level::Level(BaseObject* creator) : Info(creator)
    5650    {
    5751        RegisterObject(Level);
    5852
    59         this->rootGametype_ = 0;
    6053        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();
    7255
    7356        COUT(0) << "created Level" << std::endl;
    7457    }
    7558
    76     Level* Level::getActiveLevel()
     59    Level::~Level()
    7760    {
    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);
    8164
    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_);
    9467        }
    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;
    14468    }
    14569
     
    15074        XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode);
    15175        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));
    15476
    155         this->xmlfile_ = this->getFilename();
     77        XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
    15678    }
    15779
    15880    void Level::registerVariables()
    15981    {
    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));
    16183        REGISTERSTRING(this->name_,        network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::changedName));
    16284        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));
    16585    }
    16686
    16787    void Level::networkcallback_applyXMLFile()
    16888    {
    169         COUT(0) << "Loading level \"" << this->xmlfile_ << "\"..." << std::endl;
     89        COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl;
    17090
    17191        ClassTreeMask mask;
     
    17393        mask.include(Class(Template));
    17494
    175         XMLFile* file = new XMLFile(Settings::getDataPath() + this->xmlfile_, mask);
     95        this->xmlfile_ = new XMLFile(Settings::getDataPath() + this->xmlfilename_, mask);
    17696
    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_);
    19598    }
    19699
     
    198101    {
    199102        Identifier* identifier = ClassByString(gametype);
    200         if (identifier)
     103        if (identifier && identifier->isA(Class(Gametype)))
    201104        {
    202105            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);
    206114        }
    207115    }
     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    }
    208146}
Note: See TracChangeset for help on using the changeset viewer.