1 | #include "script_trigger.h" |
---|
2 | #include "class_list.h" |
---|
3 | #include "script.h" |
---|
4 | |
---|
5 | |
---|
6 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
7 | { |
---|
8 | scriptCalled = false; |
---|
9 | callScript = false; |
---|
10 | triggered = false; |
---|
11 | loadParams(root); |
---|
12 | |
---|
13 | |
---|
14 | } |
---|
15 | |
---|
16 | ScriptTrigger::~ScriptTrigger() |
---|
17 | { |
---|
18 | |
---|
19 | } |
---|
20 | |
---|
21 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
22 | { |
---|
23 | if(root != NULL) |
---|
24 | { |
---|
25 | WorldEntity ::loadParams(root); |
---|
26 | |
---|
27 | { LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
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(""); |
---|
33 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
34 | .describe("where this script trigger should be located") |
---|
35 | .defaultValues(""); |
---|
36 | LoadParam(root, "radius", this, ScriptTrigger, setRadius) |
---|
37 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
38 | .defaultValues(0); |
---|
39 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
40 | .describe("the delay after which the funtion sould be triggered") |
---|
41 | .defaultValues(0); |
---|
42 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
43 | .describe("The name of the target as it is in the *.oxw file") |
---|
44 | .defaultValues(""); |
---|
45 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
46 | .describe("The name of the parent as it is in the *.oxw file") |
---|
47 | .defaultValues(""); |
---|
48 | |
---|
49 | } |
---|
50 | timer = delay; |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
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 | { |
---|
66 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName()); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
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)); |
---|
77 | this->setParentMode(PNODE_MOVEMENT); |
---|
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 | |
---|
85 | void ScriptTrigger::tick(float timestep) |
---|
86 | { |
---|
87 | |
---|
88 | if((this->getAbsDirV()-target->getAbsDirV()).len() < radius) |
---|
89 | { |
---|
90 | if(!callOnce) |
---|
91 | { |
---|
92 | executeAction(); |
---|
93 | } |
---|
94 | else if(callOnce && !scriptCalled) |
---|
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); |
---|
109 | if(script != NULL) |
---|
110 | { |
---|
111 | if(!(script->selectFunction(this->functionName,0)) ) |
---|
112 | printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), scriptFile.c_str()); |
---|
113 | if( !(script->executeFunction()) ) |
---|
114 | printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(),scriptFile.c_str()); |
---|
115 | } |
---|
116 | } |
---|