| 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 | *      Johannes Ritz | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file CountDown.cc | 
|---|
| 31 | @brief Countdown HUD element, counting down from CountDown::counter_ to zero. | 
|---|
| 32 |  | 
|---|
| 33 | In use it would like this: | 
|---|
| 34 | @code | 
|---|
| 35 | <OverlayGroup name="spacefightHUD" scale = "1, 1"> | 
|---|
| 36 | <CountDown | 
|---|
| 37 | position    = "0.49, 0.05" | 
|---|
| 38 | pickpoint   = "0.0, 0.0" | 
|---|
| 39 | font        = "ShareTechMono" | 
|---|
| 40 | textsize    = 0.06 | 
|---|
| 41 | colour      = "1.0, 1.0, 1.0, 1.0" | 
|---|
| 42 | align       = "right" | 
|---|
| 43 | counter     = "10.0" | 
|---|
| 44 | speedfactor = "1.0" | 
|---|
| 45 | active      = "false" | 
|---|
| 46 | > | 
|---|
| 47 | <events> | 
|---|
| 48 | <activity> | 
|---|
| 49 | <EventListener event="startcounting" /> | 
|---|
| 50 | </activity> | 
|---|
| 51 | </events> | 
|---|
| 52 | </CountDown> | 
|---|
| 53 | </OverlayGroup> | 
|---|
| 54 | @endcode | 
|---|
| 55 | The counter is triggered by an event called "startcounting" and counts from 10 to 0. | 
|---|
| 56 | */ | 
|---|
| 57 |  | 
|---|
| 58 | #include "CountDown.h" | 
|---|
| 59 |  | 
|---|
| 60 | #include "core/CoreIncludes.h" | 
|---|
| 61 | #include "core/XMLPort.h" | 
|---|
| 62 | #include "infos/PlayerInfo.h" | 
|---|
| 63 | #include "util/Convert.h" | 
|---|
| 64 |  | 
|---|
| 65 | namespace orxonox | 
|---|
| 66 | { | 
|---|
| 67 | RegisterClass(CountDown); | 
|---|
| 68 |  | 
|---|
| 69 | CountDown::CountDown(Context* context) : OverlayText(context) | 
|---|
| 70 | { | 
|---|
| 71 | RegisterObject(CountDown); | 
|---|
| 72 |  | 
|---|
| 73 | this->owner_ = nullptr; | 
|---|
| 74 | this->hasStopped_ = false; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | CountDown::~CountDown() | 
|---|
| 78 | { | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | void CountDown::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 82 | { | 
|---|
| 83 | SUPER(CountDown, XMLPort, xmlelement, mode); | 
|---|
| 84 | XMLPortParam(CountDown, "counter", setCounter, getCounter, xmlelement, mode).defaultValues(10); | 
|---|
| 85 | XMLPortParam(CountDown, "speedfactor", setSpeed, getSpeed, xmlelement, mode).defaultValues(1); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void CountDown::tick(float dt) | 
|---|
| 89 | { | 
|---|
| 90 | SUPER(CountDown, tick, dt); | 
|---|
| 91 | if (this->isActive() && !this->hasStopped_) | 
|---|
| 92 | { | 
|---|
| 93 | if (this->counter_ <= 0) | 
|---|
| 94 | { | 
|---|
| 95 | this->counter_ = 0; | 
|---|
| 96 | this->hasStopped_ = true; | 
|---|
| 97 | this->setCaption(""); | 
|---|
| 98 | } | 
|---|
| 99 | else | 
|---|
| 100 | { | 
|---|
| 101 | this->counter_ -= dt*speed_; | 
|---|
| 102 | this->setCaption(multi_cast<std::string>((int)this->counter_)); //TODO: evtl. initialize with +0.5f | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void CountDown::changedOwner() | 
|---|
| 108 | { | 
|---|
| 109 | SUPER(CountDown, changedOwner); | 
|---|
| 110 |  | 
|---|
| 111 | this->owner_ = orxonox_cast<PlayerInfo*>(this->getOwner()); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|