1 | //for testing |
---|
2 | #include "luaincl.h" |
---|
3 | |
---|
4 | #include "script_trigger.h" |
---|
5 | #include "class_list.h" |
---|
6 | #include "script.h" |
---|
7 | |
---|
8 | #include "state.h" |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | /** |
---|
13 | * Constructs a new ScriptTrigger. |
---|
14 | * @param root the xml element to load the parameters from. |
---|
15 | * |
---|
16 | */ |
---|
17 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
18 | { |
---|
19 | this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger"); |
---|
20 | this->toList(OM_COMMON); |
---|
21 | |
---|
22 | returnCount = 1; |
---|
23 | actionFinished = false; |
---|
24 | doDebugDraw = false; |
---|
25 | invert = false; |
---|
26 | scriptCalled = false; |
---|
27 | scriptIsOk = false; |
---|
28 | triggerLasts = false; |
---|
29 | loadParams(root); |
---|
30 | |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Deletes the ScriptTrigger. |
---|
35 | * |
---|
36 | */ |
---|
37 | ScriptTrigger::~ScriptTrigger() |
---|
38 | { |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Reads the values from the tml element and sets them. |
---|
44 | * @param root the xml element to load the parameters from. |
---|
45 | * |
---|
46 | */ |
---|
47 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
48 | { |
---|
49 | if(root != NULL) |
---|
50 | { |
---|
51 | WorldEntity ::loadParams(root); |
---|
52 | |
---|
53 | LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
54 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
55 | .defaultValues(""); |
---|
56 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
---|
57 | .describe("the function of the script, that should be triggered by this script trigger") |
---|
58 | .defaultValues(""); |
---|
59 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
60 | .describe("where this script trigger should be located") |
---|
61 | .defaultValues(""); |
---|
62 | LoadParam(root, "radius", this, ScriptTrigger, setRadius) |
---|
63 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
64 | .defaultValues(0); |
---|
65 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
66 | .describe("the delay after which the funtion sould be triggered") |
---|
67 | .defaultValues(0); |
---|
68 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
69 | .describe("The name of the target as it is in the *.oxw file") |
---|
70 | .defaultValues(""); |
---|
71 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
72 | .describe("The name of the parent as it is in the *.oxw file") |
---|
73 | .defaultValues(""); |
---|
74 | LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce) |
---|
75 | .describe("True if the script shoul only be called once") |
---|
76 | .defaultValues(""); |
---|
77 | LoadParam(root, "invert", this, ScriptTrigger, setInvert) |
---|
78 | .describe("") |
---|
79 | .defaultValues(""); |
---|
80 | LoadParam(root, "triggerlasts", this, ScriptTrigger, setTriggerLasts) |
---|
81 | .describe("") |
---|
82 | .defaultValues(""); |
---|
83 | LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) |
---|
84 | .describe("True if the script should only be called once") |
---|
85 | .defaultValues(""); |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | /** |
---|
92 | * Sets the target(a world entity) of the ScriptTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered. |
---|
93 | * @param target The worldentity that the script supervises. |
---|
94 | */ |
---|
95 | void ScriptTrigger::setTarget(const std::string& target) |
---|
96 | { |
---|
97 | BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY); |
---|
98 | |
---|
99 | if (targetEntity != NULL) |
---|
100 | { |
---|
101 | this->setTarget(dynamic_cast<WorldEntity*>(targetEntity)); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName()); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Sets the parent of the trigger. |
---|
111 | * @param parent The parrent. |
---|
112 | */ |
---|
113 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
114 | { |
---|
115 | BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY); |
---|
116 | |
---|
117 | if (parentEntity != NULL) |
---|
118 | { |
---|
119 | this->setParent(dynamic_cast<WorldEntity*>(parentEntity)); |
---|
120 | this->setParentMode(PNODE_MOVEMENT); |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName()); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | void ScriptTrigger::tick(float timestep) |
---|
129 | { |
---|
130 | if(actionFinished) return; |
---|
131 | |
---|
132 | if(triggerLasts && scriptCalled) |
---|
133 | { |
---|
134 | executeAction(timestep); |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | if( !invert && this->distance(target) < radius) |
---|
140 | { |
---|
141 | if(!callOnce) |
---|
142 | { |
---|
143 | executeAction(timestep); |
---|
144 | scriptCalled = true; |
---|
145 | } |
---|
146 | else if(callOnce && !scriptCalled) |
---|
147 | { |
---|
148 | executeAction(timestep); |
---|
149 | scriptCalled = true; |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|
153 | else if( invert && this->distance(target) > radius) |
---|
154 | { |
---|
155 | if(!callOnce) |
---|
156 | { |
---|
157 | executeAction(timestep); |
---|
158 | } |
---|
159 | else if(callOnce && !scriptCalled) |
---|
160 | { |
---|
161 | executeAction(timestep); |
---|
162 | scriptCalled = true; |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|
166 | //else |
---|
167 | //printf("SCRIPTTRIGGER: target out of range\n"); |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | void ScriptTrigger::executeAction(float timestep) |
---|
173 | { |
---|
174 | if(scriptIsOk) |
---|
175 | { |
---|
176 | //testScriptingFramework(); |
---|
177 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
---|
178 | printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
179 | |
---|
180 | script->pushParam( timestep, this->functionName); |
---|
181 | |
---|
182 | if( !(script->executeFunction()) ) |
---|
183 | printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
184 | } |
---|
185 | |
---|
186 | actionFinished = script->getReturnedBool(); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void ScriptTrigger::setScript(const std::string& file) |
---|
191 | { |
---|
192 | |
---|
193 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
194 | if (scriptManager != NULL) |
---|
195 | { |
---|
196 | |
---|
197 | script = scriptManager->getScriptByFile(file); |
---|
198 | if(script != NULL) |
---|
199 | { |
---|
200 | scriptIsOk = true; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | void ScriptTrigger::testScriptingFramework() |
---|
207 | { |
---|
208 | std::string file("lunartest2.lua"); |
---|
209 | //get script |
---|
210 | Script* script = State::getScriptManager()->getScriptByFile(file); |
---|
211 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
212 | |
---|
213 | //execute a function |
---|
214 | printf("----------- main -----------\n"); |
---|
215 | std::string main("main"); |
---|
216 | if( script->selectFunction(main,3)) |
---|
217 | printf("function %s selected\n",main.c_str()); |
---|
218 | |
---|
219 | script->pushParam(3.14159,main); |
---|
220 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
221 | script->executeFunction(); |
---|
222 | |
---|
223 | int ret = script->getReturnedInt(); |
---|
224 | printf("main returned %i\n",ret); |
---|
225 | |
---|
226 | if(script->getReturnedBool()) |
---|
227 | printf("main returned true\n"); |
---|
228 | else |
---|
229 | printf("main returned false\n"); |
---|
230 | |
---|
231 | float retf = script->getReturnedFloat(); |
---|
232 | printf("main returned %f\n",retf); |
---|
233 | |
---|
234 | |
---|
235 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
236 | //execute a 2nd function |
---|
237 | printf("----------- test -----------\n"); |
---|
238 | std::string test("test"); |
---|
239 | if( script->selectFunction(test,0)) |
---|
240 | printf("function %s selected\n",test.c_str()); |
---|
241 | |
---|
242 | script->executeFunction(); |
---|
243 | |
---|
244 | |
---|
245 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
---|
246 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
247 | |
---|
248 | } |
---|