Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/gamestates/GSRoot.cc @ 4073

Last change on this file since 4073 was 3256, checked in by scheusso, 15 years ago

added NetworkFunctionBase::destroyAllNetworkFunctions to cleanup premain created stuff

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