Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2353 was 2350, checked in by landauf, 16 years ago
  • Added support for postprocessing shaders (bloom, motion blur, …). Shaders are only visible if Plugin_CgProgramManager is included in bin/Plugins.cfg.
  • Added class GlobalShader which is visible on all clients and can be activated or deactivated by triggers or other events.
  • Added RadialBlur shader to the SpaceShip's engine. The strength of the RadialBlur depends on the SpaceShip's velocity.
  • 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
[2087]103        if (Core::isMaster())
104        {
105            // create the global LevelManager
106            this->levelManager_ = new LevelManager();
[2344]107            this->playerManager_ = new PlayerManager();
[1662]108
[2087]109            // reset game speed to normal
110            timeFactor_ = 1.0f;
[1674]111
[2087]112            this->loadLevel();
113        }
[1755]114
[2087]115        if (Core::showsGraphics())
116        {
117            // TODO: insert slomo console command with
118            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
[1887]119
[2087]120            // keybind console command
121            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
122            functor1->setObject(this);
[2344]123            ccKeybind_ = createConsoleCommand(functor1, "keybind");
124            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]125            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
126            functor2->setObject(this);
[2344]127            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
128            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]129            // set our console command as callback for the key detector
130            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
131
132            // level is loaded: we can start capturing the input
133            InputManager::getInstance().requestEnterState("game");
134        }
135
136        if (Core::isMaster())
137        {
138            // time factor console command
139            FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
140            functor->setObject(this);
[2344]141            ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
142            CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
[2087]143        }
[1661]144    }
145
146    void GSLevel::leave()
147    {
[2344]148        // destroy console commands
[2350]149        if (this->ccKeybind_)
150        {
151            delete this->ccKeybind_;
152            this->ccKeybind_ = 0;
153        }
154        if (this->ccSetTimeFactor_)
155        {
156            delete this->ccSetTimeFactor_;
157            this->ccSetTimeFactor_ = 0;
158        }
159        if (this->ccTkeybind_)
160        {
161            delete this->ccTkeybind_;
162            this->ccTkeybind_ = 0;
163        }
[2344]164
[1662]165        // this call will delete every BaseObject!
166        // But currently this will call methods of objects that exist no more
167        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
168        // and call a sceneNode method that has already been destroy by the corresponding space ship.
169        //Loader::close();
170
[2087]171        if (Core::showsGraphics())
172            InputManager::getInstance().requestLeaveState("game");
[1662]173
[2087]174        if (Core::isMaster())
175            this->unloadLevel();
[1662]176
[2087]177        if (this->radar_)
[2350]178        {
[2087]179            delete this->radar_;
[2350]180            this->radar_ = 0;
181        }
[2087]182
183        if (this->cameraManager_)
[2350]184        {
[2087]185            delete this->cameraManager_;
[2350]186            this->cameraManager_ = 0;
187        }
[2087]188
189        if (this->levelManager_)
[2350]190        {
[2087]191            delete this->levelManager_;
[2350]192            this->levelManager_ = 0;
193        }
[2087]194
[2344]195        if (this->playerManager_)
[2350]196        {
[2344]197            delete this->playerManager_;
[2350]198            this->playerManager_ = 0;
199        }
[2344]200
[2087]201        if (Core::showsGraphics())
202        {
203            inputState_->setHandler(0);
204            InputManager::getInstance().requestDestroyState("game");
205            if (this->keyBinder_)
[2350]206            {
[2087]207                delete this->keyBinder_;
[2350]208                this->keyBinder_ = 0;
209            }
[2087]210        }
[1661]211    }
212
[1674]213    void GSLevel::ticked(const Clock& time)
[1661]214    {
[2087]215        // Commented by 1337: Temporarily moved to GSGraphics.
216        //// Call the scene objects
217        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
218        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]219    }
220
221    /**
222    @brief
223        Changes the speed of Orxonox
224    */
[1662]225    void GSLevel::setTimeFactor(float factor)
226    {
[2087]227/*
[1674]228        float change = factor / this->timeFactor_;
[2087]229*/
[1674]230        this->timeFactor_ = factor;
[2087]231/*
[1755]232        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
[1662]233            it->setSpeedFactor(it->getSpeedFactor() * change);
[1661]234
[1755]235        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
[1674]236            it->setTimeFactor(timeFactor_);
[2087]237*/
[1662]238    }
[1670]239
240    void GSLevel::loadLevel()
241    {
242        // call the loader
243        COUT(0) << "Loading level..." << std::endl;
[1934]244        std::string levelName;
245        CommandLine::getValue("level", &levelName);
[2087]246        startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName);
247        Loader::open(startFile_);
[1670]248    }
249
250    void GSLevel::unloadLevel()
251    {
[2087]252        //////////////////////////////////////////////////////////////////////////////////////////
253        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
254        //////////////////////////////////////////////////////////////////////////////////////////
255        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
256        //////////////////////////////////////////////////////////////////////////////////////////
257
258        delete this->startFile_;
[1670]259    }
[1887]260
261    void GSLevel::keybind(const std::string &command)
262    {
263        this->keybindInternal(command, false);
264    }
265
266    void GSLevel::tkeybind(const std::string &command)
267    {
268        this->keybindInternal(command, true);
269    }
270
271    /**
272    @brief
273        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
274    @param command
275        Command string that can be executed by the CommandExecutor
276        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
277        the key/button/axis that has been activated. This is configured above in enter().
278    */
279    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
280    {
[2087]281        if (Core::showsGraphics())
[1887]282        {
[2087]283            static std::string bindingString = "";
284            static bool bTemporarySaved = false;
285            static bool bound = true;
286            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
287            // Howerver there will be no real issue if it happens anyway.
288            if (command.find(keyDetectorCallbackCode_) != 0)
[1887]289            {
[2087]290                if (bound)
291                {
292                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
293                    InputManager::getInstance().requestEnterState("detector");
294                    bindingString = command;
295                    bTemporarySaved = bTemporary;
296                    bound = false;
297                }
298                //else:  We're still in a keybind command. ignore this call.
[1887]299            }
[2087]300            else
[1887]301            {
[2087]302                if (!bound)
303                {
304                    // user has pressed the key
305                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
306                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
307                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
308                    InputManager::getInstance().requestLeaveState("detector");
309                    bound = true;
310                }
311                // else: A key was pressed within the same tick, ignore it.
[1887]312            }
313        }
314    }
[1661]315}
Note: See TracBrowser for help on using the repository browser.