Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the single_player_map branche back
merged with command:
svn merge -r8896:HEAD https://svn.orxonox.net/orxonox/branches/single_player_map .
no conflicts

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