diff --git a/converter/converter.cc b/converter/converter.cc index 78c5d3f..d50fe0a 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -264,6 +264,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* trigger = doc->NewElement("trigger"); newComposition->InsertEndChild(trigger); XMLElement* name = doc->NewElement("name"); + XMLElement* undo = doc->NewElement("undo"); XMLElement* xPosition = doc->NewElement("xPosition"); XMLElement* yPosition = doc->NewElement("yPosition"); XMLElement* zPosition = doc->NewElement("zPosition"); @@ -278,6 +279,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* toChangeObjNum = doc->NewElement("toChangeObjNum"); name->SetText("-"); + undo->SetText("false"); xPosition->SetText("0"); yPosition->SetText("0"); zPosition->SetText("0"); @@ -292,6 +294,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ toChangeObjNum->SetText("0"); trigger->InsertEndChild(name); + trigger->InsertEndChild(undo); trigger->InsertEndChild(xPosition); trigger->InsertEndChild(yPosition); trigger->InsertEndChild(zPosition); diff --git a/data/levels/Compositions.xml b/data/levels/Compositions.xml index 744df8e..4e66431 100644 --- a/data/levels/Compositions.xml +++ b/data/levels/Compositions.xml @@ -76,6 +76,8 @@ 0.3 4.0 -1.5 + 3.0 + 0.8 diff --git a/data/levels/Level1.xml b/data/levels/Level1.xml index 76335e2..2b937a8 100644 --- a/data/levels/Level1.xml +++ b/data/levels/Level1.xml @@ -11776,18 +11776,19 @@ 105 20 - - + resetPlayer + false 0 - 0 + -100000 0 - - - 1.0 + 100015 false 0 - - + resetPlayer.lua 0 - 0 + 105 0 diff --git a/data/levels/scripts/resetPlayer.lua b/data/levels/scripts/resetPlayer.lua new file mode 100644 index 0000000..4428ee6 --- /dev/null +++ b/data/levels/scripts/resetPlayer.lua @@ -0,0 +1,17 @@ +local global = require( "global" ) +if(global.triggeredResetPlayer == nil) then + global.triggeredResetPlayer = false +end +function trigger(objectToChange) + if(global.triggeredResetPlayer == false) then + if(not level) then + print("No level found in Lua!") + return + end + + level:resetPlayer() + + --global.triggeredResetPlayer = true + print("reset player") + end +end diff --git a/level.cc b/level.cc index 4c25179..90c3ba4 100644 --- a/level.cc +++ b/level.cc @@ -38,6 +38,7 @@ void Level::load() { .addFunction("deleteObject", &Level::deleteObject) .addFunction("getObjectCount", &Level::getPhysicsObjectsVectorSize) .addFunction("moveObject", &Level::moveObject) + .addFunction("resetPlayer", &Level::resetPlayer) .endClass(); //Push the level to Lua as a global variable luabridge::push(luaState, this); @@ -63,23 +64,28 @@ void Level::update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseD // Ignore first two mouse updates, because they are incorrect // DON'T try to move this functionallity elsewhere static int i = 0; - if (i <2) { + if (i <20) { i++; + mouseDelta.x=mouseDelta.y=0; } int runs = 2; + + if(i>=20) + { + mouseDelta.x = -mouseDelta.x; + } + for(int j = runs; j > 0; j--) { physics.takeUpdateStep(runTimeSinceLastUpdate/runs); - if(i>=2) - { - mouseDelta.x = -mouseDelta.x; - camera.updateRotation(mouseDelta/100.0f/(float)runs); - physics.updateCameraPos(mouseDelta, 50/runs, camera.getDistance()); + + camera.updateRotation(mouseDelta/100.0f/(float)runs); + physics.updateCameraPos(mouseDelta, 50/runs, camera.getDistance()); - camera.setPosition(physics.getCameraPosition()); - camera.setDirection(physics.getCameraToPlayer()); - } + camera.setPosition(physics.getCameraPosition()); + camera.setDirection(physics.getCameraToPlayer()); + if(wPressed){ physics.rollForward(camera.getVector(),strength/runs); } @@ -182,7 +188,12 @@ void Level::deleteObject(int objectIndex){ void Level::resetPlayer(){ Loader loader = Loader(); glm::vec3 newPosition = loader.reloadPlayerPosition(xmlFilePath, this); - //TODO cameraCenter.setPosition(newPosition); + physics.forceMove(newPosition, playerIndex); + physics.forceMoveCamera(newPosition + glm::vec3(1,0,0)); +} + +void Level::setPlayerIndex(int index){ + playerIndex = index; } void Level::setStrength(float strength) { diff --git a/level.hh b/level.hh index 69bce6c..6e60bbf 100644 --- a/level.hh +++ b/level.hh @@ -57,6 +57,7 @@ class Level { lua_State* getLuaState(); Terrain* getTerrain(); void resetPlayer(); + void setPlayerIndex(int index); private: lua_State* luaState=nullptr; std::vector objects; @@ -67,6 +68,7 @@ class Level { glm::vec4 fogColour; Light directionalLight; Object* cameraCenter; + int playerIndex; Object* skydome; Physics physics; Camera camera; diff --git a/light.cc b/light.cc index fa8dfde..e523ba5 100644 --- a/light.cc +++ b/light.cc @@ -1,10 +1,12 @@ #include "light.hh" -Light::Light(glm::vec3 position, glm::vec3 colour, float intensity, float flameYOffset) +Light::Light(glm::vec3 position, glm::vec3 colour, float intensity, float flameYOffset, float flameHeight, float flameWidth) : Entity(position, glm::vec3(0.0f, 0.0f, 0.0f)) { this->colour = colour; this->intensity = intensity; this->flameYOffset = flameYOffset; + this->flameHeight = flameHeight; + this->flameWidth = flameWidth; } Light::Light() { diff --git a/light.hh b/light.hh index 2112487..0ad997c 100644 --- a/light.hh +++ b/light.hh @@ -6,7 +6,7 @@ class Light : public Entity { public: - Light(glm::vec3 position, glm::vec3 colour, float intensity, float flameYOffset = 0.0f); + Light(glm::vec3 position, glm::vec3 colour, float intensity, float flameYOffset = 0.0f, float flameHeight = 0.0f, float flameWidth = 0.0f); Light(); glm::vec3 getColour(); float getIntensity(); @@ -14,6 +14,8 @@ class Light : public Entity { ~Light(); private: float flameYOffset; + float flameHeight; + float flameWidth; float intensity; glm::vec3 colour; }; diff --git a/loader.cc b/loader.cc index b5f03f3..cbba55a 100644 --- a/loader.cc +++ b/loader.cc @@ -258,6 +258,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa if(compositionType == 20){ level->setCameraCenter(object); + level->setPlayerIndex(objectIdentifier[1]); } }//objectData found }//finding the objectData @@ -298,7 +299,9 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa if (flameOffset != NULL){ float offset = 0; errorCheck(flameOffset->QueryFloatText(&offset)); - Light light = Light(lightPosition, lightColour, lightIntensity, offset); + float flameHeight = queryBool(xmlLight, "flameHeight"); + float flameWidth = queryBool(xmlLight, "flameWidth"); + Light light = Light(lightPosition, lightColour, lightIntensity, offset, flameHeight, flameWidth); level->addLight(light); } else { diff --git a/physics.cc b/physics.cc index 94653fe..ed6c952 100644 --- a/physics.cc +++ b/physics.cc @@ -414,9 +414,29 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f throw std::invalid_argument( "Bodies out of Sync" ); } +void Physics::prepareCollisionDetection() +{ + playerTerrainCol = playerObjectColision = false; + int numManifods = world->getDispatcher()->getNumManifolds(); + + for (int i=0;icameraDistance = distance; //note: in mouseMovement x and y are flipped in contrast to bullet btVector3 dodo = btVector3(0,1,0).cross(btVector3(currentDirection.x(),0,currentDirection.z())); - currentDirection = currentDirection.rotate(dodo,-mouseMovement.x / 100);//mathhelper 3.14159265359 + currentDirection = currentDirection.rotate(dodo,-mouseMovement.x / 50);//mathhelper 3.14159265359 btVector3 compare = currentDirection; compare.setY(0); @@ -496,7 +516,7 @@ void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength, float dis currentDirection = compare; } - currentDirection = currentDirection.rotate(btVector3(0,1,0),mouseMovement.y/100); + currentDirection = currentDirection.rotate(btVector3(0,1,0),-mouseMovement.y/100); currentDirection.normalize(); } @@ -557,6 +577,14 @@ void Physics::addStaticGroundPlane() void Physics::forceMove(glm::vec3 newPosition, unsigned indice)//ugly, but needed for reset { bodies[indice]->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z))); + bodies[indice]->setLinearVelocity(btVector3(0,0,0)); +} + +void Physics::forcePlayer(glm::vec3 newPosition)//ugly, but needed for reset +{ + playerBall->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z))); + playerBall->setLinearVelocity(btVector3(0,0,0)); + forceMoveCamera(newPosition + glm::vec3(currentDirection.x()*cameraDistance,currentDirection.y()*cameraDistance,currentDirection.z()*cameraDistance)); } void Physics::forceMoveCamera(glm::vec3 newPosition) diff --git a/physics.hh b/physics.hh index d8f287e..de2be9c 100644 --- a/physics.hh +++ b/physics.hh @@ -74,6 +74,10 @@ class Physics { void forceMove(glm::vec3 newPosition, unsigned indice); void forceMoveCamera(glm::vec3 newPosition); void addConvexBody(Entity entity, std::string path, float mass, float dampningL, float dampningA, unsigned indice, float scaling, bool rotate); + void prepareCollisionDetection(); + bool playerWithGround(); + bool playerWithObject(); + void forcePlayer(glm::vec3 newPosition); struct positionConstraint{btRigidBody* body; float strength; btVector3 position;}; @@ -98,6 +102,8 @@ class Physics { int counter = 0; std::string geometryPath; float cameraDistance = 5; //distance of the camera to the player. + bool playerTerrainCol = false; + bool playerObjectColision = false; }; enum collisionTypes{