Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/Backlight.cc @ 2212

Last change on this file since 2212 was 2212, checked in by landauf, 15 years ago

maybe fixed backlight crash

File size: 7.4 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 "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/Executor.h"
38#include "core/XMLPort.h"
39#include "objects/Scene.h"
40
41namespace orxonox
42{
43    CreateFactory(Backlight);
44
45    Backlight::Backlight(BaseObject* creator) : Billboard(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->turnofftime_ = 0.5f;
56        this->bTurningOff_ = false;
57        this->maxelements_ = 1;
58
59        this->tickcount_ = 0;
60
61        if (Core::showsGraphics())
62        {
63            assert(this->getScene());
64            assert(this->getScene()->getSceneManager());
65            assert(this->getScene()->getRootSceneNode());
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, "turnofftime",   setTurnOffTime,   getTurnOffTime,   xmlelement, mode).defaultValues(0.5f);
105        XMLPortParam(Backlight, "trailmaterial", setTrailMaterial, getTrailMaterial, xmlelement, mode);
106    }
107
108    void Backlight::registerVariables()
109    {
110        REGISTERDATA  (this->width_,         direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_width));
111        REGISTERDATA  (this->lifetime_,      direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_lifetime));
112        REGISTERDATA  (this->length_,        direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_length));
113        REGISTERDATA  (this->maxelements_,   direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_maxelements));
114        REGISTERSTRING(this->trailmaterial_, direction::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_trailmaterial));
115    }
116
117    void Backlight::changedColour()
118    {
119        Billboard::changedColour();
120
121        if (this->ribbonTrail_ && this->isActive() && this->tickcount_ >= 2)
122            this->ribbonTrail_->setInitialColour(0, this->getColour());
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_/* * Backlight::timeFactor_s*/);
137            this->ribbonTrail_->setColourChange(0, 0, 0, 0, 1.0f / this->lifetime_/* * Backlight::timeFactor_s*/);
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::changedActivity()
168    {
169        SUPER(Backlight, changedActivity);
170
171        if (this->ribbonTrail_)
172        {
173            if (this->isActive())
174                this->ribbonTrail_->setInitialColour(0, this->getColour());
175            else
176            {
177                this->bTurningOff_ = true;
178                this->turnofftimer_.setTimer(this->turnofftime_, false, this, createExecutor(createFunctor(&Backlight::stopturnoff)));
179            }
180        }
181    }
182
183    void Backlight::changedScale()
184    {
185        SUPER(Backlight, changedScale);
186
187        this->update_width();
188        this->update_length();
189    }
190
191    void Backlight::stopturnoff()
192    {
193        this->bTurningOff_ = false;
194    }
195
196    void Backlight::tick(float dt)
197    {
198        if (this->tickcount_ < 2)
199        {
200            ++this->tickcount_;
201            if (this->tickcount_ == 2)
202            {
203                this->changedColour();
204                this->update_width();
205                this->update_lifetime();
206                this->update_length();
207                this->update_maxelements();
208                this->update_trailmaterial();
209                if (this->ribbonTrail_)
210                    this->ribbonTrail_->addNode(this->getNode());
211            }
212        }
213
214        if (this->bTurningOff_ && this->ribbonTrail_)
215        {
216            this->ribbonTrail_->setInitialColour(0, this->ribbonTrail_->getInitialColour(0) - this->getColour() / this->turnofftime_ * dt);
217        }
218    }
219}
Note: See TracBrowser for help on using the repository browser.