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 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "Engine.h" |
---|
31 | |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/ConfigValueIncludes.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | #include "objects/Scene.h" |
---|
36 | #include "objects/worldentities/pawns/SpaceShip.h" |
---|
37 | #include "tools/Shader.h" |
---|
38 | #include "sound/SoundBase.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(Engine); |
---|
43 | |
---|
44 | Engine::Engine(BaseObject* creator) : Item(creator) |
---|
45 | { |
---|
46 | RegisterObject(Engine); |
---|
47 | |
---|
48 | this->ship_ = 0; |
---|
49 | this->shipID_ = OBJECTID_UNKNOWN; |
---|
50 | |
---|
51 | this->boostFactor_ = 1.5; |
---|
52 | this->speedFactor_ = 1.0; |
---|
53 | |
---|
54 | this->maxSpeedFront_ = 0.0; |
---|
55 | this->maxSpeedBack_ = 0.0; |
---|
56 | this->maxSpeedLeftRight_ = 0.0; |
---|
57 | this->maxSpeedUpDown_ = 0.0; |
---|
58 | |
---|
59 | this->accelerationFront_ = 0.0; |
---|
60 | this->accelerationBrake_ = 0.0; |
---|
61 | this->accelerationBack_ = 0.0; |
---|
62 | this->accelerationLeftRight_ = 0.0; |
---|
63 | this->accelerationUpDown_ = 0.0; |
---|
64 | |
---|
65 | this->boostBlur_ = 0; |
---|
66 | |
---|
67 | this->setConfigValues(); |
---|
68 | this->registerVariables(); |
---|
69 | |
---|
70 | this->sound_ = NULL; |
---|
71 | } |
---|
72 | |
---|
73 | Engine::~Engine() |
---|
74 | { |
---|
75 | if (this->isInitialized() && this->ship_) |
---|
76 | { |
---|
77 | this->ship_->setEngine(0); |
---|
78 | |
---|
79 | if (this->boostBlur_) |
---|
80 | delete this->boostBlur_; |
---|
81 | |
---|
82 | if(this->sound_ != NULL) |
---|
83 | delete this->sound_; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
88 | { |
---|
89 | SUPER(Engine, XMLPort, xmlelement, mode); |
---|
90 | |
---|
91 | XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode); |
---|
92 | |
---|
93 | XMLPortParam(Engine, "speedfront", setMaxSpeedFront, setMaxSpeedFront, xmlelement, mode); |
---|
94 | XMLPortParam(Engine, "speedback", setMaxSpeedBack, setMaxSpeedBack, xmlelement, mode); |
---|
95 | XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode); |
---|
96 | XMLPortParam(Engine, "speedupdown", setMaxSpeedUpDown, setMaxSpeedUpDown, xmlelement, mode); |
---|
97 | |
---|
98 | XMLPortParam(Engine, "accelerationfront", setAccelerationFront, setAccelerationFront, xmlelement, mode); |
---|
99 | XMLPortParam(Engine, "accelerationbrake", setAccelerationBrake, setAccelerationBrake, xmlelement, mode); |
---|
100 | XMLPortParam(Engine, "accelerationback", setAccelerationBack, setAccelerationBack, xmlelement, mode); |
---|
101 | XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode); |
---|
102 | XMLPortParam(Engine, "accelerationupdown", setAccelerationUpDown, setAccelerationUpDown, xmlelement, mode); |
---|
103 | |
---|
104 | XMLPortParamLoadOnly(Engine, "sound", loadSound, xmlelement, mode); |
---|
105 | } |
---|
106 | |
---|
107 | void Engine::setConfigValues() |
---|
108 | { |
---|
109 | SetConfigValue(blurStrength_, 3.0f); |
---|
110 | } |
---|
111 | |
---|
112 | void Engine::registerVariables() |
---|
113 | { |
---|
114 | registerVariable(this->shipID_, variableDirection::toclient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID)); |
---|
115 | |
---|
116 | registerVariable(this->speedFactor_, variableDirection::toclient); |
---|
117 | registerVariable(this->boostFactor_, variableDirection::toclient); |
---|
118 | |
---|
119 | registerVariable(this->maxSpeedFront_, variableDirection::toclient); |
---|
120 | registerVariable(this->maxSpeedBack_, variableDirection::toclient); |
---|
121 | registerVariable(this->maxSpeedLeftRight_, variableDirection::toclient); |
---|
122 | registerVariable(this->maxSpeedUpDown_, variableDirection::toclient); |
---|
123 | |
---|
124 | registerVariable(this->accelerationFront_, variableDirection::toclient); |
---|
125 | registerVariable(this->accelerationBrake_, variableDirection::toclient); |
---|
126 | registerVariable(this->accelerationBack_, variableDirection::toclient); |
---|
127 | registerVariable(this->accelerationLeftRight_, variableDirection::toclient); |
---|
128 | registerVariable(this->accelerationUpDown_, variableDirection::toclient); |
---|
129 | } |
---|
130 | |
---|
131 | void Engine::networkcallback_shipID() |
---|
132 | { |
---|
133 | this->ship_ = 0; |
---|
134 | |
---|
135 | if (this->shipID_ != OBJECTID_UNKNOWN) |
---|
136 | { |
---|
137 | Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_); |
---|
138 | if (object) |
---|
139 | this->addToSpaceShip(dynamic_cast<SpaceShip*>(object)); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | void Engine::tick(float dt) |
---|
144 | { |
---|
145 | if (!this->ship_) |
---|
146 | { |
---|
147 | if (this->shipID_) |
---|
148 | { |
---|
149 | this->networkcallback_shipID(); |
---|
150 | |
---|
151 | if (!this->ship_) |
---|
152 | return; |
---|
153 | } |
---|
154 | else |
---|
155 | return; |
---|
156 | } |
---|
157 | |
---|
158 | if (!this->isActive()) |
---|
159 | return; |
---|
160 | |
---|
161 | SUPER(Engine, tick, dt); |
---|
162 | |
---|
163 | const Vector3& direction = this->getDirection(); |
---|
164 | Vector3 velocity = this->ship_->getLocalVelocity(); |
---|
165 | Vector3 acceleration = Vector3::ZERO; |
---|
166 | |
---|
167 | float factor = 1.0f / this->speedFactor_; |
---|
168 | velocity *= factor; |
---|
169 | |
---|
170 | if (direction.z < 0) |
---|
171 | { |
---|
172 | if (this->maxSpeedFront_ != 0) |
---|
173 | { |
---|
174 | float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f); |
---|
175 | acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); |
---|
176 | } |
---|
177 | } |
---|
178 | else if (direction.z > 0) |
---|
179 | { |
---|
180 | if (velocity.z < 0) |
---|
181 | acceleration.z = direction.z * this->accelerationBrake_; |
---|
182 | else if (this->maxSpeedBack_ != 0) |
---|
183 | acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f); |
---|
184 | } |
---|
185 | |
---|
186 | if (this->maxSpeedLeftRight_ != 0) |
---|
187 | { |
---|
188 | if (direction.x < 0) |
---|
189 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
190 | else if (direction.x > 0) |
---|
191 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
192 | } |
---|
193 | |
---|
194 | if (this->maxSpeedUpDown_ != 0) |
---|
195 | { |
---|
196 | if (direction.y < 0) |
---|
197 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
198 | else if (direction.y > 0) |
---|
199 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
200 | } |
---|
201 | |
---|
202 | this->ship_->setAcceleration(this->ship_->getPickups().processModifiers(ModifierType::Acceleration, this->ship_->getOrientation() * acceleration, false)); |
---|
203 | |
---|
204 | if (!this->ship_->getPermanentBoost()) |
---|
205 | this->ship_->setBoost(false); |
---|
206 | this->ship_->setSteeringDirection(Vector3::ZERO); |
---|
207 | |
---|
208 | if (!this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController()) |
---|
209 | { |
---|
210 | this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager()); |
---|
211 | this->boostBlur_->setCompositor("Radial Blur"); |
---|
212 | } |
---|
213 | |
---|
214 | if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) |
---|
215 | this->boostBlur_->setParameter("Ogre/Compositor/Radial_Blur", 0, 0, "sampleStrength", this->blurStrength_ * clamp((-velocity.z - this->maxSpeedFront_) / ((this->boostFactor_ - 1) * this->maxSpeedFront_), 0.0f, 1.0f)); |
---|
216 | } |
---|
217 | |
---|
218 | void Engine::changedActivity() |
---|
219 | { |
---|
220 | SUPER(Engine, changedActivity); |
---|
221 | |
---|
222 | if (this->boostBlur_) |
---|
223 | this->boostBlur_->setVisible(this->isVisible()); |
---|
224 | } |
---|
225 | |
---|
226 | void Engine::addToSpaceShip(SpaceShip* ship) |
---|
227 | { |
---|
228 | this->ship_ = ship; |
---|
229 | |
---|
230 | if (ship) |
---|
231 | { |
---|
232 | this->shipID_ = ship->getObjectID(); |
---|
233 | if (ship->getEngine() != this) |
---|
234 | ship->setEngine(this); |
---|
235 | |
---|
236 | if (this->boostBlur_) |
---|
237 | { |
---|
238 | delete this->boostBlur_; |
---|
239 | this->boostBlur_ = 0; |
---|
240 | } |
---|
241 | |
---|
242 | if(this->sound_ != NULL) |
---|
243 | this->sound_->attachToEntity(ship); |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | const Vector3& Engine::getDirection() const |
---|
248 | { |
---|
249 | if (this->ship_) |
---|
250 | return this->ship_->getSteeringDirection(); |
---|
251 | else |
---|
252 | return Vector3::ZERO; |
---|
253 | } |
---|
254 | |
---|
255 | void Engine::loadSound(const std::string filename) |
---|
256 | { |
---|
257 | if(filename == "") return; |
---|
258 | else |
---|
259 | { |
---|
260 | if(this->sound_ == NULL) |
---|
261 | { |
---|
262 | this->sound_ = new SoundBase(this->ship_); |
---|
263 | } |
---|
264 | |
---|
265 | this->sound_->loadFile(filename); |
---|
266 | this->sound_->play(true); |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|