[8171] | 1 | #include "script_trigger.h" |
---|
[8202] | 2 | #include "class_list.h" |
---|
| 3 | #include "script.h" |
---|
[8171] | 4 | |
---|
| 5 | |
---|
[8202] | 6 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
[8171] | 7 | { |
---|
[8202] | 8 | scriptCalled = false; |
---|
| 9 | callScript = false; |
---|
| 10 | triggered = false; |
---|
| 11 | loadParams(root); |
---|
| 12 | |
---|
| 13 | |
---|
[8171] | 14 | } |
---|
| 15 | |
---|
| 16 | ScriptTrigger::~ScriptTrigger() |
---|
| 17 | { |
---|
| 18 | |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
| 22 | { |
---|
[8207] | 23 | if(root != NULL) |
---|
| 24 | { |
---|
[8202] | 25 | WorldEntity ::loadParams(root); |
---|
[8199] | 26 | |
---|
[8205] | 27 | { LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
[8171] | 28 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
| 29 | .defaultValues(""); |
---|
| 30 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
---|
| 31 | .describe("the function of the script, that should be triggered by this script trigger") |
---|
| 32 | .defaultValues(""); |
---|
[8205] | 33 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
[8178] | 34 | .describe("where this script trigger should be located") |
---|
[8171] | 35 | .defaultValues(""); |
---|
| 36 | LoadParam(root, "radius", this, ScriptTrigger, setRadius) |
---|
| 37 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
[8172] | 38 | .defaultValues(0); |
---|
| 39 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
| 40 | .describe("the delay after which the funtion sould be triggered") |
---|
| 41 | .defaultValues(0); |
---|
[8205] | 42 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
[8178] | 43 | .describe("The name of the target as it is in the *.oxw file") |
---|
| 44 | .defaultValues(""); |
---|
[8205] | 45 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
[8208] | 46 | .describe("The name of the parent as it is in the *.oxw file") |
---|
[8178] | 47 | .defaultValues(""); |
---|
| 48 | |
---|
[8205] | 49 | } |
---|
[8207] | 50 | timer = delay; |
---|
| 51 | } |
---|
| 52 | |
---|
[8171] | 53 | } |
---|
| 54 | |
---|
| 55 | |
---|
[8199] | 56 | void ScriptTrigger::setTarget(const std::string& target) |
---|
| 57 | { |
---|
| 58 | BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY); |
---|
| 59 | |
---|
| 60 | if (targetEntity != NULL) |
---|
| 61 | { |
---|
| 62 | this->setTarget(dynamic_cast<WorldEntity*>(targetEntity)); |
---|
| 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
[8202] | 66 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName()); |
---|
[8199] | 67 | } |
---|
| 68 | } |
---|
[8171] | 69 | |
---|
[8205] | 70 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
| 71 | { |
---|
| 72 | BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY); |
---|
| 73 | |
---|
| 74 | if (parentEntity != NULL) |
---|
| 75 | { |
---|
| 76 | this->setParent(dynamic_cast<WorldEntity*>(parentEntity)); |
---|
[8207] | 77 | this->setParentMode(PNODE_MOVEMENT); |
---|
[8205] | 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName()); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
[8171] | 85 | void ScriptTrigger::tick(float timestep) |
---|
| 86 | { |
---|
| 87 | |
---|
| 88 | if((this->getAbsDirV()-target->getAbsDirV()).len() < radius) |
---|
| 89 | { |
---|
[8178] | 90 | if(!callOnce) |
---|
[8171] | 91 | { |
---|
| 92 | executeAction(); |
---|
| 93 | } |
---|
[8178] | 94 | else if(callOnce && !scriptCalled) |
---|
[8171] | 95 | { |
---|
| 96 | executeAction(); |
---|
| 97 | scriptCalled = true; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | void ScriptTrigger::executeAction() |
---|
| 106 | { |
---|
| 107 | ScriptManager* scriptManager = ScriptManager::getInstance(); |
---|
| 108 | Script* script = scriptManager->getScriptByFile(this->scriptFile); |
---|
[8206] | 109 | if(script != NULL) |
---|
[8207] | 110 | { |
---|
[8171] | 111 | if(!(script->selectFunction(this->functionName,0)) ) |
---|
[8178] | 112 | printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), scriptFile.c_str()); |
---|
[8171] | 113 | if( !(script->executeFunction()) ) |
---|
[8178] | 114 | printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(),scriptFile.c_str()); |
---|
[8207] | 115 | } |
---|
[8171] | 116 | } |
---|