Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5842 was 5831, checked in by landauf, 15 years ago

Realized Timer doesn't have to be a template, hence merged TimerBase, Timer and StaticTimer.
Removed the object pointer from Timer for memberfunctions, use createFunctor(f, object) instead.

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