Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound2/src/orxonox/objects/Level.cc @ 3013

Last change on this file since 3013 was 3013, checked in by bknecht, 15 years ago

merged sound changes into sound2 use sound2 from now on

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