Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9215 was 8051, checked in by dafrick, 14 years ago

Merging latest changes in usability branch into tutorial branch.

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