Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/world_entities/script_trigger.cc @ 9611

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

removed testerror from scripttrigger

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