Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8254 was 8246, checked in by snellen, 19 years ago

moved example.cc to scripttrigger, object.cc, account.cc

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