Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7669 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

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