Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gamestates/GSRoot.cc @ 2846

Last change on this file since 2846 was 2846, checked in by rgrieder, 15 years ago

Moving game clock from Core to Game.
Other small fixes.

  • Property svn:eol-style set to native
File size: 5.0 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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "GSRoot.h"
31
32#include "util/Exception.h"
33#include "util/Debug.h"
34#include "core/Clock.h"
35#include "core/Core.h"
36#include "core/CommandLine.h"
37#include "core/ConsoleCommand.h"
38#include "tools/TimeFactorListener.h"
39#include "tools/Timer.h"
40#include "objects/Tickable.h"
41#include "core/Game.h"
42
43namespace orxonox
44{
45    AddGameState(GSRoot, "root");
46    SetCommandLineSwitch(startWithConsole);
47
48    GSRoot::GSRoot(const std::string& name)
49        : GameState(name)
50        , timeFactor_(1.0f)
51        , bPaused_(false)
52        , timeFactorPauseBackup_(1.0f)
53    {
54        this->ccSetTimeFactor_ = 0;
55        this->ccPause_ = 0;
56    }
57
58    GSRoot::~GSRoot()
59    {
60    }
61
62    void GSRoot::activate()
63    {
64        // reset game speed to normal
65        timeFactor_ = 1.0f;
66
67        {
68            // time factor console command
69            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
70            functor->setObject(this);
71            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
72            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
73        }
74
75        {
76            // time factor console command
77            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
78            functor->setObject(this);
79            this->ccPause_ = createConsoleCommand(functor, "pause");
80            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
81        }
82
83        // Determine where to start
84        if (CommandLine::getValue("startWithConsole").getBool())
85        {
86            // Start the game in the console
87            Game::getInstance().requestState("ioConsole");
88        }
89        else
90        {
91            // Start in GUI main menu
92            Game::getInstance().requestState("graphics");
93            Game::getInstance().requestState("mainMenu");
94        }
95
96    }
97
98    void GSRoot::deactivate()
99    {
100        if (this->ccSetTimeFactor_)
101        {
102            delete this->ccSetTimeFactor_;
103            this->ccSetTimeFactor_ = 0;
104        }
105
106        if (this->ccPause_)
107        {
108            delete this->ccPause_;
109            this->ccPause_ = 0;
110        }
111    }
112
113    void GSRoot::update(const Clock& time)
114    {
115        uint64_t timeBeforeTick = time.getRealMicroseconds();
116
117        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
118            it->tick(time);
119
120        /*** HACK *** HACK ***/
121        // Call the Tickable objects
122        float leveldt = time.getDeltaTime();
123        if (leveldt > 1.0f)
124        {
125            // just loaded
126            leveldt = 0.0f;
127        }
128        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
129            it->tick(leveldt * this->timeFactor_);
130        /*** HACK *** HACK ***/
131
132        uint64_t timeAfterTick = time.getRealMicroseconds();
133
134        // Also add our tick time to the list in GSRoot
135        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
136    }
137
138    /**
139    @brief
140        Changes the speed of Orxonox
141    */
142    void GSRoot::setTimeFactor(float factor)
143    {
144        if (Core::isMaster())
145        {
146            if (!this->bPaused_)
147            {
148                TimeFactorListener::timefactor_s = factor;
149
150                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
151                    it->changedTimeFactor(factor, this->timeFactor_);
152
153                this->timeFactor_ = factor;
154            }
155            else
156                this->timeFactorPauseBackup_ = factor;
157        }
158    }
159
160    void GSRoot::pause()
161    {
162        if (Core::isMaster())
163        {
164            if (!this->bPaused_)
165            {
166                this->timeFactorPauseBackup_ = this->timeFactor_;
167                this->setTimeFactor(0.0f);
168                this->bPaused_ = true;
169            }
170            else
171            {
172                this->bPaused_ = false;
173                this->setTimeFactor(this->timeFactorPauseBackup_);
174            }
175        }
176    }
177}
Note: See TracBrowser for help on using the repository browser.