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