Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2350 was 2350, checked in by landauf, 15 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
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/CommandLine.h"
40#include "core/ConfigValueIncludes.h"
41#include "core/CoreIncludes.h"
42#include "core/Core.h"
43//#include "objects/Backlight.h"
44#include "objects/Tickable.h"
45#include "objects/Radar.h"
46//#include "tools/ParticleInterface.h"
47#include "CameraManager.h"
48#include "LevelManager.h"
49#include "PlayerManager.h"
50#include "Settings.h"
51
52namespace orxonox
53{
54    SetCommandLineArgument(level, "sample3.oxw").shortcut("l");
55
56    GSLevel::GSLevel()
57//        : GameState<GSGraphics>(name)
58        : timeFactor_(1.0f)
59        , keyBinder_(0)
60        , inputState_(0)
61        , radar_(0)
62        , startFile_(0)
63        , cameraManager_(0)
64        , levelManager_(0)
65    {
66        RegisterObject(GSLevel);
67
68        this->ccKeybind_ = 0;
69        this->ccTkeybind_ = 0;
70        this->ccSetTimeFactor_ = 0;
71
72        setConfigValues();
73    }
74
75    GSLevel::~GSLevel()
76    {
77    }
78
79    void GSLevel::setConfigValues()
80    {
81        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
82        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
83            .description("Filename of default keybindings.");
84    }
85
86    void GSLevel::enter(Ogre::Viewport* viewport)
87    {
88        if (Core::showsGraphics())
89        {
90            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
91            keyBinder_ = new KeyBinder();
92            keyBinder_->loadBindings("keybindings.ini", defaultKeybindings_);
93            inputState_->setHandler(keyBinder_);
94
95            // create the global CameraManager
96            assert(viewport);
97            this->cameraManager_ = new CameraManager(viewport);
98
99            // Start the Radar
100            this->radar_ = new Radar();
101        }
102
103        if (Core::isMaster())
104        {
105            // create the global LevelManager
106            this->levelManager_ = new LevelManager();
107            this->playerManager_ = new PlayerManager();
108
109            // reset game speed to normal
110            timeFactor_ = 1.0f;
111
112            this->loadLevel();
113        }
114
115        if (Core::showsGraphics())
116        {
117            // TODO: insert slomo console command with
118            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
119
120            // keybind console command
121            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
122            functor1->setObject(this);
123            ccKeybind_ = createConsoleCommand(functor1, "keybind");
124            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
125            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
126            functor2->setObject(this);
127            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
128            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
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);
141            ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
142            CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
143        }
144    }
145
146    void GSLevel::leave()
147    {
148        // destroy console commands
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        }
164
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
171        if (Core::showsGraphics())
172            InputManager::getInstance().requestLeaveState("game");
173
174        if (Core::isMaster())
175            this->unloadLevel();
176
177        if (this->radar_)
178        {
179            delete this->radar_;
180            this->radar_ = 0;
181        }
182
183        if (this->cameraManager_)
184        {
185            delete this->cameraManager_;
186            this->cameraManager_ = 0;
187        }
188
189        if (this->levelManager_)
190        {
191            delete this->levelManager_;
192            this->levelManager_ = 0;
193        }
194
195        if (this->playerManager_)
196        {
197            delete this->playerManager_;
198            this->playerManager_ = 0;
199        }
200
201        if (Core::showsGraphics())
202        {
203            inputState_->setHandler(0);
204            InputManager::getInstance().requestDestroyState("game");
205            if (this->keyBinder_)
206            {
207                delete this->keyBinder_;
208                this->keyBinder_ = 0;
209            }
210        }
211    }
212
213    void GSLevel::ticked(const Clock& time)
214    {
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_);
219    }
220
221    /**
222    @brief
223        Changes the speed of Orxonox
224    */
225    void GSLevel::setTimeFactor(float factor)
226    {
227/*
228        float change = factor / this->timeFactor_;
229*/
230        this->timeFactor_ = factor;
231/*
232        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
233            it->setSpeedFactor(it->getSpeedFactor() * change);
234
235        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
236            it->setTimeFactor(timeFactor_);
237*/
238    }
239
240    void GSLevel::loadLevel()
241    {
242        // call the loader
243        COUT(0) << "Loading level..." << std::endl;
244        std::string levelName;
245        CommandLine::getValue("level", &levelName);
246        startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName);
247        Loader::open(startFile_);
248    }
249
250    void GSLevel::unloadLevel()
251    {
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_;
259    }
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    {
281        if (Core::showsGraphics())
282        {
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)
289            {
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.
299            }
300            else
301            {
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.
312            }
313        }
314    }
315}
Note: See TracBrowser for help on using the repository browser.