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 | #include "event_handler.h" |
---|
20 | |
---|
21 | ObjectListDefinition(ActionTrigger); |
---|
22 | CREATE_FACTORY(ActionTrigger); |
---|
23 | |
---|
24 | CREATE_SCRIPTABLE_CLASS(ActionTrigger, |
---|
25 | // Coordinates |
---|
26 | addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
27 | ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
28 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
29 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
30 | //Properties |
---|
31 | ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName)) |
---|
32 | ->addMethod("setTarget", Executor1<ActionTrigger, lua_State*, const std::string&>(&ActionTrigger::setTarget)) |
---|
33 | ->addMethod("setTriggerParent", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTriggerParent)) |
---|
34 | ->addMethod("setInvert", Executor1<ActionTrigger, lua_State*, bool>(&ActionTrigger::setInvert)) |
---|
35 | ->addMethod("setRadius", Executor1<ActionTrigger, lua_State*, float>(&ActionTrigger::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 ActionTrigger. |
---|
45 | * @param root the xml element to load the parameters from. |
---|
46 | * |
---|
47 | */ |
---|
48 | ActionTrigger::ActionTrigger(const TiXmlElement* root) |
---|
49 | { |
---|
50 | this->registerObject(this, ActionTrigger::_objectList); |
---|
51 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_ACTION); |
---|
52 | |
---|
53 | radius = 10; |
---|
54 | invert = false; |
---|
55 | actionScheduled = false; |
---|
56 | |
---|
57 | if(root != NULL) |
---|
58 | { |
---|
59 | loadParams(root); |
---|
60 | |
---|
61 | if(addToScript && scriptIsOk) |
---|
62 | { |
---|
63 | script->addObject( "ActionTrigger", this->getName()); |
---|
64 | } |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Deletes the ActionTrigger. |
---|
72 | * |
---|
73 | */ |
---|
74 | ActionTrigger::~ActionTrigger() |
---|
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 | */ |
---|
84 | void ActionTrigger::loadParams(const TiXmlElement* root) |
---|
85 | { |
---|
86 | |
---|
87 | ScriptTrigger ::loadParams(root); |
---|
88 | |
---|
89 | |
---|
90 | LoadParam(root, "radius", this, ActionTrigger, setRadius) |
---|
91 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
92 | .defaultValues(0); |
---|
93 | LoadParam(root, "worldentity", this, ActionTrigger, setTarget) |
---|
94 | .describe("The name of the target as it is in the *.oxw file") |
---|
95 | .defaultValues(""); |
---|
96 | LoadParam(root, "invert", this, ActionTrigger, setInvert) |
---|
97 | .describe("") |
---|
98 | .defaultValues(false); |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * 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. |
---|
105 | * @param target The worldentity that the script supervises. |
---|
106 | */ |
---|
107 | void ActionTrigger::setTarget(const std::string& target) |
---|
108 | { |
---|
109 | |
---|
110 | WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); |
---|
111 | if (targetEntity != NULL) |
---|
112 | { |
---|
113 | this->setTarget(targetEntity); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName()); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | void ActionTrigger::process( const Event &event) |
---|
122 | { |
---|
123 | if (event.type == KeyMapper::PEV_ACTION && event.bPressed) |
---|
124 | { |
---|
125 | actionScheduled = true; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | void ActionTrigger::tick( float timestep ) |
---|
130 | { |
---|
131 | if( scriptFinished ) return; |
---|
132 | |
---|
133 | if( this->target != NULL && actionScheduled) |
---|
134 | { |
---|
135 | if( !invert && this->distance(target) < radius && actionScheduled) |
---|
136 | { |
---|
137 | executeScriptFunction(timestep); |
---|
138 | scriptCalled = true; |
---|
139 | } |
---|
140 | else if( invert && this->distance(target) > radius && actionScheduled) |
---|
141 | { |
---|
142 | executeScriptFunction(timestep); |
---|
143 | scriptCalled = true; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | actionScheduled = false; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|