From 2a1119da79609c069d81b42c45ea6a00dd0fe557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Mon, 15 Dec 2014 18:43:10 +0100 Subject: [PATCH] Implemented simple triggers. --- level.cc | 79 +++++++++++++++++++++++++++++++++++++++++++----------- level.hh | 5 ++++ trigger.cc | 11 +++++--- trigger.hh | 4 +-- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/level.cc b/level.cc index 264ecf9..1935149 100644 --- a/level.cc +++ b/level.cc @@ -107,7 +107,7 @@ void Level::load() { directionalLight = Light(glm::vec3(xOffset,yOffset,zOffset), glm::vec3(rColour,gColour,bColour), intensity); //load Objects - std::vector objectIdentifiers; //The first entry is the index in objects, the others are idGreen, idBlue and objectNum. + std::vector> objectIdentifiers = std::vector>(); //The first entry is the index in objects, the others are idGreen, idBlue and objectNum. XMLDocument* compositions = new XMLDocument(); const char* compositionsFile = "../Levels/ObjectSetups/Compositions.xml"; compositions->LoadFile(compositionsFile); @@ -186,7 +186,7 @@ void Level::load() { Object* object = new Object(model, material, objectPosition, compRot); objects.push_back(object); //create an identifier for this object - int* objectIdentifier = new int[4]; + std::vector objectIdentifier = std::vector(4); objectIdentifier[0] = objects.size()-1; int idGreen, idBlue; errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); @@ -221,8 +221,7 @@ void Level::load() { errorCheck(objectData->FirstChildElement("dampningA")->QueryFloatText(&dampningA)); std::string bulletModelPath = modelPath.substr(0, modelPath.length()-3); bulletModelPath += "bullet"; - //this->physics.addRigidBodyFromFile(*object, mass, dampningL, dampningA, bulletModelPath, physicObjects.size()); - this->physics.addBox(1, 1, 1, *object, mass, physicObjects.size()); + this->physics.addTriangleMeshBody(*object, modelPath, mass, dampningL, dampningA, physicObjects.size()); } else{ printf("XMLError: Not a valid physicType.\n"); } @@ -282,35 +281,65 @@ void Level::load() { std::string name = charName; if (name.compare("-") != 0){ float xPos, yPos, zPos, distance; - std::vector position; + glm::vec3 position; bool isBigger; - int idGreen, idBlue, objectNum; + int idGreen, idBlue, objectNum, functionPointer_int; errorCheck(xmlTrigger->FirstChildElement("xPosition")->QueryFloatText(&xPos)); errorCheck(xmlTrigger->FirstChildElement("yPosition")->QueryFloatText(&yPos)); errorCheck(xmlTrigger->FirstChildElement("zPosition")->QueryFloatText(&zPos)); errorCheck(xmlTrigger->FirstChildElement("distance")->QueryFloatText(&distance)); - position.push_back(xPos); - position.push_back(yPos); - position.push_back(zPos); + position = glm::vec3(xPos, yPos, zPos); errorCheck(xmlTrigger->FirstChildElement("isBiggerThan")->QueryBoolText(&isBigger)); errorCheck(xmlTrigger->FirstChildElement("idGreen")->QueryIntText(&idGreen)); errorCheck(xmlTrigger->FirstChildElement("idBlue")->QueryIntText(&idBlue)); errorCheck(xmlTrigger->FirstChildElement("objectNum")->QueryIntText(&objectNum)); - //TODO find the object - //Trigger trigger = Trigger::Trigger(position, distance, isBigger, Object* object, void (*functionPointer)()); - //triggers.push_back(trigger); + Object* object=0; + for (unsigned int i = 0; iFirstChildElement("functionPointer")->QueryIntText(&functionPointer_int)); + void (*functionPointer)() = NULL; + switch(functionPointer_int) { + case 0: + functionPointer = &trigger_function_0; + break; + case 1: + functionPointer = &trigger_function_1; + break; + case 2: + functionPointer = &trigger_function_2; + break; + case 3: + functionPointer = &trigger_function_3; + break; + case 4: + functionPointer = &trigger_function_4; + break; + default: + printf("Trigger function could not be found."); + } + if (object != 0) { + Trigger trigger = Trigger(position, distance, isBigger, object, functionPointer); + triggers.push_back(trigger); + } + else { + printf("Trigger object not found.\n"); + } } } //add player (//TODO remove this as soon as we have a levelSetup with a player) - Model marbleModel = Model("marbleSmooth.obj", 0.75f); + /*Model marbleModel = Model("marbleSmooth.obj", 0.75f); Material marbleMaterial = Material("Marbletexture.png", 0.1f, 0.5f, 0.5f, 3.0f); - Object* object = new Object(marbleModel, marbleMaterial, glm::vec3(2.0f, 10.0f, 2.0f), + Object* object = new Object(marbleModel, marbleMaterial, glm::vec3(240.0f, 30.0f, -240.0f), glm::vec3(0.0f, 0.0f, 0.0f)); objects.push_back(object); physicObjects.push_back(object); this->physics.addPlayer(1.25f,*object,8.0f,physicObjects.size()); - cameraCenter = object; + cameraCenter = object;*/ } void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, @@ -363,6 +392,10 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre skydome->setPosition(glm::vec3(cameraCenter->getPosition().x, 0.0f, cameraCenter->getPosition().z)); + + for(unsigned int i = 0; i position, float distance, bool isBigger, Object* object, void (*functionPointer)()) { +Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, void (*functionPointer)()) { this->position=position; this->distance=distance; this->isBigger=isBigger; this->object=object; - this->functionPointer=functionPointer; + this->functionPointer = functionPointer; } Trigger::Trigger(){ @@ -15,5 +15,10 @@ Trigger::~Trigger(){ } void Trigger::triggerUpdate(){ - + if (isBigger && (glm::distance(object->getPosition(), position) > distance)) { + (*functionPointer)(); + } + else if (!isBigger && (glm::distance(object->getPosition(), position) < distance)) { + (*functionPointer)(); + } } diff --git a/trigger.hh b/trigger.hh index 04fbac1..d9c74fb 100644 --- a/trigger.hh +++ b/trigger.hh @@ -6,12 +6,12 @@ class Trigger { public: - Trigger(std::vector position, float distance, bool isBigger, Object* object, void (*functionPointer)()); + Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, void (*functionPointer)()); Trigger(); ~Trigger(); void triggerUpdate(); private: - std::vector position; + glm::vec3 position; float distance; bool isBigger; Object* object;