Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5693 was 5693, checked in by landauf, 15 years ago

merged libraries branch back to trunk

  • Property svn:eol-style set to native
File size: 5.2 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/ConsoleCommand.h"
33#include "core/Game.h"
34#include "core/GameMode.h"
35#include "core/LuaBind.h"
36#include "network/NetworkFunction.h"
37#include "ToluaBindCore.h"
38#include "ToluaBindOrxonox.h"
39#include "tools/Timer.h"
40#include "tools/interfaces/TimeFactorListener.h"
41#include "tools/interfaces/Tickable.h"
42#include "LevelManager.h"
43
44namespace orxonox
45{
46    DeclareGameState(GSRoot, "root", false, false);
47
48    GSRoot::GSRoot(const GameStateInfo& info)
49        : GameState(info)
50        , timeFactor_(1.0f)
51        , bPaused_(false)
52        , timeFactorPauseBackup_(1.0f)
53    {
54        this->ccSetTimeFactor_ = 0;
55        this->ccPause_ = 0;
56
57        // Tell LuaBind about all tolua interfaces
58        LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core");
59        LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox");
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
92    void GSRoot::deactivate()
93    {
94/*
95        if (this->ccSetTimeFactor_)
96        {
97            delete this->ccSetTimeFactor_;
98            this->ccSetTimeFactor_ = 0;
99        }
100
101        if (this->ccPause_)
102        {
103            delete this->ccPause_;
104            this->ccPause_ = 0;
105        }
106*/
107
108        delete this->levelManager_;
109    }
110
111    void GSRoot::update(const Clock& time)
112    {
113        if (this->getActivity().topState)
114        {
115            // This state can not 'survive' on its own.
116            // Load a user interface therefore
117            Game::getInstance().requestState("ioConsole");
118        }
119
120        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
121            it->tick(time);
122
123        /*** HACK *** HACK ***/
124        // Call the Tickable objects
125        float leveldt = time.getDeltaTime();
126        if (leveldt > 1.0f)
127        {
128            // just loaded
129            leveldt = 0.0f;
130        }
131        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
132            it->tick(leveldt * this->timeFactor_);
133        /*** HACK *** HACK ***/
134    }
135
136    /**
137    @brief
138        Changes the speed of Orxonox
139    @remark
140        This function is a hack when placed here!
141        Timefactor should be related to the scene (level or so), not the game
142    */
143    void GSRoot::setTimeFactor(float factor)
144    {
145        if (GameMode::isMaster())
146        {
147            if (!this->bPaused_)
148            {
149                TimeFactorListener::timefactor_s = factor;
150
151                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
152                    it->changedTimeFactor(factor, this->timeFactor_);
153
154                this->timeFactor_ = factor;
155            }
156            else
157                this->timeFactorPauseBackup_ = factor;
158        }
159    }
160
161    void GSRoot::pause()
162    {
163        if (GameMode::isMaster())
164        {
165            if (!this->bPaused_)
166            {
167                this->timeFactorPauseBackup_ = this->timeFactor_;
168                this->setTimeFactor(0.0f);
169                this->bPaused_ = true;
170            }
171            else
172            {
173                this->bPaused_ = false;
174                this->setTimeFactor(this->timeFactorPauseBackup_);
175            }
176        }
177    }
178}
Note: See TracBrowser for help on using the repository browser.