Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7208 was 7172, checked in by landauf, 14 years ago

removed network as dependency of tools. these libraries can build in parallel again now.
moved network synchronization of setTimeFactor from TimeFactorListener to GSRoot (GSRoot inherits from TimeFactorListener now)

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