Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/MouseAPI_FS19/src/modules/orxokart/TimeHUD.cc @ 12272

Last change on this file since 12272 was 12128, checked in by ottka, 6 years ago

Make two levels and more

File size: 2.9 KB
Line 
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 <iostream>
36#include <fstream>
37#include "Highscore.h"
38
39#include "core/CoreIncludes.h"
40#include "util/Convert.h"
41#include "OrxoKart.h"
42
43namespace orxonox
44{
45    RegisterClass(TimeHUD);
46
47    TimeHUD::TimeHUD(Context* context) : OverlayText(context)
48    {
49        RegisterObject(TimeHUD);
50
51        this->time_ = 0.0f;
52        this->running_ = false;
53        this->orxokartGame_ = nullptr;
54        setRunning(true);
55    }
56
57    inline std::string getTimeString(float time) {
58        std::ostringstream ss;
59
60        int h = ((int) (time * 100.0f)) % 100;
61        int s = ((int) time) % 60;
62        int m = (int) (time / 60.0f);
63
64        ss << m << ":";
65
66        if(s < 10) {
67            ss << 0;
68        }
69        ss << s << ":";
70
71        if(h < 10) {
72            ss << 0;
73        }
74        ss << h << "s";
75        return ss.str();
76    }
77
78    void TimeHUD::tick(float dt)
79    {
80        SUPER(TimeHUD, tick, dt);
81
82        if(this->running_) {
83            this->time_ += dt;
84        }
85        if (this->orxokartGame_)
86        {
87            this->setCaption(getTimeString(this->time_));
88            if (this->orxokartGame_->getRaceFinished()) {
89                setRunning(false);
90                if (!score_set) {
91                    std::ofstream highscoreFile;
92                    highscoreFile.open("orxokart_highscores.txt", std::ios::app);
93                    highscoreFile << "Name: " << getTimeString(this->time_) << std::endl;
94                    highscoreFile.close();
95                    score_set = true;
96                }
97            }
98        }
99       
100    }
101
102    void TimeHUD::changedOwner()
103    {
104        SUPER(TimeHUD, changedOwner);
105
106        if (this->getOwner() && this->getOwner()->getGametype())
107        {
108            this->orxokartGame_ = orxonox_cast<OrxoKart*>(this->getOwner()->getGametype());
109        }
110        else
111        {
112            this->orxokartGame_ = nullptr;
113        }
114    }
115
116    void TimeHUD::reset()
117    {
118        this->time_ = 0;
119    }
120}
Note: See TracBrowser for help on using the repository browser.