Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Exported showsGraphics, etc. to a new class named GameMode in the core.

  • Property svn:eol-style set to native
File size: 5.0 KB
RevLine 
[1661]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
[1764]32#include "util/Exception.h"
33#include "util/Debug.h"
[2844]34#include "core/Clock.h"
[2848]35#include "core/Game.h"
36#include "core/GameMode.h"
[2845]37#include "core/CommandLine.h"
[1661]38#include "core/ConsoleCommand.h"
[2843]39#include "tools/TimeFactorListener.h"
[1826]40#include "tools/Timer.h"
[2171]41#include "objects/Tickable.h"
[1661]42
43namespace orxonox
44{
[2844]45    AddGameState(GSRoot, "root");
[2845]46    SetCommandLineSwitch(startWithConsole);
[2844]47
48    GSRoot::GSRoot(const std::string& name)
49        : GameState(name)
[2662]50        , timeFactor_(1.0f)
51        , bPaused_(false)
52        , timeFactorPauseBackup_(1.0f)
[1661]53    {
[2662]54        this->ccSetTimeFactor_ = 0;
55        this->ccPause_ = 0;
[1661]56    }
57
58    GSRoot::~GSRoot()
59    {
60    }
61
[2844]62    void GSRoot::activate()
[1661]63    {
[2662]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        }
[2845]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
[1661]96    }
97
[2844]98    void GSRoot::deactivate()
[1661]99    {
[2662]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        }
[1661]111    }
112
[2844]113    void GSRoot::update(const Clock& time)
[1661]114    {
[2662]115        uint64_t timeBeforeTick = time.getRealMicroseconds();
116
[1826]117        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
118            it->tick(time);
119
[2171]120        /*** HACK *** HACK ***/
121        // Call the Tickable objects
[2662]122        float leveldt = time.getDeltaTime();
123        if (leveldt > 1.0f)
124        {
125            // just loaded
126            leveldt = 0.0f;
127        }
[2171]128        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
[2662]129            it->tick(leveldt * this->timeFactor_);
[2171]130        /*** HACK *** HACK ***/
131
[2662]132        uint64_t timeAfterTick = time.getRealMicroseconds();
133
[2817]134        // Also add our tick time to the list in GSRoot
135        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
[1662]136    }
[1674]137
138    /**
[2662]139    @brief
140        Changes the speed of Orxonox
141    */
142    void GSRoot::setTimeFactor(float factor)
143    {
[2848]144        if (GameMode::isMaster())
[2662]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    {
[2848]162        if (GameMode::isMaster())
[2662]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    }
[1661]177}
Note: See TracBrowser for help on using the repository browser.