Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gamestates/GSLevel.cc @ 2848

Last change on this file since 2848 was 2848, checked in by rgrieder, 15 years ago

Exported showsGraphics, etc. to a new class named GameMode in the core.

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