Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/world_entities/script_trigger.cc @ 8735

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

script trigger can now be added to a script

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