Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3979 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
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#include "network/NetworkFunction.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        NetworkFunctionBase::destroyAllNetworkFunctions();
64    }
65
66    void GSRoot::activate()
67    {
68        // reset game speed to normal
69        this->timeFactor_ = 1.0f;
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        }
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        }
116    }
117
118    void GSRoot::deactivate()
119    {
120/*
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        }
132*/
133    }
134
135    void GSRoot::update(const Clock& time)
136    {
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
144        uint64_t timeBeforeTick = time.getRealMicroseconds();
145
146        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
147            it->tick(time);
148
149        /*** HACK *** HACK ***/
150        // Call the Tickable objects
151        float leveldt = time.getDeltaTime();
152        if (leveldt > 1.0f)
153        {
154            // just loaded
155            leveldt = 0.0f;
156        }
157        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
158            it->tick(leveldt * this->timeFactor_);
159        /*** HACK *** HACK ***/
160
161        uint64_t timeAfterTick = time.getRealMicroseconds();
162
163        // Also add our tick time
164        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
165    }
166
167    /**
168    @brief
169        Changes the speed of Orxonox
170    */
171    void GSRoot::setTimeFactor(float factor)
172    {
173        if (GameMode::isMaster())
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    {
191        if (GameMode::isMaster())
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    }
206}
Note: See TracBrowser for help on using the repository browser.