Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/world_entities/script_trigger.cc @ 8319

Last change on this file since 8319 was 8271, checked in by bensch, 19 years ago

merge

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