Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/gamestates/GSLevel.cc @ 2031

Last change on this file since 2031 was 2030, checked in by rgrieder, 16 years ago
  • Moved SignalHandler from orxonox to util
  • Moved CameraHandler from orxonox/objects to orxonox
  • updated msvc files
  • Property svn:eol-style set to native
File size: 8.5 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 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "GSLevel.h"
31
32#include "core/input/InputManager.h"
33#include "core/input/SimpleInputState.h"
34#include "core/input/KeyBinder.h"
35#include "core/Loader.h"
36#include "core/XMLFile.h"
37#include "core/CommandExecutor.h"
38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
41#include "core/Core.h"
42//#include "objects/Backlight.h"
43#include "objects/Tickable.h"
44#include "objects/Radar.h"
45//#include "tools/ParticleInterface.h"
46#include "CameraHandler.h"
47#include "LevelManager.h"
48#include "Settings.h"
49
50namespace orxonox
51{
52    GSLevel::GSLevel()
53//        : GameState<GSGraphics>(name)
54        : timeFactor_(1.0f)
55        , keyBinder_(0)
56        , inputState_(0)
57        , radar_(0)
58        , startFile_(0)
59        , cameraHandler_(0)
60        , levelManager_(0)
61    {
62        RegisterObject(GSLevel);
63        setConfigValues();
64    }
65
66    GSLevel::~GSLevel()
67    {
68    }
69
70    void GSLevel::setConfigValues()
71    {
72        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
73    }
74
75    void GSLevel::enter(Ogre::Viewport* viewport)
76    {
77        if (Core::showsGraphics())
78        {
79            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
80            keyBinder_ = new KeyBinder();
81            keyBinder_->loadBindings("keybindings.ini");
82            inputState_->setHandler(keyBinder_);
83
84            // create the global CameraHandler
85            assert(viewport);
86            this->cameraHandler_ = new CameraHandler(viewport);
87
88            // Start the Radar
89            this->radar_ = new Radar();
90        }
91
92        if (Core::isMaster())
93        {
94            // create the global LevelManager
95            this->levelManager_ = new LevelManager();
96
97            // reset game speed to normal
98            timeFactor_ = 1.0f;
99
100            this->loadLevel();
101        }
102
103        if (Core::showsGraphics())
104        {
105            // TODO: insert slomo console command with
106            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
107
108            // keybind console command
109            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
110            functor1->setObject(this);
111            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
112            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
113            functor2->setObject(this);
114            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
115            // set our console command as callback for the key detector
116            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
117
118            // level is loaded: we can start capturing the input
119            InputManager::getInstance().requestEnterState("game");
120        }
121
122        if (Core::isMaster())
123        {
124            // time factor console command
125            FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
126            functor->setObject(this);
127            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor, "setTimeFactor")).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
128        }
129    }
130
131    void GSLevel::leave()
132    {
133        // this call will delete every BaseObject!
134        // But currently this will call methods of objects that exist no more
135        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
136        // and call a sceneNode method that has already been destroy by the corresponding space ship.
137        //Loader::close();
138
139        if (Core::showsGraphics())
140            InputManager::getInstance().requestLeaveState("game");
141
142        if (Core::isMaster())
143            this->unloadLevel();
144
145        if (this->radar_)
146            delete this->radar_;
147
148        if (this->cameraHandler_)
149            delete this->cameraHandler_;
150
151        if (this->levelManager_)
152            delete this->levelManager_;
153
154        if (Core::showsGraphics())
155        {
156            inputState_->setHandler(0);
157            InputManager::getInstance().requestDestroyState("game");
158            if (this->keyBinder_)
159                delete this->keyBinder_;
160        }
161    }
162
163    void GSLevel::ticked(const Clock& time)
164    {
165        // Call the scene objects
166        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
167            it->tick(time.getDeltaTime() * this->timeFactor_);
168    }
169
170    /**
171    @brief
172        Changes the speed of Orxonox
173    */
174    void GSLevel::setTimeFactor(float factor)
175    {
176/*
177        float change = factor / this->timeFactor_;
178*/
179        this->timeFactor_ = factor;
180/*
181        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
182            it->setSpeedFactor(it->getSpeedFactor() * change);
183
184        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
185            it->setTimeFactor(timeFactor_);
186*/
187    }
188
189    void GSLevel::loadLevel()
190    {
191        // call the loader
192        COUT(0) << "Loading level..." << std::endl;
193        startFile_ = new XMLFile(Settings::getDataPath() + "levels/sample2.oxw");
194        Loader::open(startFile_);
195    }
196
197    void GSLevel::unloadLevel()
198    {
199        Loader::unload(startFile_);
200        delete this->startFile_;
201    }
202
203    void GSLevel::keybind(const std::string &command)
204    {
205        this->keybindInternal(command, false);
206    }
207
208    void GSLevel::tkeybind(const std::string &command)
209    {
210        this->keybindInternal(command, true);
211    }
212
213    /**
214    @brief
215        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
216    @param command
217        Command string that can be executed by the CommandExecutor
218        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
219        the key/button/axis that has been activated. This is configured above in enter().
220    */
221    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
222    {
223        if (Core::showsGraphics())
224        {
225            static std::string bindingString = "";
226            static bool bTemporarySaved = false;
227            static bool bound = true;
228            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
229            // Howerver there will be no real issue if it happens anyway.
230            if (command.find(keyDetectorCallbackCode_) != 0)
231            {
232                if (bound)
233                {
234                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
235                    InputManager::getInstance().requestEnterState("detector");
236                    bindingString = command;
237                    bTemporarySaved = bTemporary;
238                    bound = false;
239                }
240                //else:  We're still in a keybind command. ignore this call.
241            }
242            else
243            {
244                if (!bound)
245                {
246                    // user has pressed the key
247                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
248                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
249                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
250                    InputManager::getInstance().requestLeaveState("detector");
251                    bound = true;
252                }
253                // else: A key was pressed within the same tick, ignore it.
254            }
255        }
256    }
257}
Note: See TracBrowser for help on using the repository browser.