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