Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/script_trigger.cc @ 8650

Last change on this file since 8650 was 8408, checked in by bensch, 18 years ago

trunk: merged the script_engine branche back here
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/script_engine . -r8284:HEAD
no conflicts

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