2014-12-15 12:36:04 +00:00
|
|
|
#include "trigger.hh"
|
2015-03-03 17:45:02 +00:00
|
|
|
#include <sys/stat.h>
|
2014-12-15 12:36:04 +00:00
|
|
|
|
2015-03-04 13:35:14 +00:00
|
|
|
Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* luaState, int objectToChange, std::string scriptPath, bool undo) {
|
2014-12-15 13:59:03 +00:00
|
|
|
this->position=position;
|
|
|
|
this->distance=distance;
|
|
|
|
this->isBigger=isBigger;
|
|
|
|
this->object=object;
|
2015-02-15 18:34:15 +00:00
|
|
|
this->luaScript= scriptPath + luaScript;
|
2015-03-03 17:45:02 +00:00
|
|
|
struct stat buf;
|
|
|
|
if(stat(this->luaScript.c_str(), &buf) != 0){
|
|
|
|
std::cout << "The lua file " << this->luaScript << " does not exist." << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
2015-02-15 18:37:13 +00:00
|
|
|
this->luaState = luaState;
|
|
|
|
if(luaState == nullptr){
|
|
|
|
printf("The Lua state is NULL in trigger!\n");
|
2015-01-09 15:51:28 +00:00
|
|
|
}
|
2015-01-13 12:52:22 +00:00
|
|
|
this->objectToChange = objectToChange;
|
2015-03-04 13:35:14 +00:00
|
|
|
this->undo = undo;
|
2014-12-15 12:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Trigger::Trigger(){
|
|
|
|
}
|
|
|
|
|
|
|
|
Trigger::~Trigger(){
|
|
|
|
}
|
2014-12-15 13:59:03 +00:00
|
|
|
|
|
|
|
void Trigger::triggerUpdate(){
|
2015-03-15 17:46:44 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
|
|
|
bool printDebug = true;
|
|
|
|
#else
|
|
|
|
bool printDebug = false;
|
|
|
|
#endif
|
2015-03-06 12:18:55 +00:00
|
|
|
if (isBigger && (glm::distance(object->getPosition(), position) > distance)) {
|
|
|
|
int error = luaL_dofile(luaState, luaScript.c_str());
|
|
|
|
if (error != 0) {
|
|
|
|
std::cout << "Couldn't load file: " << this->luaScript << std::endl;
|
|
|
|
exit(-1);
|
2015-01-09 15:51:28 +00:00
|
|
|
}
|
2015-03-06 12:18:55 +00:00
|
|
|
if (undo){
|
2015-03-15 17:46:44 +00:00
|
|
|
luabridge::getGlobal(luaState, "triggerUndo")(objectToChange, printDebug);
|
2015-03-06 12:18:55 +00:00
|
|
|
}else{
|
2015-03-15 17:46:44 +00:00
|
|
|
luabridge::getGlobal(luaState, "trigger")(objectToChange, printDebug);
|
2015-01-09 15:51:28 +00:00
|
|
|
}
|
2015-03-06 12:18:55 +00:00
|
|
|
}
|
|
|
|
else if (!isBigger && (glm::distance(object->getPosition(), position) < distance)) {
|
|
|
|
int error = luaL_dofile(luaState, luaScript.c_str());
|
|
|
|
if (error != 0) {
|
|
|
|
std::cout << "Couldn't load file: " << this->luaScript << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (undo){
|
2015-03-15 17:46:44 +00:00
|
|
|
luabridge::getGlobal(luaState, "triggerUndo")(objectToChange, printDebug);
|
2015-03-06 12:18:55 +00:00
|
|
|
}else{
|
2015-03-15 17:46:44 +00:00
|
|
|
luabridge::getGlobal(luaState, "trigger")(objectToChange, printDebug);
|
2015-03-06 12:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-16 11:40:30 +00:00
|
|
|
}
|
2015-01-13 17:18:13 +00:00
|
|
|
|
|
|
|
bool Trigger::deleteNotification(int deletedObjectIndex){
|
|
|
|
if (deletedObjectIndex < objectToChange){
|
|
|
|
objectToChange-=1;
|
|
|
|
}
|
|
|
|
if (deletedObjectIndex == objectToChange){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|