Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Exported TimeFactorListener to separate file in the tools folder.

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