Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged pch branch back to trunk.

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