Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/gamestates/GSRoot.cc @ 6160

Last change on this file since 6160 was 6160, checked in by scheusso, 15 years ago

server may now pause/slow the game (also on clients) with commands setTimeFactor & pause

  • Property svn:eol-style set to native
File size: 4.4 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/ConsoleCommand.h"
33#include "core/Game.h"
34#include "core/GameMode.h"
35#include "network/NetworkFunction.h"
36#include "tools/Timer.h"
37#include "tools/interfaces/TimeFactorListener.h"
38#include "tools/interfaces/Tickable.h"
39
40namespace orxonox
41{
42    DeclareGameState(GSRoot, "root", false, false);
43    SetConsoleCommandShortcut(GSRoot, printObjects);
44
45    GSRoot::GSRoot(const GameStateInfo& info)
46        : GameState(info)
47        , bPaused_(false)
48        , timeFactorPauseBackup_(1.0f)
49    {
50    }
51
52    GSRoot::~GSRoot()
53    {
54        NetworkFunctionBase::destroyAllNetworkFunctions();
55    }
56   
57    void GSRoot::printObjects()
58    {
59        unsigned int nr=0;
60        for(ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ++it){
61            if( dynamic_cast<Synchronisable*>(*it) )
62                COUT(0) << "object: " << it->getIdentifier()->getName() << " id: " << dynamic_cast<Synchronisable*>(*it)->getObjectID() << std::endl;
63            else
64                COUT(0) << "object: " << it->getIdentifier()->getName() << std::endl;
65            nr++;
66        }
67        COUT(0) << "currently got " << nr << " objects" << std::endl;
68   
69    }
70
71    void GSRoot::activate()
72    {
73        // reset game speed to normal
74        TimeFactorListener::setTimeFactor(1.0f);
75
76        // time factor console command
77        ConsoleCommand* command = createConsoleCommand(createFunctor(&GSRoot::setTimeFactor, this), "setTimeFactor");
78        CommandExecutor::addConsoleCommandShortcut(command).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
79
80        // time factor console command
81        command = createConsoleCommand(createFunctor(&GSRoot::pause, this), "pause");
82        CommandExecutor::addConsoleCommandShortcut(command).accessLevel(AccessLevel::Offline);
83    }
84
85    void GSRoot::deactivate()
86    {
87    }
88
89    void GSRoot::update(const Clock& time)
90    {
91        for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; )
92            (it++)->tick(time);
93
94        /*** HACK *** HACK ***/
95        // Call the Tickable objects
96        float leveldt = time.getDeltaTime();
97        if (leveldt > 1.0f)
98        {
99            // just loaded
100            leveldt = 0.0f;
101        }
102        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; )
103            (it++)->tick(leveldt * TimeFactorListener::getTimeFactor());
104        /*** HACK *** HACK ***/
105    }
106
107    /**
108    @brief
109        Changes the speed of Orxonox
110    @remark
111        This function is a hack when placed here!
112        Timefactor should be related to the scene (level or so), not the game
113    */
114    void GSRoot::setTimeFactor(float factor)
115    {
116        if (GameMode::isMaster())
117        {
118            if (!this->bPaused_)
119            {
120                TimeFactorListener::setTimeFactor(factor);
121            }
122            else
123                this->timeFactorPauseBackup_ = factor;
124        }
125    }
126
127    void GSRoot::pause()
128    {
129        if (GameMode::isMaster())
130        {
131            if (!this->bPaused_)
132            {
133                this->timeFactorPauseBackup_ = TimeFactorListener::getTimeFactor();
134                this->setTimeFactor(0.0f);
135                this->bPaused_ = true;
136            }
137            else
138            {
139                this->bPaused_ = false;
140                this->setTimeFactor(this->timeFactorPauseBackup_);
141            }
142        }
143    }
144
145    float GSRoot::getTimeFactor()
146    {
147        return TimeFactorListener::getTimeFactor();
148    }
149}
Note: See TracBrowser for help on using the repository browser.