Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/gamestates/GSRoot.cc @ 5855

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

Moved Clock from core to util (used in Scope anyway).

  • Property svn:eol-style set to native
File size: 4.5 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 "GSRoot.h"
30
[5855]31#include "util/Clock.h"
[3196]32#include "core/ConsoleCommand.h"
[2896]33#include "core/Game.h"
34#include "core/GameMode.h"
[3304]35#include "network/NetworkFunction.h"
[1826]36#include "tools/Timer.h"
[5693]37#include "tools/interfaces/TimeFactorListener.h"
38#include "tools/interfaces/Tickable.h"
[1661]39
40namespace orxonox
41{
[3370]42    DeclareGameState(GSRoot, "root", false, false);
[1663]43
[3370]44    GSRoot::GSRoot(const GameStateInfo& info)
45        : GameState(info)
[2662]46        , timeFactor_(1.0f)
47        , bPaused_(false)
48        , timeFactorPauseBackup_(1.0f)
[1661]49    {
[2662]50        this->ccSetTimeFactor_ = 0;
51        this->ccPause_ = 0;
[1661]52    }
53
54    GSRoot::~GSRoot()
55    {
[3304]56        NetworkFunctionBase::destroyAllNetworkFunctions();
[1661]57    }
58
[2896]59    void GSRoot::activate()
[1686]60    {
[2662]61        // reset game speed to normal
[2896]62        this->timeFactor_ = 1.0f;
[2662]63
[5829]64        // time factor console command
65        this->ccSetTimeFactor_ = createConsoleCommand(createFunctor(&GSRoot::setTimeFactor, this), "setTimeFactor");
66        CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
[2662]67
[5829]68        // time factor console command
69        this->ccPause_ = createConsoleCommand(createFunctor(&GSRoot::pause, this), "pause");
70        CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
[1661]71    }
72
[2896]73    void GSRoot::deactivate()
[1661]74    {
[2928]75/*
[2662]76        if (this->ccSetTimeFactor_)
77        {
78            delete this->ccSetTimeFactor_;
79            this->ccSetTimeFactor_ = 0;
80        }
81
82        if (this->ccPause_)
83        {
84            delete this->ccPause_;
85            this->ccPause_ = 0;
86        }
[2928]87*/
[1661]88    }
89
[2896]90    void GSRoot::update(const Clock& time)
[1661]91    {
[2896]92        if (this->getActivity().topState)
93        {
94            // This state can not 'survive' on its own.
95            // Load a user interface therefore
96            Game::getInstance().requestState("ioConsole");
97        }
98
[5831]99        for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; )
[5785]100            (it++)->tick(time);
[1826]101
[2171]102        /*** HACK *** HACK ***/
103        // Call the Tickable objects
[2662]104        float leveldt = time.getDeltaTime();
105        if (leveldt > 1.0f)
106        {
107            // just loaded
108            leveldt = 0.0f;
109        }
[5785]110        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; )
111            (it++)->tick(leveldt * this->timeFactor_);
[2171]112        /*** HACK *** HACK ***/
[1662]113    }
[1674]114
115    /**
[2662]116    @brief
117        Changes the speed of Orxonox
[3370]118    @remark
119        This function is a hack when placed here!
120        Timefactor should be related to the scene (level or so), not the game
[2662]121    */
122    void GSRoot::setTimeFactor(float factor)
123    {
[2896]124        if (GameMode::isMaster())
[2662]125        {
126            if (!this->bPaused_)
127            {
128                TimeFactorListener::timefactor_s = factor;
129
130                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
131                    it->changedTimeFactor(factor, this->timeFactor_);
132
133                this->timeFactor_ = factor;
134            }
135            else
136                this->timeFactorPauseBackup_ = factor;
137        }
138    }
139
140    void GSRoot::pause()
141    {
[2896]142        if (GameMode::isMaster())
[2662]143        {
144            if (!this->bPaused_)
145            {
146                this->timeFactorPauseBackup_ = this->timeFactor_;
147                this->setTimeFactor(0.0f);
148                this->bPaused_ = true;
149            }
150            else
151            {
152                this->bPaused_ = false;
153                this->setTimeFactor(this->timeFactorPauseBackup_);
154            }
155        }
156    }
[1661]157}
Note: See TracBrowser for help on using the repository browser.