Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/world_entities/script_trigger.cc @ 8669

Last change on this file since 8669 was 8666, checked in by snellen, 19 years ago

removed lunartest2.lua, cleaned all testing code

File size: 6.6 KB
Line 
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 */
17ScriptTrigger::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 */
37ScriptTrigger::~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 */
47void 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 */
95void 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 */
113void 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
128void ScriptTrigger::tick(float timestep)
129{
130  if(actionFinished) return;
131
132  if(triggerLasts && scriptCalled)
133  {
134    executeAction(timestep);
135    return;
136  }
137 
138  if( !invert && this->distance(target) < radius)
139 {
140  if(!callOnce)
141   {
142    executeAction(timestep);
143    scriptCalled = true;
144   }
145  else if(callOnce && !scriptCalled)
146  {
147   executeAction(timestep);
148   scriptCalled = true;
149  }
150 
151 }
152 else if( invert && this->distance(target) > radius)
153 {
154   if(!callOnce)
155   {
156     executeAction(timestep);
157   }
158   else if(callOnce && !scriptCalled)
159   {
160     executeAction(timestep);
161     scriptCalled = true;
162   }
163   
164 }
165 //else
166   //printf("SCRIPTTRIGGER: target out of range\n");
167
168}
169
170
171void ScriptTrigger::executeAction(float timestep)
172{
173     if(scriptIsOk)
174     {
175       //testScriptingFramework();
176     if(!(script->selectFunction(this->functionName,returnCount)) )
177       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
178     
179     script->pushParam( timestep, this->functionName);
180     
181     if( !(script->executeFunction()) )
182       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
183     }
184     
185     actionFinished = script->getReturnedBool();
186}
187
188
189void ScriptTrigger::setScript(const std::string& file)
190{
191
192  ScriptManager* scriptManager = State::getScriptManager();
193  if (scriptManager != NULL)
194  {
195
196    script = scriptManager->getScriptByFile(file);
197    if(script != NULL)
198    {
199      scriptIsOk = true;
200    }
201  }
202}
203
204
205 void ScriptTrigger::testScriptingFramework()
206 {
207   std::string file("lunartest2.lua");
208   //get script
209   Script* script = State::getScriptManager()->getScriptByFile(file);
210   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
211
212      //execute a function
213   printf("----------- main -----------\n");
214   std::string main("main");
215   if( script->selectFunction(main,3))
216     printf("function %s selected\n",main.c_str());
217
218   script->pushParam(3.14159,main);
219   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
220   script->executeFunction();
221
222   int ret = script->getReturnedInt();
223   printf("main returned %i\n",ret);
224
225   if(script->getReturnedBool())
226     printf("main returned true\n");
227   else
228     printf("main returned false\n");
229
230   float retf = script->getReturnedFloat();
231   printf("main returned %f\n",retf);
232   
233
234   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
235      //execute a 2nd function
236   printf("----------- test -----------\n");
237   std::string test("test");
238   if( script->selectFunction(test,0))
239     printf("function %s selected\n",test.c_str());
240
241   script->executeFunction();
242
243
244      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
245   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
246
247 }
Note: See TracBrowser for help on using the repository browser.