Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial5/src/orxonox/gamestates/GSRoot.cc @ 11479

Last change on this file since 11479 was 9348, checked in by landauf, 12 years ago

merged branch presentation2012merge back to trunk

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