Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/orxonox/Level.cc @ 10562

Last change on this file since 10562 was 10562, checked in by landauf, 5 years ago

removed unnecessary code - this is all done by passing the context to the BaseObject's constructor

  • Property svn:eol-style set to native
File size: 5.9 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 "Level.h"
30
31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/Loader.h"
34#include "core/Template.h"
35#include "core/XMLFile.h"
36#include "core/XMLPort.h"
37
38#include "infos/PlayerInfo.h"
39#include "gametypes/Gametype.h"
40#include "overlays/OverlayGroup.h"
41#include "LevelManager.h"
42
43namespace orxonox
44{
45    RegisterClass(Level);
46
47    Level::Level(Context* context) : BaseObject(context), Synchronisable(context), Context(context)
48    {
49        RegisterObject(Level);
50
51
52        this->registerVariables();
53        this->xmlfilename_ = this->getFilename();
54        this->xmlfile_ = 0;
55    }
56
57    Level::~Level()
58    {
59        if (this->isInitialized())
60        {
61            if (LevelManager::exists())
62                LevelManager::getInstance().releaseActivity(this);
63
64            if (this->xmlfile_)
65                Loader::getInstance().unload(this->xmlfile_);
66        }
67    }
68
69    void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode)
70    {
71        SUPER(Level, XMLPort, xmlelement, mode);
72
73        XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype");
74
75        XMLPortObject(Level, MeshLodInformation, "lodinformation", addLodInfo, getLodInfo, xmlelement, mode);
76        XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
77    }
78
79    void Level::registerVariables()
80    {
81        registerVariable(this->xmlfilename_,            VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));
82        registerVariable(this->name_,                   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::changedName));
83        registerVariable(this->networkTemplateNames_,   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkCallbackTemplatesChanged));
84    }
85
86    void Level::networkcallback_applyXMLFile()
87    {
88        orxout(user_status) << "Loading level \"" << this->xmlfilename_ << "\"..." << endl;
89
90        ClassTreeMask mask;
91        mask.exclude(Class(BaseObject));
92        mask.include(Class(Template));
93        mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay
94
95        this->xmlfile_ = new XMLFile(mask, this->xmlfilename_);
96
97        Loader::getInstance().load(this->xmlfile_);
98    }
99
100    void Level::networkCallbackTemplatesChanged()
101    {
102        for( std::set<std::string>::iterator it = this->networkTemplateNames_.begin(); it!=this->networkTemplateNames_.end(); ++it )
103        {
104            assert(Template::getTemplate(*it));
105            Template::getTemplate(*it)->applyOn(this);
106        }
107    }
108
109    void Level::setGametypeString(const std::string& gametype)
110    {
111        Identifier* identifier = ClassByString(gametype);
112
113        if (!identifier || !identifier->isA(Class(Gametype)))
114        {
115            orxout(internal_error) << "\"" << gametype << "\" is not a valid gametype." << endl;
116            identifier = Class(Gametype);
117            this->gametype_ = "Gametype";
118        }
119        else
120            this->gametype_ = gametype;
121
122        Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this));
123        this->setGametype(rootgametype);
124
125        for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
126            (*it)->setGametype(rootgametype);
127
128        if (LevelManager::exists())
129            LevelManager::getInstance().requestActivity(this);
130    }
131
132
133    void Level::addObject(BaseObject* object)
134    {
135        this->objects_.push_back(object);
136        object->setLevel(this);
137    }
138
139    BaseObject* Level::getObject(unsigned int index) const
140    {
141        unsigned int i = 0;
142        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
143        {
144            if (i == index)
145                return (*it);
146            ++i;
147        }
148        return 0;
149    }
150
151    void Level::addLodInfo(MeshLodInformation* lodInformation)
152    {
153        std::string meshName = lodInformation->getMeshName();
154//         this->lodInformation_.insert(std::make_pair(meshName,lodInformation));
155        if( this->lodInformation_.find(meshName) != this->lodInformation_.end())
156            orxout(verbose, context::lod) << "replacing lod information for " << meshName << endl;
157        this->lodInformation_[meshName] = lodInformation;
158    }
159
160    MeshLodInformation* Level::getLodInfo(std::string meshName) const
161    {
162        if(this->lodInformation_.find(meshName)!=this->lodInformation_.end())
163            return this->lodInformation_.find(meshName)->second;
164
165        return 0;
166    }
167
168    void Level::playerEntered(PlayerInfo* player)
169    {
170        orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl;
171        player->setGametype(this->getGametype());
172    }
173
174    void Level::playerLeft(PlayerInfo* player)
175    {
176        orxout(internal_info) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl;
177        player->setGametype(0);
178    }
179}
Note: See TracBrowser for help on using the repository browser.