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