Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem5/src/orxonox/gamestates/GSLevel.cc @ 2786

Last change on this file since 2786 was 2786, checked in by dafrick, 15 years ago

Some changes to QuestManager and NotificationManager by Reto's request. (Make them more consistent as Singletons)

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