Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/Backlight.cc @ 3149

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

Extracted OrxAssert from Exception.h to OrxAssert.h since it doesn't really have anything to do with exceptions.

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