1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Reto Grieder |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #include "MultiStateEngine.h" |
---|
31 | |
---|
32 | extern "C" { |
---|
33 | #include <lua.h> |
---|
34 | } |
---|
35 | |
---|
36 | #include "util/Convert.h" |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "core/GameMode.h" |
---|
39 | #include "core/LuaState.h" |
---|
40 | #include "core/XMLPort.h" |
---|
41 | #include "worldentities/EffectContainer.h" |
---|
42 | #include "worldentities/pawns/SpaceShip.h" |
---|
43 | #include "sound/WorldSound.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 0; |
---|
48 | static const float MAX_VELOCITY_NORMAL = 111; |
---|
49 | static const float MAX_VELOCITY_BOOST = 221; |
---|
50 | |
---|
51 | CreateFactory(MultiStateEngine); |
---|
52 | |
---|
53 | MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) |
---|
54 | { |
---|
55 | RegisterObject(MultiStateEngine); |
---|
56 | |
---|
57 | this->defEngineSndNormal_ = new WorldSound(this); |
---|
58 | this->defEngineSndBoost_ = new WorldSound(this); |
---|
59 | this->defEngineSndNormal_->setLooping(true); |
---|
60 | this->defEngineSndBoost_->setLooping(true); |
---|
61 | |
---|
62 | this->lua_ = new LuaState(); |
---|
63 | this->state_ = 0; |
---|
64 | |
---|
65 | this->registerVariables(); |
---|
66 | } |
---|
67 | |
---|
68 | MultiStateEngine::~MultiStateEngine() |
---|
69 | { |
---|
70 | if (this->isInitialized() && !this->getShip()) |
---|
71 | { |
---|
72 | // We have no ship, so the effects are not attached and won't be destroyed automatically |
---|
73 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
74 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) |
---|
75 | (*it2)->destroy(); |
---|
76 | delete this->defEngineSndNormal_; |
---|
77 | delete this->defEngineSndBoost_; |
---|
78 | delete this->lua_; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
83 | { |
---|
84 | SUPER(MultiStateEngine, XMLPort, xmlelement, mode); |
---|
85 | XMLPortObject(MultiStateEngine, EffectContainer, "", addEffectContainer, getEffectContainer, xmlelement, mode); |
---|
86 | XMLPortParam(MultiStateEngine, "defEngineSndNormal", setDefEngSndNormal, getDefEngSndNormal, xmlelement, mode); |
---|
87 | XMLPortParam(MultiStateEngine, "defEngineSndBoost", setDefEngSndBoost, getDefEngSndBoost, xmlelement, mode); |
---|
88 | } |
---|
89 | |
---|
90 | void MultiStateEngine::registerVariables() |
---|
91 | { |
---|
92 | registerVariable(this->state_, VariableDirection::ToServer); |
---|
93 | } |
---|
94 | |
---|
95 | void MultiStateEngine::tick(float dt) |
---|
96 | { |
---|
97 | if (this->getShip()) |
---|
98 | { |
---|
99 | if (this->getShip()->hasLocalController()) |
---|
100 | { |
---|
101 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
102 | |
---|
103 | const Vector3& direction = this->getDirection(); |
---|
104 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
105 | |
---|
106 | float pitch = velocity.length(); |
---|
107 | bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
108 | |
---|
109 | int newState = 0; |
---|
110 | if (this->getShip()->getBoost() && forward) |
---|
111 | { |
---|
112 | newState = Boost; |
---|
113 | pitch = pitch/MAX_VELOCITY_BOOST + 1; |
---|
114 | pitch = pitch > 2 ? 2 : pitch; |
---|
115 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
116 | defEngineSndBoost_->setPitch(pitch); |
---|
117 | } |
---|
118 | else if (forward && !newState) // newState == Boost |
---|
119 | { |
---|
120 | newState = Normal; |
---|
121 | pitch = pitch/MAX_VELOCITY_NORMAL + 1; |
---|
122 | pitch = pitch > 2 ? 2 : pitch; |
---|
123 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
124 | defEngineSndNormal_->setPitch(pitch); |
---|
125 | } |
---|
126 | else if (direction.z > 0 && velocity.z < 0) |
---|
127 | newState = Brake; |
---|
128 | else |
---|
129 | newState = Idle; |
---|
130 | |
---|
131 | if (newState != this->state_) |
---|
132 | { |
---|
133 | int changes = newState | this->state_; |
---|
134 | if (changes & Idle) |
---|
135 | { |
---|
136 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle); |
---|
137 | lua_setglobal(this->lua_->getInternalLuaState(), "idle"); |
---|
138 | } |
---|
139 | if (changes & Normal) |
---|
140 | { |
---|
141 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal); |
---|
142 | lua_setglobal(this->lua_->getInternalLuaState(), "normal"); |
---|
143 | if(newState & Normal) |
---|
144 | { |
---|
145 | defEngineSndNormal_->play(); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | defEngineSndNormal_->stop(); |
---|
150 | } |
---|
151 | } |
---|
152 | if (changes & Brake) |
---|
153 | { |
---|
154 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake); |
---|
155 | lua_setglobal(this->lua_->getInternalLuaState(), "brake"); |
---|
156 | } |
---|
157 | if (changes & Boost) |
---|
158 | { |
---|
159 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost); |
---|
160 | lua_setglobal(this->lua_->getInternalLuaState(), "boost"); |
---|
161 | if(newState & Boost) |
---|
162 | { |
---|
163 | defEngineSndBoost_->play(); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | defEngineSndBoost_->stop(); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | // Update all effect conditions |
---|
172 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
173 | (*it)->updateCondition(); |
---|
174 | |
---|
175 | this->state_ = newState; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | if (GameMode::isMaster()) |
---|
180 | { |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | SUPER(MultiStateEngine, tick, dt); |
---|
185 | } |
---|
186 | |
---|
187 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
188 | { |
---|
189 | Engine::addToSpaceShip(ship); |
---|
190 | |
---|
191 | if (!ship) |
---|
192 | return; |
---|
193 | |
---|
194 | this->getShip()->attach(defEngineSndNormal_); |
---|
195 | this->getShip()->attach(defEngineSndBoost_); |
---|
196 | |
---|
197 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
198 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) |
---|
199 | this->getShip()->attach(*it2); |
---|
200 | } |
---|
201 | |
---|
202 | void MultiStateEngine::addEffectContainer(EffectContainer* effect) |
---|
203 | { |
---|
204 | if (effect == NULL) |
---|
205 | return; |
---|
206 | effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size())); |
---|
207 | this->effectContainers_.push_back(effect); |
---|
208 | if (this->getShip()) |
---|
209 | { |
---|
210 | for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) |
---|
211 | this->getShip()->attach(*it); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const |
---|
216 | { |
---|
217 | unsigned int i = 0; |
---|
218 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
219 | { |
---|
220 | if (i == index) |
---|
221 | return (*it); |
---|
222 | } |
---|
223 | return NULL; |
---|
224 | } |
---|
225 | |
---|
226 | void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound) |
---|
227 | { |
---|
228 | defEngineSndNormal_->setSource(engineSound); |
---|
229 | } |
---|
230 | |
---|
231 | const std::string& MultiStateEngine::getDefEngSndNormal() |
---|
232 | { |
---|
233 | return defEngineSndNormal_->getSource(); |
---|
234 | } |
---|
235 | |
---|
236 | void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound) |
---|
237 | { |
---|
238 | defEngineSndBoost_->setSource(engineSound); |
---|
239 | } |
---|
240 | |
---|
241 | const std::string& MultiStateEngine::getDefEngSndBoost() |
---|
242 | { |
---|
243 | return defEngineSndBoost_->getSource(); |
---|
244 | } |
---|
245 | } |
---|