Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8538 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
RevLine 
[8246]1//for testing
2#include "luaincl.h"
[8408]3
[8171]4#include "script_trigger.h"
[8202]5#include "class_list.h"
6#include "script.h"
[8171]7
[8239]8#include "state.h"
[8408]9
10
11
12/**
13 * Constructs a new ScriptTrigger.
14 * @param root the xml element to load the parameters from.
15 *
16 */
[8202]17ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
[8171]18{
[8408]19  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
20  this->toList(OM_COMMON);
21
22  doDebugDraw = false;
[8202]23  scriptCalled = false;
[8211]24  scriptIsOk = false;
[8202]25  loadParams(root);
26
[8171]27}
28
[8408]29/**
30 * Deletes the ScriptTrigger.
31 *
32 */
[8171]33ScriptTrigger::~ScriptTrigger()
34{
35
36}
37
[8408]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 */
[8171]43void ScriptTrigger::loadParams(const TiXmlElement* root)
44{
[8207]45  if(root != NULL)
46  {
[8212]47    WorldEntity ::loadParams(root);
[8211]48
[8212]49    LoadParam(root, "file", this, ScriptTrigger, setScript)
[8171]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("");
[8205]55    LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
[8178]56        .describe("where this script trigger should be located")
[8171]57        .defaultValues("");
58    LoadParam(root, "radius", this, ScriptTrigger, setRadius)
59        .describe("the fileName of the script, that should be triggered by this script trigger")
[8172]60        .defaultValues(0);
61    LoadParam(root, "delay", this, ScriptTrigger, setDelay)
62        .describe("the delay after which the funtion sould be triggered")
63        .defaultValues(0);
[8205]64    LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
[8178]65        .describe("The name of the target as it is in the *.oxw file")
66        .defaultValues("");
[8205]67    LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
[8208]68        .describe("The name of the parent as it is in the *.oxw file")
[8178]69        .defaultValues("");
[8259]70    LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce)
71        .describe("True if the script shoul only be called once")
72        .defaultValues("");
[8408]73    LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
74        .describe("True if the script should only be called once")
75        .defaultValues("");
[8207]76  }
[8211]77
[8171]78}
79
80
[8408]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 */
[8199]85void ScriptTrigger::setTarget(const std::string& target)
86{
87  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
[8211]88
[8199]89  if (targetEntity != NULL)
90  {
91    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
92  }
93  else
94  {
[8202]95    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
[8199]96  }
97}
[8171]98
[8408]99/**
100 * Sets the parent of the trigger.
101 * @param parent The parrent.
102 */
[8205]103void ScriptTrigger::setTriggerParent(const std::string& parent)
104{
105  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
[8211]106
[8205]107  if (parentEntity != NULL)
108  {
109    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
[8207]110    this->setParentMode(PNODE_MOVEMENT);
[8205]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
[8171]118void ScriptTrigger::tick(float timestep)
119{
[8408]120
121  if( this->distance(target) < radius)
[8171]122 {
[8178]123  if(!callOnce)
[8171]124   {
125    executeAction();
126   }
[8178]127  else if(callOnce && !scriptCalled)
[8171]128  {
129   executeAction();
130   scriptCalled = true;
131  }
132 }
[8408]133 //else
134   //printf("SCRIPTTRIGGER: target out of range\n");
[8171]135
136}
137
138
139void ScriptTrigger::executeAction()
140{
[8211]141     if(scriptIsOk)
[8207]142     {
[8259]143       testScriptingFramework();
[8408]144     /*if(!(script->selectFunction(this->functionName,0)) )
[8212]145       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
[8171]146     if( !(script->executeFunction()) )
[8408]147       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/
[8207]148     }
[8171]149}
[8211]150
151
152void ScriptTrigger::setScript(const std::string& file)
153{
[8408]154
[8239]155  ScriptManager* scriptManager = State::getScriptManager();
156  if (scriptManager != NULL)
157  {
[8408]158
[8239]159    script = scriptManager->getScriptByFile(file);
160    if(script != NULL)
[8408]161    {
[8239]162      scriptIsOk = true;
[8408]163    }
[8239]164  }
[8214]165}
[8243]166
167
168 void ScriptTrigger::testScriptingFramework()
169 {
170   std::string file("lunartest2.lua");
[8408]171   //get script
[8246]172   Script* script = State::getScriptManager()->getScriptByFile(file);
[8243]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);
[8408]182   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]183   script->executeFunction();
184
[8408]185   int ret = script->getReturnedInt();
186   printf("main returned %i\n",ret);
[8243]187
188   if(script->getReturnedBool())
189     printf("main returned true\n");
190   else
191     printf("main returned false\n");
192
[8408]193   float retf = script->getReturnedFloat();
194   printf("main returned %f\n",retf);
195   
[8243]196
[8408]197   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]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.