Moved triggering to lua, got a strange bug with the const char* holding the name of the lua script.

This commit is contained in:
Steffen Fündgens 2015-01-13 13:52:22 +01:00
parent e35899523f
commit 46f163d748
8 changed files with 114 additions and 55 deletions

View File

@ -20,7 +20,10 @@
<distance>3.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>0</functionPointer>
<luaScript>deleteRandomObject</luaScript>
<toChangeIdGreen>1</toChangeIdGreen>
<toChangeIdBlue>5</toChangeIdBlue>
<toChangeObjNum>0</toChangeObjNum>
</trigger>
<trigger>
<name>deleteRandomObjectAtTorch</name>
@ -32,7 +35,10 @@
<distance>2.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>0</functionPointer>
<luaScript>deleteRandomObject</luaScript>
<toChangeIdGreen>1</toChangeIdGreen>
<toChangeIdBlue>5</toChangeIdBlue>
<toChangeObjNum>0</toChangeObjNum>
</trigger>
</composition>
@ -58,7 +64,10 @@
<distance>1.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>-</functionPointer>
<luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger>
</composition>
@ -84,7 +93,10 @@
<distance>1.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>-</functionPointer>
<luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger>
</composition>
@ -110,7 +122,10 @@
<distance>1.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>-</functionPointer>
<luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger>
</composition>
@ -136,7 +151,10 @@
<distance>1.0</distance>
<isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum>
<functionPointer>-</functionPointer>
<luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger>
</composition>

View File

@ -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

View File

@ -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

View File

@ -179,7 +179,10 @@ std::vector<int> 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<int> 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<int> 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<int> ret = nextID;
nextID[1] += 1;

View File

@ -60,11 +60,12 @@ void Level::load() {
//Expose the class Level and its functions
luabridge::getGlobalNamespace(L)
.beginClass<Level>("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<objectIdentifiers.size(); i++){
if (objectIdentifiers[i][1]==toChangeIdGreen && objectIdentifiers[i][2]==toChangeIdBlue && objectIdentifiers[i][3]==toChangeObjNum){
objectToChange = objectIdentifiers[i][0];
}
}
if (object != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, functionPointer_int, L, this);
triggers.push_back(trigger);
if (objectToChange != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, L, objectToChange, this);
triggers.push_back(trigger);
}
else {
printf("Object to be changed by a trigger not found.\n");
}
}
else {
printf("Trigger object not found.\n");
printf("Triggering object not found.\n");
}
}
}
@ -461,3 +481,19 @@ void Level::setSkydomeSize(float size) {
std::vector<Object*>* 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();
}

View File

@ -30,6 +30,8 @@ class Level {
glm::vec4 getFogColour();
void setSkydomeSize(float size);
std::vector<Object*>* getObjects();
void deleteObject(int objectIndex);
int getObjectCount();
private:
void errorCheck(tinyxml2::XMLError error);
std::string levelNum;

View File

@ -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;
}
}

View File

@ -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);