Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1942 was 1942, checked in by landauf, 16 years ago

another small fix and a new levelfile (media update)

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