Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lod/src/orxonox/gamestates/GSLevel.cc @ 2768

Last change on this file since 2768 was 2058, checked in by rgrieder, 16 years ago

Reverted unintentional changes in lod branch.

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