Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptimprovements/src/world_entities/script_triggers/space_trigger.cc @ 10616

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

shame on me: how could I forget to add the new classes to Makefile.am , removed obsolete lua_vector.*

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