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 "Backlight.h" |
---|
31 | |
---|
32 | #include <OgreRibbonTrail.h> |
---|
33 | #include <OgreSceneManager.h> |
---|
34 | |
---|
35 | #include "core/Core.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "objects/Scene.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(Backlight); |
---|
43 | |
---|
44 | Backlight::Backlight(BaseObject* creator) : Billboard(creator) |
---|
45 | { |
---|
46 | RegisterObject(Backlight); |
---|
47 | |
---|
48 | this->ribbonTrail_ = 0; |
---|
49 | this->ribbonTrailNode_ = 0; |
---|
50 | |
---|
51 | this->width_ = 0; |
---|
52 | this->length_ = 1.0f; |
---|
53 | this->lifetime_ = 1.0f; |
---|
54 | this->maxelements_ = 1; |
---|
55 | |
---|
56 | if (Core::showsGraphics()) |
---|
57 | { |
---|
58 | assert(this->getScene()); |
---|
59 | assert(this->getScene()->getSceneManager()); |
---|
60 | assert(this->getScene()->getRootSceneNode()); |
---|
61 | |
---|
62 | this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName()); |
---|
63 | this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
64 | this->ribbonTrailNode_->attachObject(this->ribbonTrail_); |
---|
65 | this->ribbonTrail_->addNode(this->getNode()); |
---|
66 | |
---|
67 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); |
---|
68 | this->ribbonTrail_->setTrailLength(this->length_); |
---|
69 | this->ribbonTrail_->setInitialWidth(0, this->width_); |
---|
70 | } |
---|
71 | |
---|
72 | this->registerVariables(); |
---|
73 | } |
---|
74 | |
---|
75 | Backlight::~Backlight() |
---|
76 | { |
---|
77 | if (this->isInitialized()) |
---|
78 | { |
---|
79 | if (this->ribbonTrail_) |
---|
80 | { |
---|
81 | if (this->ribbonTrailNode_) |
---|
82 | { |
---|
83 | this->ribbonTrailNode_->detachObject(this->ribbonTrail_); |
---|
84 | this->getScene()->getSceneManager()->destroySceneNode(this->ribbonTrailNode_->getName()); |
---|
85 | } |
---|
86 | this->getScene()->getSceneManager()->destroyRibbonTrail(this->ribbonTrail_); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
92 | { |
---|
93 | SUPER(Backlight, XMLPort, xmlelement, mode); |
---|
94 | |
---|
95 | XMLPortParam(Backlight, "length", setLength, getLength, xmlelement, mode).defaultValues(100.0f); |
---|
96 | XMLPortParam(Backlight, "width", setWidth, getWidth, xmlelement, mode).defaultValues(1.0f); |
---|
97 | XMLPortParam(Backlight, "elements", setMaxElements, getMaxElements, xmlelement, mode).defaultValues(10); |
---|
98 | XMLPortParam(Backlight, "lifetime", setLifetime, getLifetime, xmlelement, mode).defaultValues(1.0f); |
---|
99 | XMLPortParam(Backlight, "trailmaterial", setTrailMaterial, getTrailMaterial, xmlelement, mode); |
---|
100 | } |
---|
101 | |
---|
102 | void Backlight::registerVariables() |
---|
103 | { |
---|
104 | REGISTERDATA (this->width_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_width)); |
---|
105 | REGISTERDATA (this->lifetime_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_lifetime)); |
---|
106 | REGISTERDATA (this->length_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_length)); |
---|
107 | REGISTERDATA (this->maxelements_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_maxelements)); |
---|
108 | REGISTERSTRING(this->trailmaterial_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_trailmaterial)); |
---|
109 | } |
---|
110 | |
---|
111 | void Backlight::changedColour() |
---|
112 | { |
---|
113 | Billboard::changedColour(); |
---|
114 | |
---|
115 | if (this->ribbonTrail_ && this->isActive()) |
---|
116 | this->ribbonTrail_->setInitialColour(0, this->getColour()); |
---|
117 | } |
---|
118 | |
---|
119 | void Backlight::update_width() |
---|
120 | { |
---|
121 | if (this->ribbonTrail_) |
---|
122 | this->ribbonTrail_->setInitialWidth(0, this->width_); |
---|
123 | this->update_lifetime(); |
---|
124 | } |
---|
125 | |
---|
126 | void Backlight::update_lifetime() |
---|
127 | { |
---|
128 | if (this->ribbonTrail_) |
---|
129 | { |
---|
130 | this->ribbonTrail_->setWidthChange(0, this->width_ / this->lifetime_/* * Backlight::timeFactor_s*/); |
---|
131 | this->ribbonTrail_->setColourChange(0, 0, 0, 0, 1.0f / this->lifetime_/* * Backlight::timeFactor_s*/); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void Backlight::update_length() |
---|
136 | { |
---|
137 | // if (this->ribbonTrail_) |
---|
138 | // this->ribbonTrail_->setTrailLength(this->length_); |
---|
139 | } |
---|
140 | |
---|
141 | void Backlight::update_maxelements() |
---|
142 | { |
---|
143 | if (this->ribbonTrail_) |
---|
144 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); |
---|
145 | } |
---|
146 | |
---|
147 | void Backlight::update_trailmaterial() |
---|
148 | { |
---|
149 | if (this->ribbonTrail_) |
---|
150 | this->ribbonTrail_->setMaterialName(this->trailmaterial_); |
---|
151 | } |
---|
152 | |
---|
153 | void Backlight::changedVisibility() |
---|
154 | { |
---|
155 | SUPER(Backlight, changedVisibility); |
---|
156 | |
---|
157 | if (this->ribbonTrail_) |
---|
158 | this->ribbonTrail_->setVisible(this->isVisible()); |
---|
159 | } |
---|
160 | |
---|
161 | void Backlight::changedActivity() |
---|
162 | { |
---|
163 | SUPER(Backlight, changedActivity); |
---|
164 | |
---|
165 | if (this->ribbonTrail_) |
---|
166 | { |
---|
167 | if (this->isActive()) |
---|
168 | this->ribbonTrail_->setInitialColour(0, this->getColour()); |
---|
169 | else |
---|
170 | this->ribbonTrail_->setInitialColour(0, 0, 0, 0, 0); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | void Backlight::notifyAttached() |
---|
175 | { |
---|
176 | Billboard::notifyAttached(); |
---|
177 | |
---|
178 | // if (this->ribbonTrail_) |
---|
179 | // this->ribbonTrail_->clearChain(0); |
---|
180 | |
---|
181 | // if (this->ribbonTrail_) |
---|
182 | // this->ribbonTrail_->setTrailLength(this->length_); |
---|
183 | } |
---|
184 | |
---|
185 | void Backlight::tick(float dt) |
---|
186 | { |
---|
187 | if (this->ribbonTrail_) |
---|
188 | this->ribbonTrail_->setTrailLength(this->length_); |
---|
189 | } |
---|
190 | |
---|
191 | //------------------------------------------------------------------------------------ |
---|
192 | /* |
---|
193 | float Backlight::timeFactor_s = 1.0; |
---|
194 | |
---|
195 | Backlight::Backlight(float maxspeed, float brakingtime, float scale) |
---|
196 | { |
---|
197 | RegisterObject(Backlight); |
---|
198 | |
---|
199 | this->setConfigValues(); |
---|
200 | this->traillength_ = 1; |
---|
201 | this->colour_ = ColourValue::White; |
---|
202 | |
---|
203 | this->configure(maxspeed, brakingtime, scale); |
---|
204 | } |
---|
205 | |
---|
206 | bool Backlight::create(){ |
---|
207 | if(!WorldEntity::create()) |
---|
208 | return false; |
---|
209 | |
---|
210 | this->getNode()->setInheritScale(false); |
---|
211 | |
---|
212 | this->billboard_.setBillboardSet("Flares/backlightflare"); |
---|
213 | this->attachObject(this->billboard_.getBillboardSet()); |
---|
214 | |
---|
215 | this->ribbonTrail_ = GraphicsEngine::getInstance().getLevelSceneManager()->createRibbonTrail(this->getName() + "RibbonTrail"); |
---|
216 | this->ribbonTrailNode_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName() + "RibbonTrailNode"); |
---|
217 | this->ribbonTrailNode_->attachObject(this->ribbonTrail_); |
---|
218 | this->ribbonTrail_->addNode(this->getNode()); |
---|
219 | |
---|
220 | |
---|
221 | this->ribbonTrail_->setTrailLength(this->maxTraillength_); |
---|
222 | this->ribbonTrail_->setMaterialName("Trail/backlighttrail"); |
---|
223 | |
---|
224 | //this->setTimeFactor(Orxonox::getInstance().getTimeFactor()); |
---|
225 | this->setTimeFactor(1.0f); |
---|
226 | |
---|
227 | this->ribbonTrail_->setMaxChainElements(this->maxTrailsegments_); |
---|
228 | this->ribbonTrail_->setTrailLength(this->traillength_ = 2 * this->maxTrailsegments_); |
---|
229 | this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getScale()); |
---|
230 | this->ribbonTrail_->setWidthChange(0, this->width_ * this->getScale() / this->maxLifeTime_ * Backlight::timeFactor_s); |
---|
231 | return true; |
---|
232 | } |
---|
233 | |
---|
234 | Backlight::~Backlight() |
---|
235 | { |
---|
236 | if (this->isInitialized()) |
---|
237 | { |
---|
238 | this->detachObject(this->billboard_.getBillboardSet()); |
---|
239 | GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->getName() + "RibbonTrailNode"); |
---|
240 | GraphicsEngine::getInstance().getLevelSceneManager()->destroyRibbonTrail(this->ribbonTrail_); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | void Backlight::setConfigValues() |
---|
245 | { |
---|
246 | SetConfigValue(maxLifeTime_, 4.0).description("The maximal amount of seconds the trail behind a SpaceShip stays visible"); |
---|
247 | SetConfigValue(trailSegmentLength_, 50).description("The length of one segment of the trail behind a SpaceShip (lower values make it more smooth)"); |
---|
248 | SetConfigValue(width_, 7.0).description("The width of the trail"); |
---|
249 | } |
---|
250 | |
---|
251 | void Backlight::setTimeFactor(float factor) |
---|
252 | { |
---|
253 | Backlight::timeFactor_s = factor; |
---|
254 | float change = Backlight::timeFactor_s / this->maxLifeTime_; |
---|
255 | this->ribbonTrail_->setWidthChange(0, this->width_ * change); |
---|
256 | this->updateColourChange(); |
---|
257 | } |
---|
258 | |
---|
259 | void Backlight::updateColourChange() |
---|
260 | { |
---|
261 | this->ribbonTrail_->setColourChange(0, ColourValue(0, 0, 0, this->maxTraillength_ / this->traillength_ / this->maxLifeTime_ * Backlight::timeFactor_s)); |
---|
262 | } |
---|
263 | |
---|
264 | void Backlight::tick(float dt) |
---|
265 | { |
---|
266 | SUPER(Backlight, tick, dt); |
---|
267 | |
---|
268 | if (this->isActive()) |
---|
269 | { |
---|
270 | if (this->traillength_ < this->maxTraillength_) |
---|
271 | { |
---|
272 | this->traillength_ = min<float>(this->maxTraillength_, this->traillength_ + dt * this->maxTraillength_ / this->maxLifeTime_); |
---|
273 | this->updateColourChange(); |
---|
274 | } |
---|
275 | } |
---|
276 | else |
---|
277 | { |
---|
278 | if (this->traillength_ > 1) |
---|
279 | { |
---|
280 | this->traillength_ = max<float>(1, this->traillength_ - this->brakefactor_ * dt * this->maxTraillength_ / this->maxLifeTime_); |
---|
281 | this->updateColourChange(); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | this->ribbonTrail_->setTrailLength(this->traillength_); |
---|
286 | } |
---|
287 | |
---|
288 | void Backlight::configure(float maxspeed, float brakingtime, float scale) |
---|
289 | { |
---|
290 | this->maxTraillength_ = this->maxLifeTime_ * maxspeed; |
---|
291 | this->maxTrailsegments_ = (size_t)(this->maxTraillength_ / this->trailSegmentLength_); |
---|
292 | |
---|
293 | this->brakefactor_ = this->maxLifeTime_ / brakingtime; |
---|
294 | |
---|
295 | this->scale(scale); |
---|
296 | } |
---|
297 | |
---|
298 | void Backlight::changedVisibility() |
---|
299 | { |
---|
300 | SUPER(Backlight, changedVisibility); |
---|
301 | |
---|
302 | this->billboard_.setVisible(this->isVisible()); |
---|
303 | this->ribbonTrail_->setVisible(this->isVisible()); |
---|
304 | } |
---|
305 | */ |
---|
306 | } |
---|