Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged branch cpp11_v3 back to trunk

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