Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/script_trigger.cc @ 9243

Last change on this file since 9243 was 9235, checked in by bensch, 18 years ago

merged the presentation back

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