Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/orxonox/gamestates/GSRoot.cc @ 3340

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

Generalised use of tolua interfaces a bit: GSRoot now adds the interface functions to LuaBind which supports two new functions: openToluaInterfaces and closeToluaInterfaces. Both accept a lua state and can therefore be used to inject our own tolua bindings (the GUIManager makes use of that).
There's also something new: When loading a level, you could now use the tolua bindings from Orxonox as well (not just those from Core).

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