Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/gamestates/GSLevel.cc @ 5820

Last change on this file since 5820 was 5820, checked in by landauf, 15 years ago

Changes in ClientConnectionListener:

  • Added static functions for connect/disconnect which notify all instances
  • getConnectedClients() also returned client "0" which is the ID of the server. This is important for the PlayerManager, but other classes (like for example TrafficControl) don't use getConnectedClients(), so I assumed I can safely remove this line. The 0-client gets now connected and disconnected in GSLevel (which also creates and destroys the PlayerManager). If the 0-client is also needed by other classes, I suggest moving it into the network.

The instance of HumanPlayer is now deleted if GSLevel gets deactivated. This also destroys the default HUD.

  • Property svn:eol-style set to native
File size: 9.8 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 *      Benjamin Knecht
27 *
28 */
29
30#include "GSLevel.h"
31
32#include <OgreCompositorManager.h>
33
34#include "core/input/InputManager.h"
35#include "core/input/InputState.h"
36#include "core/input/KeyBinder.h"
37#include "core/Clock.h"
38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
41#include "core/Game.h"
42#include "core/GameMode.h"
43#include "core/Core.h"
44#include "core/GraphicsManager.h"
45#include "core/GUIManager.h"
46#include "core/Loader.h"
47#include "core/XMLFile.h"
48
49#include "tools/interfaces/Tickable.h"
50#include "CameraManager.h"
51#include "LevelManager.h"
52#include "PlayerManager.h"
53#include "infos/HumanPlayer.h"
54
55namespace orxonox
56{
57    DeclareGameState(GSLevel, "level", false, false);
58    SetConsoleCommand(GSLevel, showIngameGUI, true);
59
60    XMLFile* GSLevel::startFile_s = NULL;
61
62    GSLevel::GSLevel(const GameStateInfo& info)
63        : GameState(info)
64        , keyBinder_(0)
65        , gameInputState_(0)
66        , guiMouseOnlyInputState_(0)
67        , guiKeysOnlyInputState_(0)
68        , cameraManager_(0)
69    {
70        RegisterObject(GSLevel);
71
72        this->ccKeybind_ = 0;
73        this->ccTkeybind_ = 0;
74    }
75
76    GSLevel::~GSLevel()
77    {
78    }
79
80    void GSLevel::setConfigValues()
81    {
82        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
83    }
84
85    void GSLevel::activate()
86    {
87        setConfigValues();
88
89        if (GameMode::showsGraphics())
90        {
91            gameInputState_ = InputManager::getInstance().createInputState("game");
92            keyBinder_ = new KeyBinder();
93            keyBinder_->loadBindings("keybindings.ini");
94            gameInputState_->setHandler(keyBinder_);
95
96            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
97            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
98
99            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
100            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
101
102            // create the global CameraManager
103            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
104        }
105
106        this->playerManager_ = new PlayerManager();
107
108        this->scope_GSLevel_ = new Scope<ScopeID::GSLevel>();
109
110        if (GameMode::isMaster())
111        {
112            this->loadLevel();
113        }
114
115        if (GameMode::showsGraphics())
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().enterState("game");
131           
132            // connect the HumanPlayer to the game
133            this->playerManager_->clientConnected(0);
134        }
135    }
136
137    void GSLevel::showIngameGUI(bool show)
138    {
139        if (show)
140        {
141            GUIManager::getInstance().showGUI("inGameTest");
142            GUIManager::getInstance().executeCode("showCursor()");
143            InputManager::getInstance().enterState("guiMouseOnly");
144        }
145        else
146        {
147            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
148            GUIManager::getInstance().executeCode("hideCursor()");
149            InputManager::getInstance().leaveState("guiMouseOnly");
150        }
151    }
152
153    void GSLevel::deactivate()
154    {
155/*
156        // destroy console commands
157        if (this->ccKeybind_)
158        {
159            delete this->ccKeybind_;
160            this->ccKeybind_ = 0;
161        }
162        if (this->ccTkeybind_)
163        {
164            delete this->ccTkeybind_;
165            this->ccTkeybind_ = 0;
166        }
167*/
168
169        if (GameMode::showsGraphics())
170        {
171            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
172            Ogre::CompositorManager::getSingleton().removeAll();
173        }
174
175        // this call will delete every BaseObject!
176        // But currently this will call methods of objects that exist no more
177        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
178        // and call a sceneNode method that has already been destroy by the corresponding space ship.
179        //Loader::close();
180
181        if (GameMode::showsGraphics())
182        {
183            // disconnect the HumanPlayer
184            this->playerManager_->clientDisconnected(0);
185           
186            InputManager::getInstance().leaveState("game");
187        }
188
189        if (GameMode::isMaster())
190            this->unloadLevel();
191
192        if (this->cameraManager_)
193        {
194            delete this->cameraManager_;
195            this->cameraManager_ = 0;
196        }
197
198        if (this->playerManager_)
199        {
200            this->playerManager_->destroy();
201            this->playerManager_ = 0;
202        }
203
204        if (this->scope_GSLevel_)
205        {
206            delete this->scope_GSLevel_;
207            this->scope_GSLevel_ = NULL;
208        }
209
210        if (GameMode::showsGraphics())
211        {
212            gameInputState_->setHandler(0);
213            guiMouseOnlyInputState_->setHandler(0);
214            guiKeysOnlyInputState_->setHandler(0);
215            InputManager::getInstance().destroyState("game");
216            InputManager::getInstance().destroyState("guiKeysOnly");
217            InputManager::getInstance().destroyState("guiMouseOnly");
218            if (this->keyBinder_)
219            {
220                this->keyBinder_->destroy();
221                this->keyBinder_ = 0;
222            }
223        }
224    }
225
226    void GSLevel::update(const Clock& time)
227    {
228        // Note: Temporarily moved to GSGraphics.
229        //// Call the scene objects
230        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
231        //    it->tick(time.getDeltaTime() * this->timeFactor_);
232    }
233
234    void GSLevel::loadLevel()
235    {
236        // call the loader
237        COUT(0) << "Loading level..." << std::endl;
238        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
239        Loader::open(startFile_s);
240    }
241
242    void GSLevel::unloadLevel()
243    {
244        Loader::unload(startFile_s);
245
246        delete startFile_s;
247    }
248
249    void GSLevel::keybind(const std::string &command)
250    {
251        this->keybindInternal(command, false);
252    }
253
254    void GSLevel::tkeybind(const std::string &command)
255    {
256        this->keybindInternal(command, true);
257    }
258
259    /**
260    @brief
261        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
262    @param command
263        Command string that can be executed by the CommandExecutor
264        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
265        the key/button/axis that has been activated. This is configured above in activate().
266    */
267    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
268    {
269        if (GameMode::showsGraphics())
270        {
271            static std::string bindingString = "";
272            static bool bTemporarySaved = false;
273            static bool bound = true;
274            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
275            // Howerver there will be no real issue if it happens anyway.
276            if (command.find(keyDetectorCallbackCode_) != 0)
277            {
278                if (bound)
279                {
280                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
281                    InputManager::getInstance().enterState("detector");
282                    bindingString = command;
283                    bTemporarySaved = bTemporary;
284                    bound = false;
285                }
286                //else:  We're still in a keybind command. ignore this call.
287            }
288            else
289            {
290                if (!bound)
291                {
292                    // user has pressed the key
293                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
294                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
295                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
296                    InputManager::getInstance().leaveState("detector");
297                    bound = true;
298                }
299                // else: A key was pressed within the same tick, ignore it.
300            }
301        }
302    }
303}
Note: See TracBrowser for help on using the repository browser.