Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/orxonox/gamestates/GSRoot.cc @ 10474

Last change on this file since 10474 was 10474, checked in by landauf, 9 years ago

made NetworkFunctionManager a singleton, no static functions anymore (except for getInstance)

  • Property svn:eol-style set to native
File size: 6.3 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/Clock.h"
32#include "core/BaseObject.h"
33#include "core/Game.h"
34#include "core/GameMode.h"
35#include "core/command/ConsoleCommandIncludes.h"
36#include "network/NetworkFunctionIncludes.h"
37#include "network/NetworkFunctionManager.h"
38#include "tools/Timer.h"
39#include "tools/interfaces/Tickable.h"
40
41#include "GSLevel.h"
42
43namespace orxonox
44{
45    DeclareGameState(GSRoot, "root", false, false);
46
47    static const std::string __CC_setTimeFactor_name = "setTimeFactor";
48    static const std::string __CC_getTimeFactor_name = "getTimeFactor";
49    static const std::string __CC_setPause_name = "setPause";
50    static const std::string __CC_pause_name = "pause";
51
52    /*static*/ bool GSRoot::startMainMenu_s = false;
53
54    SetConsoleCommand("printObjects", &GSRoot::printObjects).hide();
55    SetConsoleCommand(__CC_setTimeFactor_name, &GSRoot::setTimeFactor).accessLevel(AccessLevel::Master).defaultValues(1.0);
56    SetConsoleCommand(__CC_getTimeFactor_name, &GSRoot::getTimeFactor).accessLevel(AccessLevel::Master);
57    SetConsoleCommand(__CC_setPause_name,      &GSRoot::setPause     ).accessLevel(AccessLevel::Master).hide();
58    SetConsoleCommand(__CC_pause_name,         &GSRoot::pause        ).accessLevel(AccessLevel::Master);
59
60    registerStaticNetworkFunction(&TimeFactorListener::setTimeFactor);
61
62    GSRoot::GSRoot(const GameStateInfo& info)
63        : GameState(info)
64        , bPaused_(false)
65        , timeFactorPauseBackup_(1.0f)
66    {
67    }
68
69    GSRoot::~GSRoot()
70    {
71        NetworkFunctionManager::getInstance().destroyAllNetworkFunctions();
72    }
73
74    void GSRoot::printObjects()
75    {
76        unsigned int nr=0;
77        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ++it)
78        {
79            Synchronisable* synchronisable = orxonox_cast<Synchronisable*>(*it);
80            if (synchronisable)
81                orxout(debug_output) << "object: " << it->getIdentifier()->getName() << " id: " << synchronisable->getObjectID() << endl;
82            else
83                orxout(debug_output) << "object: " << it->getIdentifier()->getName() << endl;
84            nr++;
85        }
86        orxout(debug_output) << "currently got " << nr << " objects" << endl;
87    }
88
89    void GSRoot::activate()
90    {
91        // reset game speed to normal
92        TimeFactorListener::setTimeFactor(1.0f);
93
94        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(this);
95        ModifyConsoleCommand(__CC_getTimeFactor_name).setObject(this);
96        ModifyConsoleCommand(__CC_setPause_name).setObject(this);
97        ModifyConsoleCommand(__CC_pause_name).setObject(this);
98    }
99
100    void GSRoot::deactivate()
101    {
102        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(0);
103        ModifyConsoleCommand(__CC_getTimeFactor_name).setObject(0);
104        ModifyConsoleCommand(__CC_setPause_name).setObject(0);
105        ModifyConsoleCommand(__CC_pause_name).setObject(0);
106    }
107
108    void GSRoot::update(const Clock& time)
109    {
110        if(startMainMenu_s)
111        {
112            delayedStartMainMenu();
113            startMainMenu_s = false;
114        }
115
116        for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; )
117        {
118            Timer* object = *it;
119            ++it;
120            object->tick(time);
121        }
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        float realdt = leveldt * TimeFactorListener::getTimeFactor();
132        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; )
133        {
134            Tickable* object = *it;
135            ++it;
136            object->tick(realdt);
137        }
138        /*** HACK *** HACK ***/
139    }
140
141    /**
142    @brief
143        Changes the speed of Orxonox
144    @remark
145        This function is a hack when placed here!
146        Timefactor should be related to the scene (level or so), not the game
147    */
148    void GSRoot::setTimeFactor(float factor)
149    {
150        if (GameMode::isMaster())
151        {
152            if (!this->bPaused_)
153            {
154                TimeFactorListener::setTimeFactor(factor);
155            }
156            else
157                this->timeFactorPauseBackup_ = factor;
158        }
159    }
160
161    float GSRoot::getTimeFactor()
162    {
163        return TimeFactorListener::getTimeFactor();
164    }
165
166    void GSRoot::pause()
167    {
168        if (GameMode::isMaster())
169        {
170            if (!this->bPaused_)
171            {
172                this->timeFactorPauseBackup_ = TimeFactorListener::getTimeFactor();
173                this->setTimeFactor(0.0f);
174                this->bPaused_ = true;
175            }
176            else
177            {
178                this->bPaused_ = false;
179                this->setTimeFactor(this->timeFactorPauseBackup_);
180            }
181        }
182    }
183
184    void GSRoot::setPause(bool pause)
185    {
186        if (GameMode::isMaster())
187        {
188            if (pause != this->bPaused_)
189                this->pause();
190        }
191    }
192
193    void GSRoot::changedTimeFactor(float factor_new, float factor_old)
194    {
195        if (!GameMode::isStandalone())
196            callStaticNetworkFunction(&TimeFactorListener::setTimeFactor, NETWORK_PEER_ID_BROADCAST, factor_new);
197    }
198
199    /*static*/ void GSRoot::delayedStartMainMenu(void)
200    {
201        if(!startMainMenu_s)
202            startMainMenu_s = true;
203        else
204            GSLevel::startMainMenu();
205    }
206
207}
Note: See TracBrowser for help on using the repository browser.