Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSRoot.cc @ 3363

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

Merged netp6 branch back to the trunk.

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