Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8379 was 8379, checked in by snellen, 18 years ago

Obj is now correctly inserted into the script

File size: 5.9 KB
RevLine 
[8246]1//for testing
2#include "luaincl.h"
[8171]3#include "script_trigger.h"
[8202]4#include "class_list.h"
5#include "script.h"
[8171]6
[8239]7#include "state.h"
[8304]8
9
10
[8202]11ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
[8171]12{
[8285]13  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
14  this->toList(OM_COMMON);
[8304]15
[8379]16  doDebugDraw = false;
[8202]17  scriptCalled = false;
[8211]18  scriptIsOk = false;
[8202]19  loadParams(root);
20
[8171]21}
22
23ScriptTrigger::~ScriptTrigger()
24{
25
26}
27
28void ScriptTrigger::loadParams(const TiXmlElement* root)
29{
[8207]30  if(root != NULL)
31  {
[8212]32    WorldEntity ::loadParams(root);
[8211]33
[8212]34    LoadParam(root, "file", this, ScriptTrigger, setScript)
[8171]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("");
[8205]40    LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
[8178]41        .describe("where this script trigger should be located")
[8171]42        .defaultValues("");
43    LoadParam(root, "radius", this, ScriptTrigger, setRadius)
44        .describe("the fileName of the script, that should be triggered by this script trigger")
[8172]45        .defaultValues(0);
46    LoadParam(root, "delay", this, ScriptTrigger, setDelay)
47        .describe("the delay after which the funtion sould be triggered")
48        .defaultValues(0);
[8205]49    LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
[8178]50        .describe("The name of the target as it is in the *.oxw file")
51        .defaultValues("");
[8205]52    LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
[8208]53        .describe("The name of the parent as it is in the *.oxw file")
[8178]54        .defaultValues("");
[8259]55    LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce)
56        .describe("True if the script shoul only be called once")
57        .defaultValues("");
[8304]58    LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
[8379]59        .describe("True if the script should only be called once")
[8304]60        .defaultValues("");
[8207]61  }
[8211]62
[8171]63}
64
65
[8199]66void ScriptTrigger::setTarget(const std::string& target)
67{
68  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
[8211]69
[8199]70  if (targetEntity != NULL)
71  {
72    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
73  }
74  else
75  {
[8202]76    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
[8199]77  }
78}
[8171]79
[8205]80void ScriptTrigger::setTriggerParent(const std::string& parent)
81{
82  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
[8211]83
[8205]84  if (parentEntity != NULL)
85  {
86    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
[8207]87    this->setParentMode(PNODE_MOVEMENT);
[8205]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
[8171]95void ScriptTrigger::tick(float timestep)
96{
[8304]97
[8286]98  if( this->distance(target) < radius)
[8171]99 {
[8178]100  if(!callOnce)
[8171]101   {
102    executeAction();
103   }
[8178]104  else if(callOnce && !scriptCalled)
[8171]105  {
106   executeAction();
107   scriptCalled = true;
108  }
109 }
[8289]110 //else
111   //printf("SCRIPTTRIGGER: target out of range\n");
[8171]112
113}
114
115
116void ScriptTrigger::executeAction()
117{
[8211]118     if(scriptIsOk)
[8207]119     {
[8259]120       testScriptingFramework();
[8289]121     /*if(!(script->selectFunction(this->functionName,0)) )
[8212]122       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
[8171]123     if( !(script->executeFunction()) )
[8289]124       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/
[8207]125     }
[8171]126}
[8211]127
128
129void ScriptTrigger::setScript(const std::string& file)
130{
[8304]131
[8239]132  ScriptManager* scriptManager = State::getScriptManager();
133  if (scriptManager != NULL)
134  {
[8304]135
[8239]136    script = scriptManager->getScriptByFile(file);
137    if(script != NULL)
[8289]138    {
[8239]139      scriptIsOk = true;
[8289]140    }
[8239]141  }
[8214]142}
[8243]143
144
145 void ScriptTrigger::testScriptingFramework()
146 {
147   std::string file("lunartest2.lua");
148     // Script script;
[8246]149   Script* script = State::getScriptManager()->getScriptByFile(file);
[8243]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);
[8289]180   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]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
[8289]195   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]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 }
Note: See TracBrowser for help on using the repository browser.