Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed hard coded sound in Level, Engine and PongBall and replaced SoundMainMenu with a simple SoundBase.

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