Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9109 was 9061, checked in by patrick, 18 years ago

merged the single_player branche to trunk

File size: 8.2 KB
RevLine 
[8711]1/*
2   orxonox - the future of 3D-vertical-scrollers
[8408]3
[8711]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
[9006]16
[8171]17#include "script_trigger.h"
[8202]18#include "class_list.h"
19#include "script.h"
[8171]20
[8239]21#include "state.h"
[8408]22
23
[8783]24CREATE_SCRIPTABLE_CLASS(ScriptTrigger, CL_SCRIPT_TRIGGER,
[9061]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             );
[8408]41
[8783]42
[8408]43/**
44 * Constructs a new ScriptTrigger.
45 * @param root the xml element to load the parameters from.
46 *
47 */
[8202]48ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
[8171]49{
[8408]50  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
51  this->toList(OM_COMMON);
52
[8711]53  returnCount = 1;
[8894]54  scriptFinished = false;
[8408]55  doDebugDraw = false;
[8711]56  invert = false;
[8202]57  scriptCalled = false;
[8211]58  scriptIsOk = false;
[8894]59  triggerLasts = true;
[8783]60  addToScript = false;
61 
62  if(root != NULL)
63  {
64   
[9006]65    loadParams(root);
[8783]66 
[9006]67    if(addToScript && scriptIsOk)
68    {
69      script->addObject( "ScriptTrigger", this->getName());
70    }
[8783]71 
72  }
[8171]73}
74
[8408]75/**
76 * Deletes the ScriptTrigger.
77 *
78 */
[8171]79ScriptTrigger::~ScriptTrigger()
80{
81
82}
83
[8408]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 */
[8171]89void ScriptTrigger::loadParams(const TiXmlElement* root)
90{
[8783]91
[9006]92  WorldEntity ::loadParams(root);
[8211]93
[9006]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");
[8171]127}
128
129
[8408]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 */
[8199]134void ScriptTrigger::setTarget(const std::string& target)
135{
136  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
[8211]137
[8199]138  if (targetEntity != NULL)
139  {
140    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
141  }
142  else
143  {
[8202]144    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
[8199]145  }
146}
[8171]147
[8408]148/**
149 * Sets the parent of the trigger.
150 * @param parent The parrent.
151 */
[8205]152void ScriptTrigger::setTriggerParent(const std::string& parent)
153{
154  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
[8211]155
[8205]156  if (parentEntity != NULL)
157  {
158    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
[8207]159    this->setParentMode(PNODE_MOVEMENT);
[8205]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
[8171]167void ScriptTrigger::tick(float timestep)
168{
[8894]169  if(scriptFinished) return;
[8408]170
[8711]171  if(triggerLasts && scriptCalled)
172  {
173    executeAction(timestep);
174    return;
175  }
176 
177  if( !invert && this->distance(target) < radius)
[9006]178  {
[8711]179    executeAction(timestep);
180    scriptCalled = true;
181 
[9006]182  }
183  else if( invert && this->distance(target) > radius)
184  {
185    executeAction(timestep); 
186    scriptCalled = true;
187  }
[8408]188 //else
189   //printf("SCRIPTTRIGGER: target out of range\n");
[8171]190
191}
192
193
[8711]194void ScriptTrigger::executeAction(float timestep)
[8171]195{
[8894]196 
[9006]197  if(scriptIsOk)
198  {
[8711]199       //testScriptingFramework();
[9006]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());
[8711]202     
[9006]203    script->pushParam( timestep, this->functionName);
[8711]204     
[9006]205    if( !(script->executeFunction()) )
206      printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
[8711]207     
[9006]208    scriptFinished = script->getReturnedBool();
209  }
[8711]210     
211     
[8171]212}
[8211]213
214
215void ScriptTrigger::setScript(const std::string& file)
216{
[8408]217
[8239]218  ScriptManager* scriptManager = State::getScriptManager();
219  if (scriptManager != NULL)
220  {
[8408]221
[8239]222    script = scriptManager->getScriptByFile(file);
223    if(script != NULL)
[8408]224    {
[8239]225      scriptIsOk = true;
[8408]226    }
[8239]227  }
[8214]228}
[8243]229
[8711]230/*
[8243]231 void ScriptTrigger::testScriptingFramework()
[9006]232{
[8243]233   std::string file("lunartest2.lua");
[8408]234   //get script
[8246]235   Script* script = State::getScriptManager()->getScriptByFile(file);
[8243]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);
[8408]245   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]246   script->executeFunction();
247
[8408]248   int ret = script->getReturnedInt();
249   printf("main returned %i\n",ret);
[8243]250
251   if(script->getReturnedBool())
252     printf("main returned true\n");
253   else
254     printf("main returned false\n");
255
[8408]256   float retf = script->getReturnedFloat();
257   printf("main returned %f\n",retf);
258   
[8243]259
[8408]260   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]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
[8711]273}*/
Note: See TracBrowser for help on using the repository browser.