Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/script_trigger.cc @ 9193

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

added setName scriptable method to script trigger, added loadModel to spaceship

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