Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Cleanup in the GameStates (also moved debug overlay to the GraphicsManager).

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