Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed a bug in Level

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