Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added debug output

File size: 5.8 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  scriptCalled = false;
17  scriptIsOk = false;
18  loadParams(root);
19
20  printf("scripttrigger generated\n");
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  }
59
60}
61
62
63void ScriptTrigger::setTarget(const std::string& target)
64{
65  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
66
67  if (targetEntity != NULL)
68  {
69    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
70  }
71  else
72  {
73    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
74  }
75}
76
77void ScriptTrigger::setTriggerParent(const std::string& parent)
78{
79  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
80
81  if (parentEntity != NULL)
82  {
83    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
84    this->setParentMode(PNODE_MOVEMENT);
85  }
86  else
87  {
88    PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName());
89  }
90}
91
92void ScriptTrigger::tick(float timestep)
93{
94 
95  if( this->distance(target) < radius)
96 {
97   printf("SCRIPTTRIGGER: condition met\n");
98  if(!callOnce)
99   {
100    executeAction();
101   }
102  else if(callOnce && !scriptCalled)
103  {
104    printf("action should be executed\n");
105   executeAction();
106   scriptCalled = true;
107  }
108 }
109 else
110   printf("SCRIPTTRIGGER: target out of range\n");
111
112}
113
114
115void ScriptTrigger::executeAction()
116{
117     if(scriptIsOk)
118     {
119       testScriptingFramework();
120     if(!(script->selectFunction(this->functionName,0)) )
121       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
122     if( !(script->executeFunction()) )
123       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
124     }
125}
126
127
128void ScriptTrigger::setScript(const std::string& file)
129{
130  ScriptManager* scriptManager = State::getScriptManager();
131  if (scriptManager != NULL)
132  {
133    script = scriptManager->getScriptByFile(file);
134    if(script != NULL)
135      scriptIsOk = true;
136  }
137}
138
139
140 void ScriptTrigger::testScriptingFramework()
141 {
142   std::string file("lunartest2.lua");
143     // Script script;
144   Script* script = State::getScriptManager()->getScriptByFile(file);
145   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
146
147      // Register classes with the script
148      // Lunar<Account>::Register(&script);
149      //Lunar<Object>::Register(&script);
150
151      //Object* obj= new Object();
152      //Account a;
153      //Account *b = new Account(30);
154
155      // Add instances to the script
156      //Lunar<Object>::insertObject(&script,obj,"Obj",true);
157      //Lunar<Account>::insertObject(&script,&a,"a",false);
158      //Lunar<Account>::insertObject(&script,b,"b",true);
159       //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
160
161      //Load a file
162      //std::string file("lunartest2.lua");
163
164      //if(script.loadFile(file))
165        //printf("File %s succefully loaded\n", file.c_str());
166      //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
167
168      //execute a function
169   printf("----------- main -----------\n");
170   std::string main("main");
171   if( script->selectFunction(main,3))
172     printf("function %s selected\n",main.c_str());
173
174   script->pushParam(3.14159,main);
175   script->executeFunction();
176
177   float retf = script->getReturnedFloat();
178   printf("main returned %f\n",retf);
179
180
181   if(script->getReturnedBool())
182     printf("main returned true\n");
183   else
184     printf("main returned false\n");
185
186   int ret = script->getReturnedInt();
187   printf("main returned %i\n",ret);
188
189      //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
190      //execute a 2nd function
191   printf("----------- test -----------\n");
192   std::string test("test");
193   if( script->selectFunction(test,0))
194     printf("function %s selected\n",test.c_str());
195
196   script->executeFunction();
197
198
199      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
200   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
201
202 }
Note: See TracBrowser for help on using the repository browser.