Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8465 was 8327, checked in by scheusso, 14 years ago

merging network6 into trunk

  • Property svn:eol-style set to native
File size: 5.7 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_setPause_name = "setPause";
48    static const std::string __CC_pause_name = "pause";
49
50    /*static*/ bool GSRoot::startMainMenu_s = false;
51
52    SetConsoleCommand("printObjects", &GSRoot::printObjects).hide();
53    SetConsoleCommand(__CC_setTimeFactor_name, &GSRoot::setTimeFactor).accessLevel(AccessLevel::Master).defaultValues(1.0);
54    SetConsoleCommand(__CC_setPause_name,      &GSRoot::setPause     ).accessLevel(AccessLevel::Master).hide();
55    SetConsoleCommand(__CC_pause_name,         &GSRoot::pause        ).accessLevel(AccessLevel::Master);
56
57    registerStaticNetworkFunction(&TimeFactorListener::setTimeFactor);
58
59    GSRoot::GSRoot(const GameStateInfo& info)
60        : GameState(info)
61        , bPaused_(false)
62        , timeFactorPauseBackup_(1.0f)
63    {
64    }
65
66    GSRoot::~GSRoot()
67    {
68        NetworkFunctionBase::destroyAllNetworkFunctions();
69    }
70
71    void GSRoot::printObjects()
72    {
73        unsigned int nr=0;
74        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ++it)
75        {
76            if (dynamic_cast<Synchronisable*>(*it))
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    }
84
85    void GSRoot::activate()
86    {
87        // reset game speed to normal
88        TimeFactorListener::setTimeFactor(1.0f);
89
90        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(this);
91        ModifyConsoleCommand(__CC_setPause_name).setObject(this);
92        ModifyConsoleCommand(__CC_pause_name).setObject(this);
93    }
94
95    void GSRoot::deactivate()
96    {
97        ModifyConsoleCommand(__CC_setTimeFactor_name).setObject(0);
98        ModifyConsoleCommand(__CC_setPause_name).setObject(0);
99        ModifyConsoleCommand(__CC_pause_name).setObject(0);
100    }
101
102    void GSRoot::update(const Clock& time)
103    {
104        if(startMainMenu_s)
105        {
106            delayedStartMainMenu();
107            startMainMenu_s = false;
108        }
109
110        for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; )
111        {
112            Timer* object = *it;
113            ++it;
114            object->tick(time);
115        }
116
117        /*** HACK *** HACK ***/
118        // Call the Tickable objects
119        float leveldt = time.getDeltaTime();
120        if (leveldt > 1.0f)
121        {
122            // just loaded
123            leveldt = 0.0f;
124        }
125        float realdt = leveldt * TimeFactorListener::getTimeFactor();
126        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; )
127        {
128            Tickable* object = *it;
129            ++it;
130            object->tick(realdt);
131        }
132        /*** HACK *** HACK ***/
133    }
134
135    /**
136    @brief
137        Changes the speed of Orxonox
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
141    */
142    void GSRoot::setTimeFactor(float factor)
143    {
144        if (GameMode::isMaster())
145        {
146            if (!this->bPaused_)
147            {
148                TimeFactorListener::setTimeFactor(factor);
149            }
150            else
151                this->timeFactorPauseBackup_ = factor;
152        }
153    }
154
155    void GSRoot::pause()
156    {
157        if (GameMode::isMaster())
158        {
159            if (!this->bPaused_)
160            {
161                this->timeFactorPauseBackup_ = TimeFactorListener::getTimeFactor();
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    }
172
173    void GSRoot::setPause(bool pause)
174    {
175        if (GameMode::isMaster())
176        {
177            if (pause != this->bPaused_)
178                this->pause();
179        }
180    }
181
182    void GSRoot::changedTimeFactor(float factor_new, float factor_old)
183    {
184        if (!GameMode::isStandalone())
185            callStaticNetworkFunction(&TimeFactorListener::setTimeFactor, NETWORK_PEER_ID_BROADCAST, factor_new);
186    }
187
188    /*static*/ void GSRoot::delayedStartMainMenu(void)
189    {
190        if(!startMainMenu_s)
191            startMainMenu_s = true;
192        else
193            GSLevel::startMainMenu();
194    }
195
196}
Note: See TracBrowser for help on using the repository browser.