Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Triggers can now be creater from the xml file… again

File size: 4.6 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
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<SpaceTrigger, lua_State*, const std::string&>(&SpaceTrigger::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<SpaceTrigger, lua_State*, const std::string&>(&SpaceTrigger::setScript))
37                            ->addMethod("setFunction", Executor1<SpaceTrigger, lua_State*, const std::string&>(&SpaceTrigger::setFunction))
38                            ->addMethod("setDebugDraw", Executor1<SpaceTrigger, lua_State*, bool>(&SpaceTrigger::setDebugDraw))
39                            ->addMethod("setAddToScript", Executor1<SpaceTrigger, lua_State*, bool>(&SpaceTrigger::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
58
59}
60
61/**
62 * Deletes the SpaceTrigger.
63 *
64 */
65SpaceTrigger::~SpaceTrigger()
66{
67
68}
69
70/**
71 * Reads the values from the tml element and sets them.
72 * @param root the xml element to load the parameters from.
73 *
74 */
75void SpaceTrigger::loadParams(const TiXmlElement* root)
76{
77
78  ScriptTrigger ::loadParams(root);
79
80 
81  LoadParam(root, "radius", this, SpaceTrigger, setRadius)
82      .describe("the fileName of the script, that should be triggered by this script trigger")
83      .defaultValues(0);
84  LoadParam(root, "worldentity", this, SpaceTrigger, setTarget)
85      .describe("The name of the target as it is in the *.oxw file")
86      .defaultValues("");
87  LoadParam(root, "invert", this, SpaceTrigger, setInvert)
88      .describe("")
89      .defaultValues(false);
90  LoadParam(root, "triggerRemains", this, SpaceTrigger, setTriggerRemains)
91      .describe("")
92      .defaultValues(true);
93 
94}
95
96
97/**
98 * 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.
99 * @param target The worldentity that the script supervises.
100 */
101void SpaceTrigger::setTarget(const std::string& target)
102{
103
104  WorldEntity* targetEntity = WorldEntity::objectList().getObject(target);
105  if (targetEntity != NULL)
106  {
107    this->setTarget(targetEntity);
108  }
109  else
110  {
111    PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName());
112  }
113}
114
115
116void SpaceTrigger::tick(float timestep)
117{
118  if( scriptFinished ) return;
119
120  if(triggerRemains && scriptCalled )
121  {
122    executeAction(timestep);
123    return;
124  }
125
126  if( this->target != NULL)
127  {
128    if( !invert && this->distance(target) < radius)
129    {
130    //printf("Distance is %f \n", this->distance(target));
131      executeAction(timestep);
132      scriptCalled = true;
133      return;
134
135    }
136    else if( invert && this->distance(target) > radius)
137    {
138      executeAction(timestep);
139      scriptCalled = true;
140      return;
141    }
142  }
143}
144
145
146
Note: See TracBrowser for help on using the repository browser.