[1940] | 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 | |
---|
[1947] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[2012] | 30 | #include "Level.h" |
---|
[1940] | 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
[2006] | 34 | #include "core/Loader.h" |
---|
[2010] | 35 | #include "core/XMLFile.h" |
---|
[2006] | 36 | #include "core/Template.h" |
---|
[1940] | 37 | |
---|
[2006] | 38 | #include "Settings.h" |
---|
[2019] | 39 | #include "LevelManager.h" |
---|
[2006] | 40 | #include "PlayerInfo.h" |
---|
[2019] | 41 | #include "objects/gametypes/Gametype.h" |
---|
[1940] | 42 | |
---|
[2006] | 43 | #include "util/Math.h" |
---|
| 44 | |
---|
[1940] | 45 | namespace orxonox |
---|
| 46 | { |
---|
[2012] | 47 | CreateFactory(Level); |
---|
[1940] | 48 | |
---|
[2019] | 49 | Level::Level(BaseObject* creator) : Info(creator) |
---|
[1940] | 50 | { |
---|
[2012] | 51 | RegisterObject(Level); |
---|
[1940] | 52 | |
---|
| 53 | this->registerVariables(); |
---|
[2032] | 54 | this->xmlfilename_ = this->getFilename(); |
---|
[1940] | 55 | |
---|
[2032] | 56 | if (this->xmlfilename_.length() >= Settings::getDataPath().length()) |
---|
| 57 | this->xmlfilename_ = this->xmlfilename_.substr(Settings::getDataPath().length()); |
---|
| 58 | |
---|
[2012] | 59 | COUT(0) << "created Level" << std::endl; |
---|
[1940] | 60 | } |
---|
| 61 | |
---|
[2019] | 62 | Level::~Level() |
---|
[2006] | 63 | { |
---|
[2019] | 64 | if (this->isInitialized()) |
---|
[2006] | 65 | { |
---|
[2019] | 66 | LevelManager::getInstance().releaseActivity(this); |
---|
[2006] | 67 | |
---|
[2019] | 68 | if (this->xmlfile_) |
---|
| 69 | Loader::unload(this->xmlfile_); |
---|
[2006] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[2012] | 73 | void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1940] | 74 | { |
---|
[2012] | 75 | SUPER(Level, XMLPort, xmlelement, mode); |
---|
[1940] | 76 | |
---|
[2012] | 77 | XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode); |
---|
| 78 | XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); |
---|
[2006] | 79 | |
---|
[2019] | 80 | XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); |
---|
[1940] | 81 | } |
---|
| 82 | |
---|
[2012] | 83 | void Level::registerVariables() |
---|
[1940] | 84 | { |
---|
[2019] | 85 | REGISTERSTRING(this->xmlfilename_, network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile)); |
---|
[2012] | 86 | REGISTERSTRING(this->name_, network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::changedName)); |
---|
[2010] | 87 | REGISTERSTRING(this->description_, network::direction::toclient); |
---|
[1940] | 88 | } |
---|
| 89 | |
---|
[2012] | 90 | void Level::networkcallback_applyXMLFile() |
---|
[2006] | 91 | { |
---|
[2019] | 92 | COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl; |
---|
[2006] | 93 | |
---|
| 94 | ClassTreeMask mask; |
---|
| 95 | mask.exclude(Class(BaseObject)); |
---|
| 96 | mask.include(Class(Template)); |
---|
| 97 | |
---|
[2019] | 98 | this->xmlfile_ = new XMLFile(Settings::getDataPath() + this->xmlfilename_, mask); |
---|
[2006] | 99 | |
---|
[2019] | 100 | Loader::open(this->xmlfile_); |
---|
[2006] | 101 | } |
---|
| 102 | |
---|
[2019] | 103 | void Level::setGametypeString(const std::string& gametype) |
---|
[1940] | 104 | { |
---|
[2019] | 105 | Identifier* identifier = ClassByString(gametype); |
---|
| 106 | if (identifier && identifier->isA(Class(Gametype))) |
---|
| 107 | { |
---|
| 108 | this->gametype_ = gametype; |
---|
[1940] | 109 | |
---|
[2019] | 110 | Gametype* rootgametype = dynamic_cast<Gametype*>(identifier->fabricate(this)); |
---|
| 111 | this->setGametype(rootgametype); |
---|
| 112 | |
---|
| 113 | for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) |
---|
| 114 | (*it)->setGametype(rootgametype); |
---|
| 115 | |
---|
| 116 | LevelManager::getInstance().requestActivity(this); |
---|
| 117 | } |
---|
[1940] | 118 | } |
---|
| 119 | |
---|
[2019] | 120 | |
---|
| 121 | void Level::addObject(BaseObject* object) |
---|
[1940] | 122 | { |
---|
[2019] | 123 | this->objects_.push_back(object); |
---|
| 124 | object->setGametype(this->getGametype()); |
---|
[1940] | 125 | } |
---|
| 126 | |
---|
[2019] | 127 | BaseObject* Level::getObject(unsigned int index) const |
---|
[1940] | 128 | { |
---|
[2019] | 129 | unsigned int i = 0; |
---|
| 130 | for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) |
---|
[1940] | 131 | { |
---|
[2019] | 132 | if (i == index) |
---|
| 133 | return (*it); |
---|
| 134 | ++i; |
---|
[1940] | 135 | } |
---|
[2019] | 136 | return 0; |
---|
[1940] | 137 | } |
---|
[2019] | 138 | |
---|
| 139 | void Level::playerEntered(PlayerInfo* player) |
---|
| 140 | { |
---|
| 141 | COUT(0) << "player entered level" << std::endl; |
---|
| 142 | player->setGametype(this->getGametype()); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void Level::playerLeft(PlayerInfo* player) |
---|
| 146 | { |
---|
| 147 | player->setGametype(0); |
---|
| 148 | } |
---|
[1940] | 149 | } |
---|