Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/orxonox/gamestates/GSRoot.cc @ 9215

Last change on this file since 9215 was 8809, checked in by landauf, 13 years ago

Replaced COUT() with orxout() in tools and orxonox library. Requires quite some fine-tuning.

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