Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/orxonox/gamestates/GSRoot.cc @ 3607

Last change on this file since 3607 was 3255, checked in by rgrieder, 16 years ago

#298: Improved usage dialogue creation and the information itself.

  • Property svn:eol-style set to native
File size: 6.3 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/CommandLine.h"
33#include "core/ConsoleCommand.h"
34#include "core/Game.h"
35#include "core/GameMode.h"
36#include "tools/Timer.h"
37#include "interfaces/TimeFactorListener.h"
38#include "interfaces/Tickable.h"
39#include "LevelManager.h"
40
41namespace orxonox
42{
43    DeclareGameState(GSRoot, "root", true, false);
44    SetCommandLineSwitch(console).information("Start in console mode (text IO only)");
45    // Shortcuts for easy direct loading
46    SetCommandLineSwitch(server).information("Start in server mode");
47    SetCommandLineSwitch(client).information("Start in client mode");
48    SetCommandLineSwitch(dedicated).information("Start in dedicated server mode");
49    SetCommandLineSwitch(standalone).information("Start in standalone mode");
50
51    GSRoot::GSRoot(const GameStateConstrParams& params)
52        : GameState(params)
53        , timeFactor_(1.0f)
54        , bPaused_(false)
55        , timeFactorPauseBackup_(1.0f)
56    {
57        this->ccSetTimeFactor_ = 0;
58        this->ccPause_ = 0;
59    }
60
61    GSRoot::~GSRoot()
62    {
63    }
64
65    void GSRoot::activate()
66    {
67        // reset game speed to normal
68        this->timeFactor_ = 1.0f;
69
70        {
71            // time factor console command
72            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
73            functor->setObject(this);
74            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
75            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
76        }
77
78        {
79            // time factor console command
80            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
81            functor->setObject(this);
82            this->ccPause_ = createConsoleCommand(functor, "pause");
83            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
84        }
85
86        // create the global LevelManager
87        this->levelManager_ = new LevelManager();
88
89        // Load level directly?
90        bool loadLevel = false;
91        if (CommandLine::getValue("standalone").getBool())
92        {
93            Game::getInstance().requestStates("graphics, standalone, level");
94            loadLevel = true;
95        }
96        if (CommandLine::getValue("server").getBool())
97        {
98            Game::getInstance().requestStates("graphics, server, level");
99            loadLevel = true;
100        }
101        if (CommandLine::getValue("client").getBool())
102        {
103            Game::getInstance().requestStates("graphics, client, level");
104            loadLevel = true;
105        }
106        if (CommandLine::getValue("dedicated").getBool())
107        {
108            Game::getInstance().requestStates("dedicated, level");
109            loadLevel = true;
110        }
111       
112        // Determine where to start otherwise
113        if (!loadLevel && !CommandLine::getValue("console").getBool())
114        {
115            // Also load graphics
116            Game::getInstance().requestState("graphics");
117        }
118    }
119
120    void GSRoot::deactivate()
121    {
122/*
123        if (this->ccSetTimeFactor_)
124        {
125            delete this->ccSetTimeFactor_;
126            this->ccSetTimeFactor_ = 0;
127        }
128
129        if (this->ccPause_)
130        {
131            delete this->ccPause_;
132            this->ccPause_ = 0;
133        }
134*/
135
136        delete this->levelManager_;
137    }
138
139    void GSRoot::update(const Clock& time)
140    {
141        if (this->getActivity().topState)
142        {
143            // This state can not 'survive' on its own.
144            // Load a user interface therefore
145            Game::getInstance().requestState("ioConsole");
146        }
147
148        uint64_t timeBeforeTick = time.getRealMicroseconds();
149
150        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
151            it->tick(time);
152
153        /*** HACK *** HACK ***/
154        // Call the Tickable objects
155        float leveldt = time.getDeltaTime();
156        if (leveldt > 1.0f)
157        {
158            // just loaded
159            leveldt = 0.0f;
160        }
161        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
162            it->tick(leveldt * this->timeFactor_);
163        /*** HACK *** HACK ***/
164
165        uint64_t timeAfterTick = time.getRealMicroseconds();
166
167        // Also add our tick time
168        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
169    }
170
171    /**
172    @brief
173        Changes the speed of Orxonox
174    */
175    void GSRoot::setTimeFactor(float factor)
176    {
177        if (GameMode::isMaster())
178        {
179            if (!this->bPaused_)
180            {
181                TimeFactorListener::timefactor_s = factor;
182
183                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
184                    it->changedTimeFactor(factor, this->timeFactor_);
185
186                this->timeFactor_ = factor;
187            }
188            else
189                this->timeFactorPauseBackup_ = factor;
190        }
191    }
192
193    void GSRoot::pause()
194    {
195        if (GameMode::isMaster())
196        {
197            if (!this->bPaused_)
198            {
199                this->timeFactorPauseBackup_ = this->timeFactor_;
200                this->setTimeFactor(0.0f);
201                this->bPaused_ = true;
202            }
203            else
204            {
205                this->bPaused_ = false;
206                this->setTimeFactor(this->timeFactorPauseBackup_);
207            }
208        }
209    }
210}
Note: See TracBrowser for help on using the repository browser.