#include "level.hh" #include "loader.hh" #include Level::Level(std::string heightmapFilePath, std::string xmlFilePath){ this->terrain = Terrain(heightmapFilePath); // default value skydomeSize = 50.0f; physics = Physics(); this->xmlFilePath = xmlFilePath; } Level::Level() { } Level::~Level() { if (luaState!=nullptr) { lua_close(luaState); } for(unsigned int i = 0; i("Level") .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); lua_setglobal(luaState, "level"); this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); } void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { for(unsigned int i = 0; irender(shader, lightingPass, viewProjectionMatrix, shadowVPs); } } } void Level::update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed,bool kPressed, bool lPressed) { // Ignore first two mouse updates, because they are incorrect // DON'T try to move this functionallity elsewhere static int i = 0; if (i <2) { i++; } int runs = 2; 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.setPosition(physics.getCameraPosition()); camera.setDirection(physics.getCameraToPlayer()); } if(wPressed){ physics.rollForward(camera.getVector(),strength/runs); } if(aPressed) { physics.rollLeft(camera.getVector(),strength/runs); } if(sPressed) { physics.rollBack(camera.getVector(),strength/runs); } if(dPressed){ physics.rollRight(camera.getVector(),strength/runs); } } if(kPressed) camera.setIsPhysicsCamera(true); if(lPressed) camera.setIsPhysicsCamera(false); cameraCenter->setPosition(physics.getPos(0)); cameraCenter->setRotation(physics.getRotation(0)); for(unsigned i = 0; i < physicsObjects.size();i++) { physicsObjects[i]->setPosition(physics.getPos(i)); physicsObjects[i]->setRotation(physics.getRotation(i)); } skydome->setPosition(glm::vec3(cameraCenter->getPosition().x, 0.0f, cameraCenter->getPosition().z)); if (runTime > 2.0f) { for(unsigned int i = 0; i* Level::getLights() { return &lights; } Camera* Level::getCamera() { return &camera; } Object* Level::getCameraCenter() { return cameraCenter; } Light* Level::getDirectionalLight() { return &directionalLight; } glm::vec4 Level::getFogColour() { return fogColour; } glm::vec3 Level::getCameraPosition() { return cameraCenter->getPosition() + camera.getVector(); } void Level::setSkydomeSize(float size) { skydomeSize = size; } float Level::getSkydomeSize() { return this->skydomeSize; } std::vector* Level::getObjects() { return &objects; } std::vector* Level::getPhysicsObjects() { return &physicsObjects; } void Level::moveObject(int objectIndex, float strength, float xPos, float yPos, float zPos){ glm::vec3 position = glm::vec3(xPos, yPos, zPos); physics.removePositionConstraint(objectIndex); physics.addPositionConstraint(objectIndex, strength, position); } //should not be used since objects does not get synchronized and deletion is not implemented in pyhsics void Level::deleteObject(int objectIndex){ physicsObjects.erase(physicsObjects.begin() + objectIndex); for(unsigned int i = 0; istrength = strength; } void Level::setSkydomeObject(Object* object){ this->skydome = object; } void Level::addObject(Object* object) { objects.push_back(object); } void Level::addPhysicsObject(Object* object) { physicsObjects.push_back(object); } void Level::setAmbientLight(glm::vec3 colour) { this->ambientLight = colour; } void Level::setFogColour(glm::vec4 colour) { this->fogColour = colour; } void Level::setDirectionalLight(Light light) { this->directionalLight = light; } Physics* Level::getPhysics() { return &physics; } unsigned int Level::getObjectsVectorSize() { return objects.size(); } unsigned int Level::getPhysicsObjectsVectorSize() { return physicsObjects.size(); } void Level::setCameraCenter(Object* object) { this->cameraCenter = object; } void Level::addLight(Light light) { this->lights.push_back(light); } void Level::addTrigger(Trigger trigger) { this->triggers.push_back(trigger); } lua_State* Level::getLuaState() { return luaState; } Terrain* Level::getTerrain() { return &terrain; }