Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/gamestates/GSRoot.cc @ 3149

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

Extracted OrxAssert from Exception.h to OrxAssert.h since it doesn't really have anything to do with exceptions.

  • Property svn:eol-style set to native
File size: 6.0 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 "util/Debug.h"
32#include "core/Clock.h"
33#include "core/Game.h"
34#include "core/GameMode.h"
35#include "core/CommandLine.h"
36#include "core/ConsoleCommand.h"
37#include "tools/TimeFactorListener.h"
38#include "tools/Timer.h"
39#include "objects/Tickable.h"
40
41namespace orxonox
42{
43    AddGameState(GSRoot, "root", false);
44    SetCommandLineSwitch(console);
45    // Shortcuts for easy direct loading
46    SetCommandLineSwitch(server);
47    SetCommandLineSwitch(client);
48    SetCommandLineSwitch(dedicated);
49    SetCommandLineSwitch(standalone);
50
51    GSRoot::GSRoot(const std::string& name, bool countTickTime)
52        : GameState(name, countTickTime)
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        // Load level directly?
87        bool loadLevel = false;
88        if (CommandLine::getValue("standalone").getBool())
89        {
90            Game::getInstance().requestStates("graphics, standalone, level");
91            loadLevel = true;
92        }
93        if (CommandLine::getValue("server").getBool())
94        {
95            Game::getInstance().requestStates("graphics, server, level");
96            loadLevel = true;
97        }
98        if (CommandLine::getValue("client").getBool())
99        {
100            Game::getInstance().requestStates("graphics, client, level");
101            loadLevel = true;
102        }
103        if (CommandLine::getValue("dedicated").getBool())
104        {
105            Game::getInstance().requestStates("dedicated, level");
106            loadLevel = true;
107        }
108       
109        // Determine where to start otherwise
110        if (!loadLevel && !CommandLine::getValue("console").getBool())
111        {
112            // Also load graphics
113            Game::getInstance().requestState("graphics");
114        }
115    }
116
117    void GSRoot::deactivate()
118    {
119/*
120        if (this->ccSetTimeFactor_)
121        {
122            delete this->ccSetTimeFactor_;
123            this->ccSetTimeFactor_ = 0;
124        }
125
126        if (this->ccPause_)
127        {
128            delete this->ccPause_;
129            this->ccPause_ = 0;
130        }
131*/
132    }
133
134    void GSRoot::update(const Clock& time)
135    {
136        if (this->getActivity().topState)
137        {
138            // This state can not 'survive' on its own.
139            // Load a user interface therefore
140            Game::getInstance().requestState("ioConsole");
141        }
142
143        uint64_t timeBeforeTick = time.getRealMicroseconds();
144
145        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
146            it->tick(time);
147
148        /*** HACK *** HACK ***/
149        // Call the Tickable objects
150        float leveldt = time.getDeltaTime();
151        if (leveldt > 1.0f)
152        {
153            // just loaded
154            leveldt = 0.0f;
155        }
156        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
157            it->tick(leveldt * this->timeFactor_);
158        /*** HACK *** HACK ***/
159
160        uint64_t timeAfterTick = time.getRealMicroseconds();
161
162        // Also add our tick time
163        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
164    }
165
166    /**
167    @brief
168        Changes the speed of Orxonox
169    */
170    void GSRoot::setTimeFactor(float factor)
171    {
172        if (GameMode::isMaster())
173        {
174            if (!this->bPaused_)
175            {
176                TimeFactorListener::timefactor_s = factor;
177
178                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
179                    it->changedTimeFactor(factor, this->timeFactor_);
180
181                this->timeFactor_ = factor;
182            }
183            else
184                this->timeFactorPauseBackup_ = factor;
185        }
186    }
187
188    void GSRoot::pause()
189    {
190        if (GameMode::isMaster())
191        {
192            if (!this->bPaused_)
193            {
194                this->timeFactorPauseBackup_ = this->timeFactor_;
195                this->setTimeFactor(0.0f);
196                this->bPaused_ = true;
197            }
198            else
199            {
200                this->bPaused_ = false;
201                this->setTimeFactor(this->timeFactorPauseBackup_);
202            }
203        }
204    }
205}
Note: See TracBrowser for help on using the repository browser.