| 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 | * Cyrill Burgener |
|---|
| 24 | * |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | @file TimeHUD.cc |
|---|
| 29 | @brief Implementation of the TimeHUD |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | #include "TimeHUD.h" |
|---|
| 34 | |
|---|
| 35 | #include "core/CoreIncludes.h" |
|---|
| 36 | #include "core/XMLPort.h" |
|---|
| 37 | #include "util/Convert.h" |
|---|
| 38 | #include "Hover.h" |
|---|
| 39 | |
|---|
| 40 | namespace orxonox |
|---|
| 41 | { |
|---|
| 42 | RegisterClass(TimeHUD); |
|---|
| 43 | |
|---|
| 44 | TimeHUD::TimeHUD(Context* context) : OverlayText(context) |
|---|
| 45 | { |
|---|
| 46 | RegisterObject(TimeHUD); |
|---|
| 47 | |
|---|
| 48 | this->_time = 0.0f; |
|---|
| 49 | this->_running = false; |
|---|
| 50 | this->hoverGame = 0; |
|---|
| 51 | setRunning(true); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void TimeHUD::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 55 | { |
|---|
| 56 | SUPER(TimeHUD, XMLPort, xmlelement, mode); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | inline std::string getTimeString(float time) { |
|---|
| 60 | std::ostringstream ss; |
|---|
| 61 | |
|---|
| 62 | int h = ((int) (time * 100.0f)) % 100; |
|---|
| 63 | int s = ((int) time) % 60; |
|---|
| 64 | int m = (int) (time / 60.0f); |
|---|
| 65 | |
|---|
| 66 | ss << m << ":"; |
|---|
| 67 | |
|---|
| 68 | if(s < 10) { |
|---|
| 69 | ss << 0; |
|---|
| 70 | } |
|---|
| 71 | ss << s << ":"; |
|---|
| 72 | |
|---|
| 73 | if(h < 10) { |
|---|
| 74 | ss << 0; |
|---|
| 75 | } |
|---|
| 76 | ss << h << "s"; |
|---|
| 77 | return ss.str(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | void TimeHUD::tick(float dt) |
|---|
| 81 | { |
|---|
| 82 | SUPER(TimeHUD, tick, dt); |
|---|
| 83 | |
|---|
| 84 | if(this->_running) { |
|---|
| 85 | this->_time += dt; |
|---|
| 86 | } |
|---|
| 87 | if (this->hoverGame) |
|---|
| 88 | { |
|---|
| 89 | this->setCaption(getTimeString(this->_time)); |
|---|
| 90 | } |
|---|
| 91 | if(this->hoverGame->getFlags() == 0) |
|---|
| 92 | setRunning(false); |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void TimeHUD::changedOwner() |
|---|
| 97 | { |
|---|
| 98 | SUPER(TimeHUD, changedOwner); |
|---|
| 99 | |
|---|
| 100 | if (this->getOwner() && this->getOwner()->getGametype()) |
|---|
| 101 | { |
|---|
| 102 | this->hoverGame = orxonox_cast<Hover*>(this->getOwner()->getGametype()); |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | this->hoverGame = 0; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | @brief |
|---|
| 112 | sets if the clock is running |
|---|
| 113 | @param running |
|---|
| 114 | */ |
|---|
| 115 | void TimeHUD::setRunning(bool running) |
|---|
| 116 | { |
|---|
| 117 | this->_running = running; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | @brief |
|---|
| 122 | returns if the clock is running |
|---|
| 123 | @returns running |
|---|
| 124 | */ |
|---|
| 125 | bool TimeHUD::isRunning() { |
|---|
| 126 | return this->_running; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void TimeHUD::reset() |
|---|
| 130 | { |
|---|
| 131 | this->_time = 0; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|