Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialFS09/src/orxonox/gamestates/GSLevel.cc @ 10256

Last change on this file since 10256 was 2763, checked in by scheusso, 16 years ago

first efforts for a tutorial

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