Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/Level.cc @ 5738

Last change on this file since 5738 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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