Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

script trigger can now be added to a script

File size: 7.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Silvan Nellen
13   co-programmer: ...
14*/
15
16
17#include "script_trigger.h"
18#include "class_list.h"
19#include "script.h"
20
21#include "state.h"
22
23
24CREATE_SCRIPTABLE_CLASS(ScriptTrigger, CL_SCRIPT_TRIGGER,
25                            addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
26                                ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
27                                ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
28                                ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
29                       );
30
31
32/**
33 * Constructs a new ScriptTrigger.
34 * @param root the xml element to load the parameters from.
35 *
36 */
37ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
38{
39  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
40  this->toList(OM_COMMON);
41
42  returnCount = 1;
43  actionFinished = false;
44  doDebugDraw = false;
45  invert = false;
46  scriptCalled = false;
47  scriptIsOk = false;
48  triggerLasts = false;
49  addToScript = false;
50 
51  if(root != NULL)
52  {
53   
54  loadParams(root);
55   
56  printf("%s \n",this->getName());
57  BaseObject* object = ClassList::getObject(this->getName(), "ScriptTrigger");
58 // printf("%s \n",object->getName());
59 
60  if(addToScript)
61  {
62    script->addObject( "ScriptTrigger", this->getName());
63  }
64 
65  }
66}
67
68/**
69 * Deletes the ScriptTrigger.
70 *
71 */
72ScriptTrigger::~ScriptTrigger()
73{
74
75}
76
77/**
78 * Reads the values from the tml element and sets them.
79 * @param root the xml element to load the parameters from.
80 *
81 */
82void ScriptTrigger::loadParams(const TiXmlElement* root)
83{
84
85    WorldEntity ::loadParams(root);
86
87    LoadParam(root, "file", this, ScriptTrigger, setScript)
88        .describe("the fileName of the script, that should be triggered by this script trigger")
89        .defaultValues("");
90    LoadParam(root, "function", this, ScriptTrigger, setFunction)
91        .describe("the function of the script, that should be triggered by this script trigger")
92        .defaultValues("");
93    LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
94        .describe("where this script trigger should be located")
95        .defaultValues("");
96    LoadParam(root, "radius", this, ScriptTrigger, setRadius)
97        .describe("the fileName of the script, that should be triggered by this script trigger")
98        .defaultValues(0);
99    LoadParam(root, "delay", this, ScriptTrigger, setDelay)
100        .describe("the delay after which the funtion sould be triggered")
101        .defaultValues(0);
102    LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
103        .describe("The name of the target as it is in the *.oxw file")
104        .defaultValues("");
105    LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
106        .describe("The name of the parent as it is in the *.oxw file")
107        .defaultValues("");
108    LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce)
109        .describe("True if the script shoul only be called once")
110        .defaultValues("");
111    LoadParam(root, "invert", this, ScriptTrigger, setInvert)
112        .describe("")
113        .defaultValues("");
114    LoadParam(root, "triggerlasts", this, ScriptTrigger, setTriggerLasts)
115        .describe("")
116        .defaultValues("");
117    LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
118        .describe("True if the script should only be called once")
119        .defaultValues("");
120    LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript)
121        .describe("True if this scripttrigger should be aviable in the script")
122        .defaultValues("");
123}
124
125
126/**
127 * Sets the target(a world entity) of the ScriptTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered.
128 * @param target The worldentity that the script supervises.
129 */
130void ScriptTrigger::setTarget(const std::string& target)
131{
132  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
133
134  if (targetEntity != NULL)
135  {
136    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
137  }
138  else
139  {
140    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
141  }
142}
143
144/**
145 * Sets the parent of the trigger.
146 * @param parent The parrent.
147 */
148void ScriptTrigger::setTriggerParent(const std::string& parent)
149{
150  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
151
152  if (parentEntity != NULL)
153  {
154    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
155    this->setParentMode(PNODE_MOVEMENT);
156  }
157  else
158  {
159    PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName());
160  }
161}
162
163void ScriptTrigger::tick(float timestep)
164{
165  if(actionFinished) return;
166
167  if(triggerLasts && scriptCalled)
168  {
169    executeAction(timestep);
170    return;
171  }
172 
173  if( !invert && this->distance(target) < radius)
174 {
175  if(!callOnce)
176   {
177    executeAction(timestep);
178    scriptCalled = true;
179   }
180  else if(callOnce && !scriptCalled)
181  {
182   executeAction(timestep);
183   scriptCalled = true;
184  }
185 
186 }
187 else if( invert && this->distance(target) > radius)
188 {
189   if(!callOnce)
190   {
191     executeAction(timestep);
192   }
193   else if(callOnce && !scriptCalled)
194   {
195     executeAction(timestep);
196     scriptCalled = true;
197   }
198   
199 }
200 //else
201   //printf("SCRIPTTRIGGER: target out of range\n");
202
203}
204
205
206void ScriptTrigger::executeAction(float timestep)
207{
208     if(scriptIsOk)
209     {
210       //testScriptingFramework();
211     if(!(script->selectFunction(this->functionName,returnCount)) )
212       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
213     
214     script->pushParam( timestep, this->functionName);
215     
216     if( !(script->executeFunction()) )
217       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
218     
219     actionFinished = script->getReturnedBool();
220     }
221     
222     
223}
224
225
226void ScriptTrigger::setScript(const std::string& file)
227{
228
229  ScriptManager* scriptManager = State::getScriptManager();
230  if (scriptManager != NULL)
231  {
232
233    script = scriptManager->getScriptByFile(file);
234    if(script != NULL)
235    {
236      scriptIsOk = true;
237    }
238  }
239}
240
241/*
242 void ScriptTrigger::testScriptingFramework()
243 {
244   std::string file("lunartest2.lua");
245   //get script
246   Script* script = State::getScriptManager()->getScriptByFile(file);
247   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
248
249      //execute a function
250   printf("----------- main -----------\n");
251   std::string main("main");
252   if( script->selectFunction(main,3))
253     printf("function %s selected\n",main.c_str());
254
255   script->pushParam(3.14159,main);
256   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
257   script->executeFunction();
258
259   int ret = script->getReturnedInt();
260   printf("main returned %i\n",ret);
261
262   if(script->getReturnedBool())
263     printf("main returned true\n");
264   else
265     printf("main returned false\n");
266
267   float retf = script->getReturnedFloat();
268   printf("main returned %f\n",retf);
269   
270
271   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
272      //execute a 2nd function
273   printf("----------- test -----------\n");
274   std::string test("test");
275   if( script->selectFunction(test,0))
276     printf("function %s selected\n",test.c_str());
277
278   script->executeFunction();
279
280
281      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
282   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
283
284}*/
Note: See TracBrowser for help on using the repository browser.