From 265c0b2ce58518127e36521785d74d796c5a1993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Fri, 13 Feb 2015 17:14:29 +0100 Subject: [PATCH] Added resetPlayer function to the level. --- Levels/ObjectSetups/Compositions.xml | 4 ++-- application.cc | 5 +++-- level.cc | 10 ++++++++- level.hh | 4 +++- loader.cc | 32 ++++++++++++++++++++++++++++ loader.hh | 3 ++- terrain.cc | 1 + 7 files changed, 52 insertions(+), 7 deletions(-) diff --git a/Levels/ObjectSetups/Compositions.xml b/Levels/ObjectSetups/Compositions.xml index 7792f76..14e78d0 100644 --- a/Levels/ObjectSetups/Compositions.xml +++ b/Levels/ObjectSetups/Compositions.xml @@ -1,7 +1,7 @@ - + 20 false @@ -415,7 +415,7 @@ 0.0 0.0 1.5 - 0.0 + 100.0 diff --git a/application.cc b/application.cc index b16e9c0..e4539a7 100644 --- a/application.cc +++ b/application.cc @@ -14,7 +14,8 @@ void Application::init() std::srand(std::time(NULL)); // choose Level TODO: Choose this in a menu std::string heightmapFilePath = heightmapPath + "heightmapLvl1.png"; - this->level = Level(heightmapFilePath); + std::string levelXmlFilePath = levelXmlPath + "Level1.xml"; + this->level = Level(heightmapFilePath, levelXmlFilePath); level.getPhysics()->init(geometryPath); // Don't change this! ignoredMouseUpdates = 0; @@ -31,7 +32,7 @@ void Application::init() // load Level level.load(); Loader loader = Loader(); - std::string levelXmlFilePath = levelXmlPath + "Level1.xml"; + loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath); graphics.init(&level); diff --git a/level.cc b/level.cc index bb3b82c..3437f42 100644 --- a/level.cc +++ b/level.cc @@ -1,11 +1,13 @@ #include "level.hh" +#include "loader.hh" #include -Level::Level(std::string heightmapFilePath){ +Level::Level(std::string heightmapFilePath, std::string xmlFilePath){ this->terrain = Terrain(heightmapFilePath); // default value skydomeSize = 50.0f; physics = Physics(); + this->xmlFilePath = xmlFilePath; } Level::Level() { @@ -162,6 +164,12 @@ void Level::deleteObject(int objectIndex){ } } +void Level::resetPlayer(){ + Loader loader = Loader(); + glm::vec3 newPosition = loader.reloadPlayerPosition(xmlFilePath, this); + //TODO cameraCenter.setPosition(newPosition); +} + void Level::setStrength(float strength) { this->strength = strength; } diff --git a/level.hh b/level.hh index 69b3412..4cbdf34 100644 --- a/level.hh +++ b/level.hh @@ -20,7 +20,7 @@ extern "C" { class Level { public: - Level(std::string heightmapFilePath); + Level(std::string heightmapFilePath, std::string xmlFilePath); Level(); ~Level(); void load(); @@ -55,6 +55,7 @@ class Level { void addTrigger(Trigger trigger); lua_State* getLuaState(); Terrain* getTerrain(); + void resetPlayer(); private: lua_State* luaState=nullptr; std::vector objects; @@ -71,6 +72,7 @@ class Level { Terrain terrain; float skydomeSize; float strength; + std::string xmlFilePath; }; #endif diff --git a/loader.cc b/loader.cc index 2c49802..0dd03e9 100644 --- a/loader.cc +++ b/loader.cc @@ -507,6 +507,38 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa }//positionConstraints } + + +glm::vec3 Loader::reloadPlayerPosition(std::string filePath, Level* level){ + XMLDocument* doc = new XMLDocument(); + const char* xmlFile = filePath.c_str(); + doc->LoadFile(xmlFile); + if (doc->ErrorID()!=0){ + printf("Could not open ObjectSetupXml!\n"); + exit(-1); + } + //iterate over all compositions in Level.xml + XMLElement* thisComposition = doc->FirstChildElement("composition"); + for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ + int thisType = 0; + errorCheck(thisComposition->FirstChildElement("typeID")->QueryIntText(&thisType)); + if (thisType == 20){ + float compXPos, compYOffset, compZPos; + errorCheck(thisComposition->FirstChildElement("xPos")->QueryFloatText(&compXPos)); + errorCheck(thisComposition->FirstChildElement("yOffset")->QueryFloatText(&compYOffset)); + errorCheck(thisComposition->FirstChildElement("zPos")->QueryFloatText(&compZPos)); + compYOffset += level->getTerrain()->getHeightmap()[int(compXPos-0.5+0.5*level->getTerrain()->getHeightmapHeight())] + [int(compZPos-0.5+0.5*level->getTerrain()->getHeightmapWidth())]; + glm::vec3 position = glm::vec3(compXPos, compYOffset, compZPos); + return position; + } + } + printf("Level.xml contains no player."); + exit(-1); +} + + + void Loader::errorCheck(XMLError error){ if (error) { printf("XMLError: "); diff --git a/loader.hh b/loader.hh index 075d3be..cc0f44d 100644 --- a/loader.hh +++ b/loader.hh @@ -7,8 +7,9 @@ class Loader { public: Loader(); - void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath); void loadConfig(Application* application); + void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath); + glm::vec3 reloadPlayerPosition(std::string filePath, Level* level); private: void errorCheck(tinyxml2::XMLError error); }; diff --git a/terrain.cc b/terrain.cc index b326ec2..acb0a11 100644 --- a/terrain.cc +++ b/terrain.cc @@ -82,6 +82,7 @@ void Terrain::makeTriangleMesh(){ } ab->setDataElements(numVertices, abData); + delete abData; this->triangleMesh = std::make_shared(); this->triangleMesh->bind(); this->triangleMesh->setMode(GL_TRIANGLE_STRIP);