Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/Backlight.cc @ 3222

Last change on this file since 3222 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
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 "Backlight.h"
30
31#include <OgreRibbonTrail.h>
32#include <OgreSceneManager.h>
33#include <OgreSceneNode.h>
34
35#include "util/Exception.h"
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "core/XMLPort.h"
39#include "objects/Scene.h"
40
41namespace orxonox
42{
43    CreateFactory(Backlight);
44
45    Backlight::Backlight(BaseObject* creator) : FadingBillboard(creator)
46    {
47        RegisterObject(Backlight);
48
49        this->ribbonTrail_ = 0;
50        this->ribbonTrailNode_ = 0;
51
52        this->width_ = 0;
53        this->length_ = 1.0f;
54        this->lifetime_ = 0.001f;
55        this->maxelements_ = 1;
56
57        this->tickcount_ = 0;
58
59        if (GameMode::showsGraphics())
60        {
61            if (!this->getScene())
62                ThrowException(AbortLoading, "Can't create Backlight, no scene given.");
63            if (!this->getScene()->getSceneManager())
64                ThrowException(AbortLoading, "Can't create Backlight, no scene manager given.");
65            if (!this->getScene()->getRootSceneNode())
66                ThrowException(AbortLoading, "Can't create Backlight, no root scene node given.");
67
68            this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName());
69
70            this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
71            this->ribbonTrailNode_->attachObject(this->ribbonTrail_);
72
73            this->ribbonTrail_->setMaxChainElements(this->maxelements_);
74            this->ribbonTrail_->setTrailLength(this->length_);
75            this->ribbonTrail_->setInitialWidth(0, 0);
76        }
77
78        this->registerVariables();
79    }
80
81    Backlight::~Backlight()
82    {
83        if (this->isInitialized())
84        {
85            if (this->ribbonTrail_)
86            {
87                if (this->ribbonTrailNode_)
88                {
89                    this->ribbonTrailNode_->detachObject(this->ribbonTrail_);
90                    this->getScene()->getSceneManager()->destroySceneNode(this->ribbonTrailNode_->getName());
91                }
92                this->getScene()->getSceneManager()->destroyRibbonTrail(this->ribbonTrail_);
93            }
94        }
95    }
96
97    void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode)
98    {
99        SUPER(Backlight, XMLPort, xmlelement, mode);
100
101        XMLPortParam(Backlight, "length",        setLength,        getLength,        xmlelement, mode).defaultValues(100.0f);
102        XMLPortParam(Backlight, "width",         setWidth,         getWidth,         xmlelement, mode).defaultValues(1.0f);
103        XMLPortParam(Backlight, "elements",      setMaxElements,   getMaxElements,   xmlelement, mode).defaultValues(10);
104        XMLPortParam(Backlight, "lifetime",      setLifetime,      getLifetime,      xmlelement, mode).defaultValues(1.0f);
105        XMLPortParam(Backlight, "trailmaterial", setTrailMaterial, getTrailMaterial, xmlelement, mode);
106    }
107
108    void Backlight::registerVariables()
109    {
110        registerVariable(this->width_,         variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_width));
111        registerVariable(this->lifetime_,      variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_lifetime));
112        registerVariable(this->length_,        variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_length));
113        registerVariable(this->maxelements_,   variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_maxelements));
114        registerVariable(this->trailmaterial_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_trailmaterial));
115    }
116
117    void Backlight::changedColour()
118    {
119        FadingBillboard::changedColour();
120
121        if (this->ribbonTrail_ && this->tickcount_ >= 2)
122            this->ribbonTrail_->setInitialColour(0, this->getFadedColour());
123    }
124
125    void Backlight::update_width()
126    {
127        if (this->ribbonTrail_ && this->tickcount_ >= 2)
128            this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getWorldScale());
129        this->update_lifetime();
130    }
131
132    void Backlight::update_lifetime()
133    {
134        if (this->ribbonTrail_ && this->tickcount_ >= 2)
135        {
136            this->ribbonTrail_->setWidthChange(0, this->width_ * this->getWorldScale() / this->lifetime_ * this->getTimeFactor());
137            this->ribbonTrail_->setColourChange(0, 0, 0, 0, 1.0f / this->lifetime_ * this->getTimeFactor());
138        }
139    }
140
141    void Backlight::update_length()
142    {
143        if (this->ribbonTrail_ && this->tickcount_ >= 2)
144            this->ribbonTrail_->setTrailLength(this->length_ * this->getWorldScale());
145    }
146
147    void Backlight::update_maxelements()
148    {
149        if (this->ribbonTrail_ && this->tickcount_ >= 2)
150            this->ribbonTrail_->setMaxChainElements(this->maxelements_);
151    }
152
153    void Backlight::update_trailmaterial()
154    {
155        if (this->ribbonTrail_ && this->tickcount_ >= 2)
156            this->ribbonTrail_->setMaterialName(this->trailmaterial_);
157    }
158
159    void Backlight::changedVisibility()
160    {
161        SUPER(Backlight, changedVisibility);
162
163        if (this->ribbonTrail_)
164            this->ribbonTrail_->setVisible(this->isVisible());
165    }
166
167    void Backlight::startturnonoff()
168    {
169        FadingBillboard::startturnonoff();
170
171        if (this->ribbonTrail_ && this->isActive() && this->isVisible())
172            this->ribbonTrail_->setVisible(true);
173    }
174
175    void Backlight::stopturnonoff()
176    {
177        this->postprocessingtime_ = max(0.0f, this->lifetime_ - this->turnofftime_);
178
179        FadingBillboard::stopturnonoff();
180
181        if (this->ribbonTrail_)
182            this->ribbonTrail_->setInitialColour(0, this->getFadedColour());
183    }
184
185    void Backlight::poststopturnonoff()
186    {
187        FadingBillboard::poststopturnonoff();
188
189        if (this->ribbonTrail_)
190            this->ribbonTrail_->setVisible(false);
191    }
192
193    void Backlight::changedScale()
194    {
195        SUPER(Backlight, changedScale);
196
197        this->update_width();
198        this->update_length();
199    }
200
201    void Backlight::tick(float dt)
202    {
203        if (this->tickcount_ < 2)
204        {
205            ++this->tickcount_;
206            if (this->tickcount_ == 2)
207            {
208                this->changedColour();
209                this->update_width();
210                this->update_lifetime();
211                this->update_length();
212                this->update_maxelements();
213                this->update_trailmaterial();
214                if (this->ribbonTrail_)
215                    this->ribbonTrail_->addNode(this->node_);
216            }
217        }
218
219        SUPER(Backlight, tick, dt);
220
221        if (this->ribbonTrail_ && this->changedirection_ != 0)
222        {
223            // we use alpha_blend, only adjust alpha
224            const ColourValue& colour = this->getColour();
225            this->ribbonTrail_->setInitialColour(0, colour.r, colour.g, colour.b, this->getFadedColour().a);
226        }
227    }
228
229    void Backlight::changedTimeFactor(float factor_new, float factor_old)
230    {
231        this->update_lifetime();
232    }
233}
Note: See TracBrowser for help on using the repository browser.