Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed GameState template and renamed GameStateBase to GameState.
Moved statistics stuff (fps and tick time) to Game and removed the remaining hacks in GSGraphics and GSRoot.

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