Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/Level.cc @ 5836

Last change on this file since 5836 was 5836, checked in by rgrieder, 15 years ago

Extracted path related parts of Core into a new PathConfig class. This should decrease the mess in Core.cc a little bit.

  • Property svn:eol-style set to native
File size: 5.5 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 "sound/SoundBase.h"
42#include "LevelManager.h"
43
44namespace orxonox
45{
46    CreateFactory(Level);
47
48    Level::Level(BaseObject* creator) : BaseObject(creator), Synchronisable(creator)
49    {
50        RegisterObject(Level);
51
52        this->registerVariables();
53        this->xmlfilename_ = this->getFilename();
54        this->xmlfile_ = 0;
55        this->ambientsound_ = 0;
56    }
57
58    Level::~Level()
59    {
60        if (this->isInitialized())
61        {
62            if (LevelManager::getInstancePtr())
63                LevelManager::getInstance().releaseActivity(this);
64
65            if (this->xmlfile_)
66                Loader::unload(this->xmlfile_);
67
68            if (this->ambientsound_ != NULL)
69                delete this->ambientsound_;
70        }
71    }
72
73    void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(Level, XMLPort, xmlelement, mode);
76
77        XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode);
78        XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype");
79
80        XMLPortParamLoadOnly(Level, "ambientsound", loadAmbientSound, xmlelement, mode);
81
82        XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
83    }
84
85    void Level::registerVariables()
86    {
87        registerVariable(this->xmlfilename_, VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));
88        registerVariable(this->name_,        VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::changedName));
89        registerVariable(this->description_, VariableDirection::ToClient);
90    }
91
92    void Level::networkcallback_applyXMLFile()
93    {
94        COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl;
95
96        ClassTreeMask mask;
97        mask.exclude(Class(BaseObject));
98        mask.include(Class(Template));
99        mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay
100
101        this->xmlfile_ = new XMLFile(mask, this->xmlfilename_);
102
103        Loader::open(this->xmlfile_);
104    }
105
106    void Level::setGametypeString(const std::string& gametype)
107    {
108        Identifier* identifier = ClassByString(gametype);
109
110        if (!identifier || !identifier->isA(Class(Gametype)))
111        {
112            COUT(0) << "Error: \"" << gametype << "\" is not a valid gametype." << std::endl;
113            identifier = Class(Gametype);
114            this->gametype_ = "Gametype";
115        }
116        else
117            this->gametype_ = gametype;
118
119std::cout << "Load Gametype: " << this->gametype_ << std::endl;
120
121        Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this));
122        this->setGametype(rootgametype);
123
124std::cout << "root gametype: " << rootgametype->getIdentifier()->getName() << std::endl;
125
126        for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
127            (*it)->setGametype(rootgametype);
128
129        if (LevelManager::getInstancePtr())
130            LevelManager::getInstance().requestActivity(this);
131    }
132
133
134    void Level::addObject(BaseObject* object)
135    {
136        this->objects_.push_back(object);
137        object->setGametype(this->getGametype());
138    }
139
140    BaseObject* Level::getObject(unsigned int index) const
141    {
142        unsigned int i = 0;
143        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
144        {
145            if (i == index)
146                return (*it);
147            ++i;
148        }
149        return 0;
150    }
151
152    void Level::loadAmbientSound(const std::string& filename)
153    {
154        if(filename == "") return;
155        else
156        {
157            if(this->ambientsound_ == NULL)
158            {
159                this->ambientsound_ = new SoundBase();
160            }
161
162            this->ambientsound_->loadFile(filename);
163            this->ambientsound_->play(true);
164        }
165    }
166
167    void Level::playerEntered(PlayerInfo* player)
168    {
169        COUT(3) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ")" << std::endl;
170        player->setGametype(this->getGametype());
171    }
172
173    void Level::playerLeft(PlayerInfo* player)
174    {
175        COUT(3) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ")" << std::endl;
176        player->setGametype(0);
177    }
178}
Note: See TracBrowser for help on using the repository browser.