Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/inputdevice/src/world_entities/script_triggers/action_trigger.cc @ 10630

Last change on this file since 10630 was 10630, checked in by snellen, 17 years ago

first step towards the action trigger

File size: 4.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
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#include "util/loading/factory.h"
17#include "action_trigger.h"
18#include "debug.h"
19
20ObjectListDefinition(ActionTrigger);
21CREATE_FACTORY(ActionTrigger);
22
23CREATE_SCRIPTABLE_CLASS(ActionTrigger,
24            // Coordinates
25                        addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
26                            ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
27                            ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
28                            ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
29            //Properties
30                            ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName))
31                            ->addMethod("setTarget", Executor1<ActionTrigger, lua_State*, const std::string&>(&ActionTrigger::setTarget))
32                            ->addMethod("setTriggerParent", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTriggerParent))
33                            ->addMethod("setInvert", Executor1<ActionTrigger, lua_State*, bool>(&ActionTrigger::setInvert))
34                            ->addMethod("setRadius", Executor1<ActionTrigger, lua_State*, float>(&ActionTrigger::setRadius))
35                            ->addMethod("setScript", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setScript))
36                            ->addMethod("setFunction", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setFunction))
37                            ->addMethod("setDebugDraw", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setDebugDraw))
38                            ->addMethod("setAddToScript", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setAddToScript))
39                       );
40
41
42/**
43 * Constructs a new ActionTrigger.
44 * @param root the xml element to load the parameters from.
45 *
46 */
47ActionTrigger::ActionTrigger(const TiXmlElement* root)
48{
49  this->registerObject(this, ActionTrigger::_objectList);
50
51  radius = 10;
52  invert = false;
53 
54  if(root != NULL)
55  {
56    loadParams(root);
57
58    if(addToScript && scriptIsOk)
59    {
60      script->addObject( "ActionTrigger", this->getName());
61    }
62
63  }
64
65}
66
67/**
68 * Deletes the ActionTrigger.
69 *
70 */
71ActionTrigger::~ActionTrigger()
72{
73
74}
75
76/**
77 * Reads the values from the tml element and sets them.
78 * @param root the xml element to load the parameters from.
79 *
80 */
81void ActionTrigger::loadParams(const TiXmlElement* root)
82{
83
84  ScriptTrigger ::loadParams(root);
85
86 
87  LoadParam(root, "radius", this, ActionTrigger, setRadius)
88      .describe("the fileName of the script, that should be triggered by this script trigger")
89      .defaultValues(0);
90  LoadParam(root, "worldentity", this, ActionTrigger, setTarget)
91      .describe("The name of the target as it is in the *.oxw file")
92      .defaultValues("");
93  LoadParam(root, "invert", this, ActionTrigger, setInvert)
94      .describe("")
95      .defaultValues(false);
96 
97}
98
99
100/**
101 * Sets the target(a world entity) of the ActionTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered.
102 * @param target The worldentity that the script supervises.
103 */
104void ActionTrigger::setTarget(const std::string& target)
105{
106
107  WorldEntity* targetEntity = WorldEntity::objectList().getObject(target);
108  if (targetEntity != NULL)
109  {
110    this->setTarget(targetEntity);
111  }
112  else
113  {
114    PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName());
115  }
116}
117
118
119void ActionTrigger::tick(float timestep)
120{
121  if( scriptFinished ) return;
122
123  if( this->target != NULL)
124  {
125    if( !invert && this->distance(target) < radius && actionScheduled)
126    {
127    //printf("Distance is %f \n", this->distance(target));
128      executeAction(timestep);
129      scriptCalled = true;
130      return;
131
132    }
133    else if( invert && this->distance(target) > radius  && actionScheduled)
134    {
135      executeAction(timestep);
136      scriptCalled = true;
137      return;
138    }
139  }
140}
141
142
143
Note: See TracBrowser for help on using the repository browser.