Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/gamestates/GSLevel.cc @ 2396

Last change on this file since 2396 was 2396, checked in by landauf, 15 years ago

fixed shader/camera crash

  • Property svn:eol-style set to native
File size: 10.4 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
32#include "core/input/InputManager.h"
33#include "core/input/SimpleInputState.h"
34#include "core/input/KeyBinder.h"
[1662]35#include "core/Loader.h"
[2087]36#include "core/XMLFile.h"
[1887]37#include "core/CommandExecutor.h"
38#include "core/ConsoleCommand.h"
[1934]39#include "core/CommandLine.h"
[1887]40#include "core/ConfigValueIncludes.h"
41#include "core/CoreIncludes.h"
[2087]42#include "core/Core.h"
43//#include "objects/Backlight.h"
[1694]44#include "objects/Tickable.h"
[1819]45#include "objects/Radar.h"
[2087]46//#include "tools/ParticleInterface.h"
47#include "CameraManager.h"
48#include "LevelManager.h"
[2344]49#include "PlayerManager.h"
[1670]50#include "Settings.h"
[1661]51
52namespace orxonox
53{
[2173]54    SetCommandLineArgument(level, "sample3.oxw").shortcut("l");
[1934]55
[2087]56    GSLevel::GSLevel()
57//        : GameState<GSGraphics>(name)
58        : timeFactor_(1.0f)
[1662]59        , keyBinder_(0)
[1670]60        , inputState_(0)
[1662]61        , radar_(0)
[2087]62        , startFile_(0)
63        , cameraManager_(0)
64        , levelManager_(0)
[1661]65    {
[1887]66        RegisterObject(GSLevel);
[2350]67
68        this->ccKeybind_ = 0;
69        this->ccTkeybind_ = 0;
70        this->ccSetTimeFactor_ = 0;
71
[1887]72        setConfigValues();
[1661]73    }
74
75    GSLevel::~GSLevel()
76    {
77    }
78
[1887]79    void GSLevel::setConfigValues()
80    {
81        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
[2103]82        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
83            .description("Filename of default keybindings.");
[1887]84    }
85
[2087]86    void GSLevel::enter(Ogre::Viewport* viewport)
[1661]87    {
[2087]88        if (Core::showsGraphics())
89        {
90            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
91            keyBinder_ = new KeyBinder();
[2103]92            keyBinder_->loadBindings("keybindings.ini", defaultKeybindings_);
[2087]93            inputState_->setHandler(keyBinder_);
[1661]94
[2087]95            // create the global CameraManager
96            assert(viewport);
97            this->cameraManager_ = new CameraManager(viewport);
[1688]98
[2087]99            // Start the Radar
100            this->radar_ = new Radar();
101        }
[1662]102
[2396]103        this->playerManager_ = new PlayerManager();
104
[2087]105        if (Core::isMaster())
106        {
107            // create the global LevelManager
108            this->levelManager_ = new LevelManager();
[1662]109
[2087]110            // reset game speed to normal
111            timeFactor_ = 1.0f;
[1674]112
[2087]113            this->loadLevel();
114        }
[1755]115
[2087]116        if (Core::showsGraphics())
117        {
118            // TODO: insert slomo console command with
119            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
[1887]120
[2087]121            // keybind console command
122            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
123            functor1->setObject(this);
[2344]124            ccKeybind_ = createConsoleCommand(functor1, "keybind");
125            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]126            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
127            functor2->setObject(this);
[2344]128            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
129            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]130            // set our console command as callback for the key detector
131            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
132
133            // level is loaded: we can start capturing the input
134            InputManager::getInstance().requestEnterState("game");
135        }
136
137        if (Core::isMaster())
138        {
139            // time factor console command
140            FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
141            functor->setObject(this);
[2344]142            ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
143            CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
[2087]144        }
[1661]145    }
146
147    void GSLevel::leave()
148    {
[2344]149        // destroy console commands
[2350]150        if (this->ccKeybind_)
151        {
152            delete this->ccKeybind_;
153            this->ccKeybind_ = 0;
154        }
155        if (this->ccSetTimeFactor_)
156        {
157            delete this->ccSetTimeFactor_;
158            this->ccSetTimeFactor_ = 0;
159        }
160        if (this->ccTkeybind_)
161        {
162            delete this->ccTkeybind_;
163            this->ccTkeybind_ = 0;
164        }
[2344]165
[1662]166        // this call will delete every BaseObject!
167        // But currently this will call methods of objects that exist no more
168        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
169        // and call a sceneNode method that has already been destroy by the corresponding space ship.
170        //Loader::close();
171
[2087]172        if (Core::showsGraphics())
173            InputManager::getInstance().requestLeaveState("game");
[1662]174
[2087]175        if (Core::isMaster())
176            this->unloadLevel();
[1662]177
[2087]178        if (this->radar_)
[2350]179        {
[2087]180            delete this->radar_;
[2350]181            this->radar_ = 0;
182        }
[2087]183
184        if (this->cameraManager_)
[2350]185        {
[2087]186            delete this->cameraManager_;
[2350]187            this->cameraManager_ = 0;
188        }
[2087]189
190        if (this->levelManager_)
[2350]191        {
[2087]192            delete this->levelManager_;
[2350]193            this->levelManager_ = 0;
194        }
[2087]195
[2344]196        if (this->playerManager_)
[2350]197        {
[2344]198            delete this->playerManager_;
[2350]199            this->playerManager_ = 0;
200        }
[2344]201
[2087]202        if (Core::showsGraphics())
203        {
204            inputState_->setHandler(0);
205            InputManager::getInstance().requestDestroyState("game");
206            if (this->keyBinder_)
[2350]207            {
[2087]208                delete this->keyBinder_;
[2350]209                this->keyBinder_ = 0;
210            }
[2087]211        }
[1661]212    }
213
[1674]214    void GSLevel::ticked(const Clock& time)
[1661]215    {
[2087]216        // Commented by 1337: Temporarily moved to GSGraphics.
217        //// Call the scene objects
218        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
219        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]220    }
221
222    /**
223    @brief
224        Changes the speed of Orxonox
225    */
[1662]226    void GSLevel::setTimeFactor(float factor)
227    {
[2087]228/*
[1674]229        float change = factor / this->timeFactor_;
[2087]230*/
[1674]231        this->timeFactor_ = factor;
[2087]232/*
[1755]233        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
[1662]234            it->setSpeedFactor(it->getSpeedFactor() * change);
[1661]235
[1755]236        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
[1674]237            it->setTimeFactor(timeFactor_);
[2087]238*/
[1662]239    }
[1670]240
241    void GSLevel::loadLevel()
242    {
243        // call the loader
244        COUT(0) << "Loading level..." << std::endl;
[1934]245        std::string levelName;
246        CommandLine::getValue("level", &levelName);
[2087]247        startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName);
248        Loader::open(startFile_);
[1670]249    }
250
251    void GSLevel::unloadLevel()
252    {
[2087]253        //////////////////////////////////////////////////////////////////////////////////////////
254        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
255        //////////////////////////////////////////////////////////////////////////////////////////
256        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
257        //////////////////////////////////////////////////////////////////////////////////////////
258
259        delete this->startFile_;
[1670]260    }
[1887]261
262    void GSLevel::keybind(const std::string &command)
263    {
264        this->keybindInternal(command, false);
265    }
266
267    void GSLevel::tkeybind(const std::string &command)
268    {
269        this->keybindInternal(command, true);
270    }
271
272    /**
273    @brief
274        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
275    @param command
276        Command string that can be executed by the CommandExecutor
277        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
278        the key/button/axis that has been activated. This is configured above in enter().
279    */
280    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
281    {
[2087]282        if (Core::showsGraphics())
[1887]283        {
[2087]284            static std::string bindingString = "";
285            static bool bTemporarySaved = false;
286            static bool bound = true;
287            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
288            // Howerver there will be no real issue if it happens anyway.
289            if (command.find(keyDetectorCallbackCode_) != 0)
[1887]290            {
[2087]291                if (bound)
292                {
293                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
294                    InputManager::getInstance().requestEnterState("detector");
295                    bindingString = command;
296                    bTemporarySaved = bTemporary;
297                    bound = false;
298                }
299                //else:  We're still in a keybind command. ignore this call.
[1887]300            }
[2087]301            else
[1887]302            {
[2087]303                if (!bound)
304                {
305                    // user has pressed the key
306                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
307                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
308                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
309                    InputManager::getInstance().requestLeaveState("detector");
310                    bound = true;
311                }
312                // else: A key was pressed within the same tick, ignore it.
[1887]313            }
314        }
315    }
[1661]316}
Note: See TracBrowser for help on using the repository browser.