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 | |
---|
20 | ObjectListDefinition(ActionTrigger); |
---|
21 | CREATE_FACTORY(ActionTrigger); |
---|
22 | |
---|
23 | CREATE_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 | */ |
---|
47 | ActionTrigger::ActionTrigger(const TiXmlElement* root) |
---|
48 | { |
---|
49 | this->registerObject(this, ActionTrigger::_objectList); |
---|
50 | |
---|
51 | radius = 10; |
---|
52 | invert = false; |
---|
53 | actionScheduled = false; |
---|
54 | |
---|
55 | if(root != NULL) |
---|
56 | { |
---|
57 | loadParams(root); |
---|
58 | |
---|
59 | if(addToScript && scriptIsOk) |
---|
60 | { |
---|
61 | script->addObject( "ActionTrigger", this->getName()); |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Deletes the ActionTrigger. |
---|
70 | * |
---|
71 | */ |
---|
72 | ActionTrigger::~ActionTrigger() |
---|
73 | { |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
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 | */ |
---|
82 | void ActionTrigger::loadParams(const TiXmlElement* root) |
---|
83 | { |
---|
84 | |
---|
85 | ScriptTrigger ::loadParams(root); |
---|
86 | |
---|
87 | |
---|
88 | LoadParam(root, "radius", this, ActionTrigger, setRadius) |
---|
89 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
90 | .defaultValues(0); |
---|
91 | LoadParam(root, "worldentity", this, ActionTrigger, setTarget) |
---|
92 | .describe("The name of the target as it is in the *.oxw file") |
---|
93 | .defaultValues(""); |
---|
94 | LoadParam(root, "invert", this, ActionTrigger, setInvert) |
---|
95 | .describe("") |
---|
96 | .defaultValues(false); |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * 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. |
---|
103 | * @param target The worldentity that the script supervises. |
---|
104 | */ |
---|
105 | void ActionTrigger::setTarget(const std::string& target) |
---|
106 | { |
---|
107 | |
---|
108 | WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); |
---|
109 | if (targetEntity != NULL) |
---|
110 | { |
---|
111 | this->setTarget(targetEntity); |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName()); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void ActionTrigger::tick( float timestep ) |
---|
121 | { |
---|
122 | if( scriptFinished || !actionScheduled ) return; |
---|
123 | |
---|
124 | if( this->target != NULL) |
---|
125 | { |
---|
126 | if( !invert && this->distance(target) < radius && actionScheduled) |
---|
127 | { |
---|
128 | executeScriptFunction(timestep); |
---|
129 | scriptCalled = true; |
---|
130 | return; |
---|
131 | } |
---|
132 | else if( invert && this->distance(target) > radius && actionScheduled) |
---|
133 | { |
---|
134 | executeScriptFunction(timestep); |
---|
135 | scriptCalled = true; |
---|
136 | return; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | actionScheduled = false; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|