Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3366 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
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 "network/NetworkFunction.h"
37#include "tools/Timer.h"
38#include "interfaces/TimeFactorListener.h"
39#include "interfaces/Tickable.h"
40#include "LevelManager.h"
41
42namespace orxonox
43{
44    DeclareGameState(GSRoot, "root", true, false);
45    SetCommandLineSwitch(console).information("Start in console mode (text IO only)");
46    // Shortcuts for easy direct loading
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");
51
52    GSRoot::GSRoot(const GameStateConstrParams& params)
53        : GameState(params)
54        , timeFactor_(1.0f)
55        , bPaused_(false)
56        , timeFactorPauseBackup_(1.0f)
57    {
58        this->ccSetTimeFactor_ = 0;
59        this->ccPause_ = 0;
60    }
61
62    GSRoot::~GSRoot()
63    {
64        NetworkFunctionBase::destroyAllNetworkFunctions();
65    }
66
67    void GSRoot::activate()
68    {
69        // reset game speed to normal
70        this->timeFactor_ = 1.0f;
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        }
87
88        // create the global LevelManager
89        this->levelManager_ = new LevelManager();
90
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        }
120    }
121
122    void GSRoot::deactivate()
123    {
124/*
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        }
136*/
137
138        delete this->levelManager_;
139    }
140
141    void GSRoot::update(const Clock& time)
142    {
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
150        uint64_t timeBeforeTick = time.getRealMicroseconds();
151
152        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
153            it->tick(time);
154
155        /*** HACK *** HACK ***/
156        // Call the Tickable objects
157        float leveldt = time.getDeltaTime();
158        if (leveldt > 1.0f)
159        {
160            // just loaded
161            leveldt = 0.0f;
162        }
163        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
164            it->tick(leveldt * this->timeFactor_);
165        /*** HACK *** HACK ***/
166
167        uint64_t timeAfterTick = time.getRealMicroseconds();
168
169        // Also add our tick time
170        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
171    }
172
173    /**
174    @brief
175        Changes the speed of Orxonox
176    */
177    void GSRoot::setTimeFactor(float factor)
178    {
179        if (GameMode::isMaster())
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    {
197        if (GameMode::isMaster())
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    }
212}
Note: See TracBrowser for help on using the repository browser.