From 76b8b3bc034b52a7e5b891eddcfa33bf552d5103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Tue, 13 Jan 2015 13:52:22 +0100 Subject: [PATCH] Moved triggering to lua, got a strange bug with the const char* holding the name of the lua script. --- Levels/ObjectSetups/Level1.xml | 30 +++++++++--- Levels/scripts/Level1/deleteRandomObject.lua | 10 ---- Levels/scripts/deleteRandomObject.lua | 11 +++++ converter/converter.cc | 15 ++++-- level.cc | 50 +++++++++++++++++--- level.hh | 2 + trigger.cc | 46 ++++++++---------- trigger.hh | 5 +- 8 files changed, 114 insertions(+), 55 deletions(-) delete mode 100644 Levels/scripts/Level1/deleteRandomObject.lua create mode 100644 Levels/scripts/deleteRandomObject.lua diff --git a/Levels/ObjectSetups/Level1.xml b/Levels/ObjectSetups/Level1.xml index 3a9b57f..5626fcc 100644 --- a/Levels/ObjectSetups/Level1.xml +++ b/Levels/ObjectSetups/Level1.xml @@ -20,7 +20,10 @@ 3.0 false 0 - 0 + deleteRandomObject + 1 + 5 + 0 deleteRandomObjectAtTorch @@ -32,7 +35,10 @@ 2.0 false 0 - 0 + deleteRandomObject + 1 + 5 + 0 @@ -58,7 +64,10 @@ 1.0 false 0 - - + - + - + - + - @@ -84,7 +93,10 @@ 1.0 false 0 - - + - + - + - + - @@ -110,7 +122,10 @@ 1.0 false 0 - - + - + - + - + - @@ -136,7 +151,10 @@ 1.0 false 0 - - + - + - + - + - diff --git a/Levels/scripts/Level1/deleteRandomObject.lua b/Levels/scripts/Level1/deleteRandomObject.lua deleted file mode 100644 index b24c44d..0000000 --- a/Levels/scripts/Level1/deleteRandomObject.lua +++ /dev/null @@ -1,10 +0,0 @@ -function trigger() - print("Hello from Lua!") - if(not Level) then - print("No Level found!") - return - end - --a = table.getn(Level.getObjects()) - --rand = math.random(0, table.getn(Level->getObjects()) - 1) - --Level->getObjects()->erase(level->getObjects()->begin() + rand) -end diff --git a/Levels/scripts/deleteRandomObject.lua b/Levels/scripts/deleteRandomObject.lua new file mode 100644 index 0000000..628eff0 --- /dev/null +++ b/Levels/scripts/deleteRandomObject.lua @@ -0,0 +1,11 @@ +function trigger(objectToChange) + print("Hello from Lua!") + --if(not this_level) then + print("No level found!") + return + --end + a = this_level:getObjectCount() + rand = math.random(0, a - 1) + this_level:deleteObject(rand) + print("Triggered from Lua!") +end diff --git a/converter/converter.cc b/converter/converter.cc index 034d367..b296fcd 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -179,7 +179,10 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* distance = doc->NewElement("distance"); XMLElement* isBiggerThan = doc->NewElement("isBiggerThan"); XMLElement* objectNum = doc->NewElement("objectNum"); - XMLElement* functionPointer = doc->NewElement("functionPointer"); + XMLElement* luaScript = doc->NewElement("luaScript"); + XMLElement* toChangeIdGreen = doc->NewElement("toChangeIdGreen"); + XMLElement* toChangeIdBlue = doc->NewElement("toChangeIdBlue"); + XMLElement* toChangeObjNum = doc->NewElement("toChangeObjNum"); name->SetText("-"); xPosition->SetText("0"); @@ -190,7 +193,10 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ distance->SetText("1.0"); isBiggerThan->SetText("false"); objectNum->SetText("0"); - functionPointer->SetText("-"); + luaScript->SetText("-"); + toChangeIdGreen->SetText("-"); + toChangeIdBlue->SetText("-"); + toChangeObjNum->SetText("-"); trigger->InsertEndChild(name); trigger->InsertEndChild(xPosition); @@ -201,7 +207,10 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ trigger->InsertEndChild(distance); trigger->InsertEndChild(isBiggerThan); trigger->InsertEndChild(objectNum); - trigger->InsertEndChild(functionPointer); + trigger->InsertEndChild(luaScript); + trigger->InsertEndChild(toChangeIdGreen); + trigger->InsertEndChild(toChangeIdBlue); + trigger->InsertEndChild(toChangeObjNum); std::vector ret = nextID; nextID[1] += 1; diff --git a/level.cc b/level.cc index 1354d9e..115aa58 100644 --- a/level.cc +++ b/level.cc @@ -60,11 +60,12 @@ void Level::load() { //Expose the class Level and its functions luabridge::getGlobalNamespace(L) .beginClass("Level") - .addFunction("getObjects", &Level::getObjects) + .addFunction("deleteObject", &Level::deleteObject) + .addFunction("getObjectCount", &Level::getObjectCount) .endClass(); //Push the level as a global variable luabridge::push(L, this); - lua_setglobal(L, "Level"); + lua_setglobal(L, "this_level"); this->physics = Physics(); @@ -316,7 +317,7 @@ void Level::load() { if (name.compare("-") != 0){ float xPos, yPos, zPos, distance; bool isBigger; - int idGreen, idBlue, objectNum, functionPointer_int; + int idGreen, idBlue, objectNum; errorCheck(xmlTrigger->FirstChildElement("xPosition")->QueryFloatText(&xPos)); errorCheck(xmlTrigger->FirstChildElement("yPosition")->QueryFloatText(&yPos)); @@ -358,13 +359,32 @@ void Level::load() { object = objects[objectIdentifiers[i][0]]; } } - errorCheck(xmlTrigger->FirstChildElement("functionPointer")->QueryIntText(&functionPointer_int)); + const char* charLuaScript = xmlTrigger->FirstChildElement("luaScript")->GetText(); + if(charLuaScript == NULL){ + printf("XMLError: No Lua script found for a trigger.\n"); + } + std::string luaScript = charLuaScript; + + int toChangeIdGreen, toChangeIdBlue, toChangeObjNum, objectToChange=0; + errorCheck(xmlTrigger->FirstChildElement("toChangeIdGreen")->QueryIntText(&toChangeIdGreen)); + errorCheck(xmlTrigger->FirstChildElement("toChangeIdBlue")->QueryIntText(&toChangeIdBlue)); + errorCheck(xmlTrigger->FirstChildElement("toChangeObjNum")->QueryIntText(&toChangeObjNum)); + for (unsigned int i = 0; i* Level::getObjects() { return &objects; } + +void Level::deleteObject(int objectIndex){ + objects.erase(objects.begin() + objectIndex); + printf("Deleting an object.\n"); +} + +int Level::getObjectCount(){ + return objects.size(); +} + + + + + + + diff --git a/level.hh b/level.hh index 00c0cca..ea48d0e 100644 --- a/level.hh +++ b/level.hh @@ -30,6 +30,8 @@ class Level { glm::vec4 getFogColour(); void setSkydomeSize(float size); std::vector* getObjects(); + void deleteObject(int objectIndex); + int getObjectCount(); private: void errorCheck(tinyxml2::XMLError error); std::string levelNum; diff --git a/trigger.cc b/trigger.cc index 8450b40..3ddeaf8 100644 --- a/trigger.cc +++ b/trigger.cc @@ -1,36 +1,22 @@ #include "trigger.hh" #include "level.hh" -Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, int functionPointer_int, lua_State* L, Level* level) { +Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, Level* level) { this->position=position; this->distance=distance; this->isBigger=isBigger; this->object=object; - void (*functionPointer)(Level*) = NULL; - switch(functionPointer_int) { - case 0: - this->functionPointer = &Trigger::trigger_function_0; - break; - case 1: - this->functionPointer = &Trigger::trigger_function_1; - break; - case 2: - this->functionPointer = &Trigger::trigger_function_2; - break; - case 3: - this->functionPointer = &Trigger::trigger_function_3; - break; - case 4: - this->functionPointer = &Trigger::trigger_function_4; - break; - default: - printf("Trigger function could not be found.\n"); - } + this->triggerLuaScript=("../Levels/scripts/" + luaScript + ".lua").c_str(); + printf("Hallo\n"); + printf(this->triggerLuaScript); + printf("Hallo\n"); + printf("\n"); this->level = level; this->L = L; if(L == nullptr){ printf("L is NULL in trigger!\n"); } + this->objectToChange = objectToChange; triggered = false; } @@ -43,15 +29,21 @@ Trigger::~Trigger(){ void Trigger::triggerUpdate(){ if (!triggered){ if (isBigger && (glm::distance(object->getPosition(), position) > distance)) { - (this->*functionPointer)(level); - luaL_dofile(L, "../Levels/scripts/Level1/deleteRandomObject.lua"); - luabridge::getGlobal(L, "trigger")(); + printf(triggerLuaScript); + printf("\n"); + printf(triggerLuaScript); + printf("\nTriggering\n"); + //luaL_dofile(L, luaScript); + //luabridge::getGlobal(L, "trigger")(objectToChange); triggered = true; } else if (!isBigger && (glm::distance(object->getPosition(), position) < distance)) { - (this->*functionPointer)(level); - luaL_dofile(L, "../Levels/scripts/Level1/deleteRandomObject.lua"); - luabridge::getGlobal(L, "trigger")(); + printf(triggerLuaScript); + printf("\n"); + printf(triggerLuaScript); + printf("\nTriggering\n"); + //luaL_dofile(L, luaScript); + //luabridge::getGlobal(L, "trigger")(objectToChange); triggered = true; } } diff --git a/trigger.hh b/trigger.hh index 1dad2ec..aed4d64 100644 --- a/trigger.hh +++ b/trigger.hh @@ -14,7 +14,7 @@ class Level; class Trigger { public: - Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, int functionPointer_int, lua_State* L, Level* level); + Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, Level* level); Trigger(); ~Trigger(); void triggerUpdate(); @@ -23,10 +23,11 @@ class Trigger { float distance; bool isBigger; Object* object; - void (Trigger::*functionPointer)(Level*); + const char* triggerLuaScript; Level* level; bool triggered; lua_State* L; + int objectToChange; void trigger_function_0(Level* level); void trigger_function_1(Level* level); void trigger_function_2(Level* level);