Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9163 was 9110, checked in by bensch, 18 years ago

orxonox/trunk: merged the Presentation back

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