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