Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2083 was 1907, checked in by scheusso, 16 years ago

merged network branch back to trunk

  • Property svn:eol-style set to native
File size: 5.8 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 <OgreBillboard.h>
33#include <OgreRibbonTrail.h>
34#include <OgreSceneManager.h>
35
36#include "core/CoreIncludes.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/Executor.h"
39#include "util/Math.h"
40#include "GraphicsEngine.h"
41
42namespace orxonox
43{
44    CreateFactory(Backlight);
45
46    float Backlight::timeFactor_s = 1.0;
47
48    Backlight::Backlight(float maxspeed, float brakingtime, float scale)
49    {
50        RegisterObject(Backlight);
51
52        this->setConfigValues();
53        this->traillength_ = 1;
54
55        this->configure(maxspeed, brakingtime, scale);
56    }
57   
58    bool Backlight::create(){
59      if(!WorldEntity::create())
60        return false;
61     
62      this->getNode()->setInheritScale(false);
63
64      this->billboard_.setBillboardSet("Flares/backlightflare");
65      this->attachObject(this->billboard_.getBillboardSet());
66
67      this->ribbonTrail_ = GraphicsEngine::getInstance().getLevelSceneManager()->createRibbonTrail(this->getName() + "RibbonTrail");
68      this->ribbonTrailNode_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName() + "RibbonTrailNode");
69      this->ribbonTrailNode_->attachObject(this->ribbonTrail_);
70      this->ribbonTrail_->addNode(this->getNode());
71
72
73      this->ribbonTrail_->setTrailLength(this->maxTraillength_);
74      this->ribbonTrail_->setMaterialName("Trail/backlighttrail");
75
76        //this->setTimeFactor(Orxonox::getInstance().getTimeFactor());
77      this->setTimeFactor(1.0f);
78     
79      this->ribbonTrail_->setMaxChainElements(this->maxTrailsegments_);
80      this->ribbonTrail_->setTrailLength(this->traillength_ = 2 * this->maxTrailsegments_);
81      this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getScale());
82      this->ribbonTrail_->setWidthChange(0, this->width_ * this->getScale() / this->maxLifeTime_ * Backlight::timeFactor_s);
83      return true;
84    }
85
86    Backlight::~Backlight()
87    {
88        if (this->isInitialized())
89        {
90            this->detachObject(this->billboard_.getBillboardSet());
91            GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->getName() + "RibbonTrailNode");
92            GraphicsEngine::getInstance().getLevelSceneManager()->destroyRibbonTrail(this->ribbonTrail_);
93        }
94    }
95
96    void Backlight::setConfigValues()
97    {
98        SetConfigValue(maxLifeTime_, 4.0).description("The maximal amount of seconds the trail behind a SpaceShip stays visible");
99        SetConfigValue(trailSegmentLength_, 50).description("The length of one segment of the trail behind a SpaceShip (lower values make it more smooth)");
100        SetConfigValue(width_, 7.0).description("The width of the trail");
101    }
102
103    void Backlight::setTimeFactor(float factor)
104    {
105        Backlight::timeFactor_s = factor;
106        float change = Backlight::timeFactor_s / this->maxLifeTime_;
107        this->ribbonTrail_->setWidthChange(0, this->width_ * change);
108        this->updateColourChange();
109    }
110
111    void Backlight::updateColourChange()
112    {
113        this->ribbonTrail_->setColourChange(0, ColourValue(0, 0, 0, this->maxTraillength_ / this->traillength_ / this->maxLifeTime_ * Backlight::timeFactor_s));
114    }
115   
116   
117    void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode){
118      SUPER(Backlight, XMLPort, xmlelement, mode);
119     
120      Backlight::create();
121    }
122
123    void Backlight::tick(float dt)
124    {
125        SUPER(Backlight, tick, dt);
126
127        if (this->isActive())
128        {
129            if (this->traillength_ < this->maxTraillength_)
130            {
131                this->traillength_ = min<float>(this->maxTraillength_, this->traillength_ + dt * this->maxTraillength_ / this->maxLifeTime_);
132                this->updateColourChange();
133            }
134        }
135        else
136        {
137            if (this->traillength_ > 1)
138            {
139                this->traillength_ = max<float>(1, this->traillength_ - this->brakefactor_ * dt * this->maxTraillength_ / this->maxLifeTime_);
140                this->updateColourChange();
141            }
142        }
143
144        this->ribbonTrail_->setTrailLength(this->traillength_);
145    }
146
147    void Backlight::setColour(const ColourValue& colour)
148    {
149        this->billboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
150        this->ribbonTrail_->setInitialColour(0, ColourValue(colour.r / 4 + 0.75, colour.g / 4 + 0.75, colour.b / 4 + 0.75));
151    }
152
153    void Backlight::configure(float maxspeed, float brakingtime, float scale)
154    {
155        this->maxTraillength_ = this->maxLifeTime_ * maxspeed;
156        this->maxTrailsegments_ = (size_t)(this->maxTraillength_ / this->trailSegmentLength_);
157
158        this->brakefactor_ = this->maxLifeTime_ / brakingtime;
159
160        this->scale(scale);
161    }
162
163    void Backlight::changedVisibility()
164    {
165        SUPER(Backlight, changedVisibility);
166
167        this->billboard_.setVisible(this->isVisible());
168        this->ribbonTrail_->setVisible(this->isVisible());
169    }
170}
Note: See TracBrowser for help on using the repository browser.