Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/script_trigger.cc @ 8869

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

made FPSPlayer scriptable

File size: 7.3 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))
[8869]26                            ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
27                            ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
28                            ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
[8783]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;
[8869]43  scriptFinished = 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("");
[8711]104    LoadParam(root, "invert", this, ScriptTrigger, setInvert)
105        .describe("")
106        .defaultValues("");
107    LoadParam(root, "triggerlasts", this, ScriptTrigger, setTriggerLasts)
108        .describe("")
109        .defaultValues("");
[8408]110    LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
[8869]111        .describe("")
[8408]112        .defaultValues("");
[8783]113    LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript)
114        .describe("True if this scripttrigger should be aviable in the script")
115        .defaultValues("");
[8171]116}
117
118
[8408]119/**
120 * 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.
121 * @param target The worldentity that the script supervises.
122 */
[8199]123void ScriptTrigger::setTarget(const std::string& target)
124{
125  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
[8211]126
[8199]127  if (targetEntity != NULL)
128  {
129    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
130  }
131  else
132  {
[8202]133    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
[8199]134  }
135}
[8171]136
[8408]137/**
138 * Sets the parent of the trigger.
139 * @param parent The parrent.
140 */
[8205]141void ScriptTrigger::setTriggerParent(const std::string& parent)
142{
143  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
[8211]144
[8205]145  if (parentEntity != NULL)
146  {
147    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
[8207]148    this->setParentMode(PNODE_MOVEMENT);
[8205]149  }
150  else
151  {
152    PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName());
153  }
154}
155
[8171]156void ScriptTrigger::tick(float timestep)
157{
[8869]158  if(scriptFinished) return;
[8408]159
[8711]160  if(triggerLasts && scriptCalled)
161  {
162    executeAction(timestep);
163    return;
164  }
165 
166  if( !invert && this->distance(target) < radius)
[8171]167 {
[8711]168    executeAction(timestep);
169    scriptCalled = true;
170 
[8171]171 }
[8711]172 else if( invert && this->distance(target) > radius)
173 {
[8869]174     executeAction(timestep); 
[8711]175     scriptCalled = true;
176 }
[8408]177 //else
178   //printf("SCRIPTTRIGGER: target out of range\n");
[8171]179
180}
181
182
[8711]183void ScriptTrigger::executeAction(float timestep)
[8171]184{
[8869]185 
[8211]186     if(scriptIsOk)
[8207]187     {
[8711]188       //testScriptingFramework();
189     if(!(script->selectFunction(this->functionName,returnCount)) )
[8212]190       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
[8711]191     
192     script->pushParam( timestep, this->functionName);
193     
[8171]194     if( !(script->executeFunction()) )
[8711]195       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
196     
[8869]197     scriptFinished = script->getReturnedBool();
[8207]198     }
[8711]199     
200     
[8171]201}
[8211]202
203
204void ScriptTrigger::setScript(const std::string& file)
205{
[8408]206
[8239]207  ScriptManager* scriptManager = State::getScriptManager();
208  if (scriptManager != NULL)
209  {
[8408]210
[8239]211    script = scriptManager->getScriptByFile(file);
212    if(script != NULL)
[8408]213    {
[8239]214      scriptIsOk = true;
[8408]215    }
[8239]216  }
[8214]217}
[8243]218
[8711]219/*
[8243]220 void ScriptTrigger::testScriptingFramework()
221 {
222   std::string file("lunartest2.lua");
[8408]223   //get script
[8246]224   Script* script = State::getScriptManager()->getScriptByFile(file);
[8243]225   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
226
227      //execute a function
228   printf("----------- main -----------\n");
229   std::string main("main");
230   if( script->selectFunction(main,3))
231     printf("function %s selected\n",main.c_str());
232
233   script->pushParam(3.14159,main);
[8408]234   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]235   script->executeFunction();
236
[8408]237   int ret = script->getReturnedInt();
238   printf("main returned %i\n",ret);
[8243]239
240   if(script->getReturnedBool())
241     printf("main returned true\n");
242   else
243     printf("main returned false\n");
244
[8408]245   float retf = script->getReturnedFloat();
246   printf("main returned %f\n",retf);
247   
[8243]248
[8408]249   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
[8243]250      //execute a 2nd function
251   printf("----------- test -----------\n");
252   std::string test("test");
253   if( script->selectFunction(test,0))
254     printf("function %s selected\n",test.c_str());
255
256   script->executeFunction();
257
258
259      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
260   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
261
[8711]262}*/
Note: See TracBrowser for help on using the repository browser.