[507] | 1 | #include <string> |
---|
| 2 | #include <vector> |
---|
[164] | 3 | #include <iostream> |
---|
[507] | 4 | #include <algorithm> |
---|
| 5 | #include <iterator> |
---|
[164] | 6 | |
---|
| 7 | #include "LevelLoader.h" |
---|
[471] | 8 | #include "tinyxml/tinyxml.h" |
---|
[496] | 9 | #include "orxonox/core/CoreIncludes.h" |
---|
[480] | 10 | #include "orxonox/core/Error.h" |
---|
| 11 | #include "orxonox/objects/BaseObject.h" |
---|
[164] | 12 | |
---|
| 13 | using namespace std; |
---|
| 14 | |
---|
| 15 | namespace loader |
---|
| 16 | { |
---|
| 17 | |
---|
[480] | 18 | LevelLoader::LevelLoader(string file, string path) |
---|
[164] | 19 | { |
---|
[480] | 20 | valid_ = false; |
---|
| 21 | |
---|
[164] | 22 | // Load XML level file |
---|
[480] | 23 | path.append("/"); |
---|
| 24 | path.append(file); |
---|
[471] | 25 | |
---|
[474] | 26 | // Open xml file |
---|
[480] | 27 | doc.LoadFile(path); |
---|
[474] | 28 | |
---|
| 29 | // Check if file was loaded |
---|
| 30 | if (doc.LoadFile()) |
---|
[471] | 31 | { |
---|
[474] | 32 | TiXmlHandle hDoc(&doc); |
---|
[480] | 33 | TiXmlHandle hRoot(0); |
---|
[474] | 34 | TiXmlElement* pElem; |
---|
| 35 | |
---|
| 36 | // Check for root element |
---|
| 37 | pElem = hDoc.FirstChildElement("orxonoxworld").Element(); |
---|
| 38 | if (pElem) |
---|
| 39 | { |
---|
| 40 | // Set root element |
---|
| 41 | hRoot = TiXmlHandle(pElem); |
---|
[480] | 42 | rootElement = hRoot.Element(); |
---|
[474] | 43 | |
---|
| 44 | // Set level description |
---|
| 45 | pElem = hRoot.FirstChild("description").Element(); |
---|
| 46 | if (pElem) |
---|
| 47 | { |
---|
| 48 | description_ = pElem->GetText(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | // Set level name |
---|
[480] | 52 | name_ = rootElement->Attribute("name"); |
---|
| 53 | image_ = rootElement->Attribute("image"); |
---|
[474] | 54 | |
---|
[480] | 55 | valid_ = true; |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | orxonox::Error("Level file has no valid root node"); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | orxonox::Error("Could not load level file "); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void LevelLoader::loadLevel() |
---|
| 69 | { |
---|
| 70 | if (valid_) |
---|
| 71 | { |
---|
| 72 | TiXmlElement* loadElem; |
---|
| 73 | TiXmlElement* worldElem; |
---|
| 74 | TiXmlElement* tElem; |
---|
| 75 | TiXmlNode* tNode; |
---|
| 76 | |
---|
| 77 | |
---|
[474] | 78 | // Set loading screen |
---|
[480] | 79 | loadElem = rootElement->FirstChildElement("loading"); |
---|
| 80 | if (loadElem) |
---|
[474] | 81 | { |
---|
| 82 | // Set background |
---|
[480] | 83 | tElem = loadElem->FirstChildElement("background"); |
---|
| 84 | if (tElem) |
---|
[474] | 85 | { |
---|
[480] | 86 | loadingBackgroundColor_ = tElem->Attribute("color"); |
---|
| 87 | loadingBackgroundImage_ = tElem->Attribute("image"); |
---|
[474] | 88 | } |
---|
| 89 | // Set bar |
---|
[480] | 90 | tElem = loadElem->FirstChildElement("bar"); |
---|
| 91 | if (tElem) |
---|
[474] | 92 | { |
---|
[480] | 93 | loadingBarImage_ = tElem->Attribute("image");; |
---|
| 94 | loadingBarTop_ = tElem->Attribute("top"); |
---|
| 95 | loadingBarLeft_ = tElem->Attribute("left"); |
---|
| 96 | loadingBarWidth_ = tElem->Attribute("width"); |
---|
| 97 | loadingBarHeight_ = tElem->Attribute("height"); |
---|
[474] | 98 | } |
---|
[480] | 99 | showLoadingScreen(); |
---|
[474] | 100 | } |
---|
| 101 | |
---|
[480] | 102 | // Load audio |
---|
| 103 | // TODO |
---|
| 104 | |
---|
| 105 | // Load scripts |
---|
| 106 | // TODO |
---|
| 107 | |
---|
| 108 | // Load world |
---|
| 109 | worldElem = rootElement->FirstChildElement("world"); |
---|
| 110 | if (worldElem) |
---|
| 111 | { |
---|
| 112 | tNode = 0; |
---|
| 113 | while( tNode = worldElem->IterateChildren( tNode ) ) |
---|
| 114 | { |
---|
| 115 | tElem = tNode->ToElement(); |
---|
[496] | 116 | orxonox::BaseObject* obj = ID(tElem->Value())->fabricate(); |
---|
[480] | 117 | obj->loadParams(tElem); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | std::cout << "Loading finished!\n\n\n\n\n"; |
---|
[474] | 122 | } |
---|
[471] | 123 | } |
---|
[480] | 124 | |
---|
| 125 | void LevelLoader::showLoadingScreen() |
---|
[471] | 126 | { |
---|
[480] | 127 | std::cout << "\n\n\nThis is Orxonox\nthe hottest 3D action shooter ever to exist\n\n\n"; |
---|
| 128 | std::cout << "Level: " << name() << "\nDescription:" << description() << "\nImage:"<<image()<<"\n\n\n"; |
---|
| 129 | std::cout << "Backgroundcolor: " << loadingBackgroundColor_ << "\nBackgroundimage:" << loadingBackgroundImage_ << "\n\n\n"; |
---|
| 130 | } |
---|
[471] | 131 | |
---|
[507] | 132 | LevelLoader::~LevelLoader() |
---|
| 133 | { |
---|
| 134 | |
---|
| 135 | } |
---|
[474] | 136 | |
---|
| 137 | |
---|
[507] | 138 | string LevelLoader::name() |
---|
[164] | 139 | { |
---|
[507] | 140 | return this->name_; |
---|
[164] | 141 | } |
---|
[507] | 142 | |
---|
| 143 | string LevelLoader::description() |
---|
[164] | 144 | { |
---|
[507] | 145 | return this->description_; |
---|
[164] | 146 | } |
---|
[507] | 147 | |
---|
| 148 | string LevelLoader::image() |
---|
[164] | 149 | { |
---|
[507] | 150 | return this->image_; |
---|
[164] | 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | } |
---|
| 155 | |
---|