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
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        this->playerManager_ = new PlayerManager();
104
105        if (Core::isMaster())
106        {
107            // create the global LevelManager
108            this->levelManager_ = new LevelManager();
109
110            // reset game speed to normal
111            timeFactor_ = 1.0f;
112
113            this->loadLevel();
114        }
115
116        if (Core::showsGraphics())
117        {
118            // TODO: insert slomo console command with
119            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
120
121            // keybind console command
122            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
123            functor1->setObject(this);
124            ccKeybind_ = createConsoleCommand(functor1, "keybind");
125            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
126            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
127            functor2->setObject(this);
128            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
129            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
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);
142            ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
143            CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
144        }
145    }
146
147    void GSLevel::leave()
148    {
149        // destroy console commands
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        }
165
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
172        if (Core::showsGraphics())
173            InputManager::getInstance().requestLeaveState("game");
174
175        if (Core::isMaster())
176            this->unloadLevel();
177
178        if (this->radar_)
179        {
180            delete this->radar_;
181            this->radar_ = 0;
182        }
183
184        if (this->cameraManager_)
185        {
186            delete this->cameraManager_;
187            this->cameraManager_ = 0;
188        }
189
190        if (this->levelManager_)
191        {
192            delete this->levelManager_;
193            this->levelManager_ = 0;
194        }
195
196        if (this->playerManager_)
197        {
198            delete this->playerManager_;
199            this->playerManager_ = 0;
200        }
201
202        if (Core::showsGraphics())
203        {
204            inputState_->setHandler(0);
205            InputManager::getInstance().requestDestroyState("game");
206            if (this->keyBinder_)
207            {
208                delete this->keyBinder_;
209                this->keyBinder_ = 0;
210            }
211        }
212    }
213
214    void GSLevel::ticked(const Clock& time)
215    {
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_);
220    }
221
222    /**
223    @brief
224        Changes the speed of Orxonox
225    */
226    void GSLevel::setTimeFactor(float factor)
227    {
228/*
229        float change = factor / this->timeFactor_;
230*/
231        this->timeFactor_ = factor;
232/*
233        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
234            it->setSpeedFactor(it->getSpeedFactor() * change);
235
236        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
237            it->setTimeFactor(timeFactor_);
238*/
239    }
240
241    void GSLevel::loadLevel()
242    {
243        // call the loader
244        COUT(0) << "Loading level..." << std::endl;
245        std::string levelName;
246        CommandLine::getValue("level", &levelName);
247        startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName);
248        Loader::open(startFile_);
249    }
250
251    void GSLevel::unloadLevel()
252    {
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_;
260    }
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    {
282        if (Core::showsGraphics())
283        {
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)
290            {
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.
300            }
301            else
302            {
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.
313            }
314        }
315    }
316}
Note: See TracBrowser for help on using the repository browser.