Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8821 was 8783, checked in by patrick, 18 years ago

merged the script engine branche back to trunk

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