Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

commented unessecary debug output

File size: 5.1 KB
Line 
1//for testing
2#include "luaincl.h"
3#include "script_trigger.h"
4#include "class_list.h"
5#include "script.h"
6
7#include "state.h"
8
9
10
11ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
12{
13  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
14  this->toList(OM_COMMON);
15
16  doDebugDraw = false;
17  scriptCalled = false;
18  scriptIsOk = false;
19  loadParams(root);
20
21}
22
23ScriptTrigger::~ScriptTrigger()
24{
25
26}
27
28void ScriptTrigger::loadParams(const TiXmlElement* root)
29{
30  if(root != NULL)
31  {
32    WorldEntity ::loadParams(root);
33
34    LoadParam(root, "file", this, ScriptTrigger, setScript)
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("");
40    LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
41        .describe("where this script trigger should be located")
42        .defaultValues("");
43    LoadParam(root, "radius", this, ScriptTrigger, setRadius)
44        .describe("the fileName of the script, that should be triggered by this script trigger")
45        .defaultValues(0);
46    LoadParam(root, "delay", this, ScriptTrigger, setDelay)
47        .describe("the delay after which the funtion sould be triggered")
48        .defaultValues(0);
49    LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
50        .describe("The name of the target as it is in the *.oxw file")
51        .defaultValues("");
52    LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
53        .describe("The name of the parent as it is in the *.oxw file")
54        .defaultValues("");
55    LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce)
56        .describe("True if the script shoul only be called once")
57        .defaultValues("");
58    LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
59        .describe("True if the script should only be called once")
60        .defaultValues("");
61  }
62
63}
64
65
66void ScriptTrigger::setTarget(const std::string& target)
67{
68  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
69
70  if (targetEntity != NULL)
71  {
72    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
73  }
74  else
75  {
76    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
77  }
78}
79
80void ScriptTrigger::setTriggerParent(const std::string& parent)
81{
82  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
83
84  if (parentEntity != NULL)
85  {
86    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
87    this->setParentMode(PNODE_MOVEMENT);
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
95void ScriptTrigger::tick(float timestep)
96{
97
98  if( this->distance(target) < radius)
99 {
100  if(!callOnce)
101   {
102    executeAction();
103   }
104  else if(callOnce && !scriptCalled)
105  {
106   executeAction();
107   scriptCalled = true;
108  }
109 }
110 //else
111   //printf("SCRIPTTRIGGER: target out of range\n");
112
113}
114
115
116void ScriptTrigger::executeAction()
117{
118     if(scriptIsOk)
119     {
120       testScriptingFramework();
121     /*if(!(script->selectFunction(this->functionName,0)) )
122       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
123     if( !(script->executeFunction()) )
124       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/
125     }
126}
127
128
129void ScriptTrigger::setScript(const std::string& file)
130{
131
132  ScriptManager* scriptManager = State::getScriptManager();
133  if (scriptManager != NULL)
134  {
135
136    script = scriptManager->getScriptByFile(file);
137    if(script != NULL)
138    {
139      scriptIsOk = true;
140    }
141  }
142}
143
144
145 void ScriptTrigger::testScriptingFramework()
146 {
147   std::string file("lunartest2.lua");
148   //get script
149   Script* script = State::getScriptManager()->getScriptByFile(file);
150   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
151
152      //execute a function
153   printf("----------- main -----------\n");
154   std::string main("main");
155   if( script->selectFunction(main,3))
156     printf("function %s selected\n",main.c_str());
157
158   script->pushParam(3.14159,main);
159   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
160   script->executeFunction();
161
162   float retf = script->getReturnedFloat();
163   printf("main returned %f\n",retf);
164
165
166   if(script->getReturnedBool())
167     printf("main returned true\n");
168   else
169     printf("main returned false\n");
170
171   int ret = script->getReturnedInt();
172   printf("main returned %i\n",ret);
173
174   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
175      //execute a 2nd function
176   printf("----------- test -----------\n");
177   std::string test("test");
178   if( script->selectFunction(test,0))
179     printf("function %s selected\n",test.c_str());
180
181   script->executeFunction();
182
183
184      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
185   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
186
187 }
Note: See TracBrowser for help on using the repository browser.