Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added comments

File size: 5.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  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
99void ScriptTrigger::setTriggerParent(const std::string& parent)
100{
101  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
102
103  if (parentEntity != NULL)
104  {
105    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
106    this->setParentMode(PNODE_MOVEMENT);
107  }
108  else
109  {
110    PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName());
111  }
112}
113
114void ScriptTrigger::tick(float timestep)
115{
116
117  if( this->distance(target) < radius)
118 {
119  if(!callOnce)
120   {
121    executeAction();
122   }
123  else if(callOnce && !scriptCalled)
124  {
125   executeAction();
126   scriptCalled = true;
127  }
128 }
129 //else
130   //printf("SCRIPTTRIGGER: target out of range\n");
131
132}
133
134
135void ScriptTrigger::executeAction()
136{
137     if(scriptIsOk)
138     {
139       testScriptingFramework();
140     /*if(!(script->selectFunction(this->functionName,0)) )
141       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
142     if( !(script->executeFunction()) )
143       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/
144     }
145}
146
147
148void ScriptTrigger::setScript(const std::string& file)
149{
150
151  ScriptManager* scriptManager = State::getScriptManager();
152  if (scriptManager != NULL)
153  {
154
155    script = scriptManager->getScriptByFile(file);
156    if(script != NULL)
157    {
158      scriptIsOk = true;
159    }
160  }
161}
162
163
164 void ScriptTrigger::testScriptingFramework()
165 {
166   std::string file("lunartest2.lua");
167   //get script
168   Script* script = State::getScriptManager()->getScriptByFile(file);
169   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
170
171      //execute a function
172   printf("----------- main -----------\n");
173   std::string main("main");
174   if( script->selectFunction(main,3))
175     printf("function %s selected\n",main.c_str());
176
177   script->pushParam(3.14159,main);
178   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
179   script->executeFunction();
180
181   int ret = script->getReturnedInt();
182   printf("main returned %i\n",ret);
183
184   if(script->getReturnedBool())
185     printf("main returned true\n");
186   else
187     printf("main returned false\n");
188
189   float retf = script->getReturnedFloat();
190   printf("main returned %f\n",retf);
191   
192
193   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
194      //execute a 2nd function
195   printf("----------- test -----------\n");
196   std::string test("test");
197   if( script->selectFunction(test,0))
198     printf("function %s selected\n",test.c_str());
199
200   script->executeFunction();
201
202
203      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
204   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
205
206 }
Note: See TracBrowser for help on using the repository browser.