[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 "Backlight.h" |
---|
| 30 | |
---|
| 31 | #include <OgreRibbonTrail.h> |
---|
[1755] | 32 | #include <OgreSceneManager.h> |
---|
[3196] | 33 | #include <OgreSceneNode.h> |
---|
[1608] | 34 | |
---|
[3196] | 35 | #include "util/Exception.h" |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
[2896] | 37 | #include "core/GameMode.h" |
---|
[2662] | 38 | #include "core/XMLPort.h" |
---|
| 39 | #include "objects/Scene.h" |
---|
[1608] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | CreateFactory(Backlight); |
---|
| 44 | |
---|
[2662] | 45 | Backlight::Backlight(BaseObject* creator) : FadingBillboard(creator) |
---|
[1608] | 46 | { |
---|
| 47 | RegisterObject(Backlight); |
---|
| 48 | |
---|
[2662] | 49 | this->ribbonTrail_ = 0; |
---|
| 50 | this->ribbonTrailNode_ = 0; |
---|
[1608] | 51 | |
---|
[2662] | 52 | this->width_ = 0; |
---|
| 53 | this->length_ = 1.0f; |
---|
| 54 | this->lifetime_ = 0.001f; |
---|
| 55 | this->maxelements_ = 1; |
---|
[1608] | 56 | |
---|
[2662] | 57 | this->tickcount_ = 0; |
---|
[1608] | 58 | |
---|
[2896] | 59 | if (GameMode::showsGraphics()) |
---|
[2662] | 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."); |
---|
[1608] | 67 | |
---|
[2662] | 68 | this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName()); |
---|
[1608] | 69 | |
---|
[2662] | 70 | this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
| 71 | this->ribbonTrailNode_->attachObject(this->ribbonTrail_); |
---|
[1608] | 72 | |
---|
[2662] | 73 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); |
---|
| 74 | this->ribbonTrail_->setTrailLength(this->length_); |
---|
| 75 | this->ribbonTrail_->setInitialWidth(0, 0); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | this->registerVariables(); |
---|
[1608] | 79 | } |
---|
| 80 | |
---|
| 81 | Backlight::~Backlight() |
---|
| 82 | { |
---|
| 83 | if (this->isInitialized()) |
---|
| 84 | { |
---|
[2662] | 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 | } |
---|
[1608] | 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[2662] | 97 | void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1608] | 98 | { |
---|
[2662] | 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); |
---|
[1608] | 106 | } |
---|
| 107 | |
---|
[2662] | 108 | void Backlight::registerVariables() |
---|
[1608] | 109 | { |
---|
[2662] | 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)); |
---|
[1608] | 115 | } |
---|
| 116 | |
---|
[2662] | 117 | void Backlight::changedColour() |
---|
[1608] | 118 | { |
---|
[2662] | 119 | FadingBillboard::changedColour(); |
---|
| 120 | |
---|
| 121 | if (this->ribbonTrail_ && this->tickcount_ >= 2) |
---|
| 122 | this->ribbonTrail_->setInitialColour(0, this->getFadedColour()); |
---|
[1608] | 123 | } |
---|
[2662] | 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(); |
---|
[1907] | 130 | } |
---|
[1608] | 131 | |
---|
[2662] | 132 | void Backlight::update_lifetime() |
---|
[1608] | 133 | { |
---|
[2662] | 134 | if (this->ribbonTrail_ && this->tickcount_ >= 2) |
---|
[1608] | 135 | { |
---|
[2662] | 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()); |
---|
[1608] | 138 | } |
---|
[2662] | 139 | } |
---|
[1608] | 140 | |
---|
[2662] | 141 | void Backlight::update_length() |
---|
| 142 | { |
---|
| 143 | if (this->ribbonTrail_ && this->tickcount_ >= 2) |
---|
| 144 | this->ribbonTrail_->setTrailLength(this->length_ * this->getWorldScale()); |
---|
[1608] | 145 | } |
---|
| 146 | |
---|
[2662] | 147 | void Backlight::update_maxelements() |
---|
[1608] | 148 | { |
---|
[2662] | 149 | if (this->ribbonTrail_ && this->tickcount_ >= 2) |
---|
| 150 | this->ribbonTrail_->setMaxChainElements(this->maxelements_); |
---|
[1608] | 151 | } |
---|
| 152 | |
---|
[2662] | 153 | void Backlight::update_trailmaterial() |
---|
[1608] | 154 | { |
---|
[2662] | 155 | if (this->ribbonTrail_ && this->tickcount_ >= 2) |
---|
| 156 | this->ribbonTrail_->setMaterialName(this->trailmaterial_); |
---|
[1608] | 157 | } |
---|
| 158 | |
---|
| 159 | void Backlight::changedVisibility() |
---|
| 160 | { |
---|
[1747] | 161 | SUPER(Backlight, changedVisibility); |
---|
[1608] | 162 | |
---|
[2662] | 163 | if (this->ribbonTrail_) |
---|
| 164 | this->ribbonTrail_->setVisible(this->isVisible()); |
---|
[1608] | 165 | } |
---|
[2662] | 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 | |
---|
[2809] | 219 | SUPER(Backlight, tick, dt); |
---|
[2662] | 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 | } |
---|
[1608] | 233 | } |
---|