Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/orxonox/gamestates/GSRoot.cc @ 8420

Last change on this file since 8420 was 7935, checked in by landauf, 14 years ago
  • opening the ingame menu pauses the game, closing the ingame menu unpauses the game
  • added a text overlay to the default HUD which shows a notice if the game is paused
  • Property svn:eol-style set to native
File size: 5.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/BaseObject.h"
33#include "core/Game.h"
34#include "core/GameMode.h"
35#include "core/command/ConsoleCommand.h"
36#include "network/NetworkFunction.h"
37#include "tools/Timer.h"
38#include "tools/interfaces/Tickable.h"
39
40namespace orxonox
41{
42    DeclareGameState(GSRoot, "root", false, false);
43
44    static const std::string __CC_setTimeFactor_name = "setTimeFactor";
45    static const std::string __CC_setPause_name = "setPause";
46    static const std::string __CC_pause_name = "pause";
47
48    SetConsoleCommand("printObjects", &GSRoot::printObjects).hide();
49    SetConsoleCommand(__CC_setTimeFactor_name, &GSRoot::setTimeFactor).accessLevel(AccessLevel::Master).defaultValues(1.0);
50    SetConsoleCommand(__CC_setPause_name,      &GSRoot::setPause     ).accessLevel(AccessLevel::Master).hide();
51    SetConsoleCommand(__CC_pause_name,         &GSRoot::pause        ).accessLevel(AccessLevel::Master);
52
53    registerStaticNetworkFunction(&TimeFactorListener::setTimeFactor);
54
55    GSRoot::GSRoot(const GameStateInfo& info)
56        : GameState(info)
57        , bPaused_(false)
58        , timeFactorPauseBackup_(1.0f)
59    {
60    }
61
62    GSRoot::~GSRoot()
63    {
64        NetworkFunctionBase::destroyAllNetworkFunctions();
65    }
66
67    void GSRoot::printObjects()
68    {
69        unsigned int nr=0;
70        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ++it)
71        {
72            if (dynamic_cast<Synchronisable*>(*it))
73                COUT(0) << "object: " << it->getIdentifier()->getName() << " id: " << dynamic_cast<Synchronisable*>(*it)->getObjectID() << std::endl;
74            else
75                COUT(0) << "object: " << it->getIdentifier()->getName() << std::endl;
76            nr++;
77        }
78        COUT(0) << "currently got " << nr << " objects" << std::endl;
79    }
80
81    void GSRoot::activate()
82    {
83        // reset game speed to normal
84        TimeFactorListener::setTimeFactor(1.0f);
85
86        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(this);
87        ModifyConsoleCommand(__CC_setPause_name).setObject(this);
88        ModifyConsoleCommand(__CC_pause_name).setObject(this);
89    }
90
91    void GSRoot::deactivate()
92    {
93        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(0);
94        ModifyConsoleCommand(__CC_setPause_name).setObject(0);
95        ModifyConsoleCommand(__CC_pause_name).setObject(0);
96    }
97
98    void GSRoot::update(const Clock& time)
99    {
100        for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; )
101        {
102            Timer* object = *it;
103            ++it;
104            object->tick(time);
105        }
106
107        /*** HACK *** HACK ***/
108        // Call the Tickable objects
109        float leveldt = time.getDeltaTime();
110        if (leveldt > 1.0f)
111        {
112            // just loaded
113            leveldt = 0.0f;
114        }
115        float realdt = leveldt * TimeFactorListener::getTimeFactor();
116        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; )
117        {
118            Tickable* object = *it;
119            ++it;
120            object->tick(realdt);
121        }
122        /*** HACK *** HACK ***/
123    }
124
125    /**
126    @brief
127        Changes the speed of Orxonox
128    @remark
129        This function is a hack when placed here!
130        Timefactor should be related to the scene (level or so), not the game
131    */
132    void GSRoot::setTimeFactor(float factor)
133    {
134        if (GameMode::isMaster())
135        {
136            if (!this->bPaused_)
137            {
138                TimeFactorListener::setTimeFactor(factor);
139            }
140            else
141                this->timeFactorPauseBackup_ = factor;
142        }
143    }
144
145    void GSRoot::pause()
146    {
147        if (GameMode::isMaster())
148        {
149            if (!this->bPaused_)
150            {
151                this->timeFactorPauseBackup_ = TimeFactorListener::getTimeFactor();
152                this->setTimeFactor(0.0f);
153                this->bPaused_ = true;
154            }
155            else
156            {
157                this->bPaused_ = false;
158                this->setTimeFactor(this->timeFactorPauseBackup_);
159            }
160        }
161    }
162
163    void GSRoot::setPause(bool pause)
164    {
165        if (GameMode::isMaster())
166        {
167            if (pause != this->bPaused_)
168                this->pause();
169        }
170    }
171
172    void GSRoot::changedTimeFactor(float factor_new, float factor_old)
173    {
174        if (!GameMode::isStandalone())
175            callStaticNetworkFunction(&TimeFactorListener::setTimeFactor, CLIENTID_UNKNOWN, factor_new);
176    }
177}
Note: See TracBrowser for help on using the repository browser.