Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSMainMenu.cc @ 7876

Last change on this file since 7876 was 7876, checked in by dafrick, 13 years ago

Extending startGame and similar console commands, now also the level to be started (or in case of startClient, the destination) can be specified (but doesn't have to).
Additionally startMainMenu is now in GSLevel and can only be executed in a Level, as opposed to only in the MainMenu as it was before.
Added changeGame console command which works in a level in standalone mode to change to a different level.

  • Property svn:eol-style set to native
File size: 7.7 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 *      ...
26 *
27 */
28
29#include "GSMainMenu.h"
30
31#include <OgreSceneManager.h>
32
33#include "core/ConfigValueIncludes.h"
34#include "core/CoreIncludes.h"
35#include "core/Game.h"
36#include "core/GraphicsManager.h"
37#include "core/GUIManager.h"
38#include "core/command/ConsoleCommand.h"
39#include "core/input/KeyBinderManager.h"
40#include "network/Client.h"
41#include "util/StringUtils.h"
42#include "LevelManager.h"
43#include "Scene.h"
44#include "sound/AmbientSound.h"
45// HACK
46#include "core/input/InputManager.h"
47#include "core/input/InputState.h"
48
49namespace orxonox
50{
51    DeclareGameState(GSMainMenu, "mainMenu", false, true);
52
53    static const std::string __CC_startStandalone_name      = "startGame";
54    static const std::string __CC_startServer_name          = "startServer";
55    static const std::string __CC_startClient_name          = "startClient";
56    static const std::string __CC_startDedicated_name       = "startDedicated";
57    static const std::string __CC_setMainMenuSoundPath_name = "setMMSoundPath";
58
59    SetConsoleCommand(__CC_startStandalone_name,      &GSMainMenu::startStandalone).defaultValues(BLANKSTRING).deactivate();
60    SetConsoleCommand(__CC_startServer_name,          &GSMainMenu::startServer    ).defaultValues(BLANKSTRING).deactivate();
61    SetConsoleCommand(__CC_startClient_name,          &GSMainMenu::startClient    ).defaultValues(BLANKSTRING).deactivate();
62    SetConsoleCommand(__CC_startDedicated_name,       &GSMainMenu::startDedicated ).defaultValues(BLANKSTRING).deactivate();
63    SetConsoleCommand(__CC_setMainMenuSoundPath_name, &GSMainMenu::setMainMenuSoundPath).hide();
64
65    GSMainMenu::GSMainMenu(const GameStateInfo& info)
66        : GameState(info)
67    {
68        RegisterRootObject(GSMainMenu);
69
70        InputManager::getInstance().createInputState("MainMenuHackery", true, true)->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
71
72        // create an empty Scene
73        this->scene_ = new Scene(NULL);
74        this->scene_->setSyncMode( 0x0 );
75        // and a Camera
76        this->camera_ = this->scene_->getSceneManager()->createCamera("mainMenu/Camera");
77        if (GameMode::playsSound())
78        {
79            // Load sound
80            this->ambient_ = new AmbientSound();
81        }
82    }
83
84    GSMainMenu::~GSMainMenu()
85    {
86        if (GameMode::playsSound())
87            this->ambient_->destroy();
88
89        InputManager::getInstance().destroyState("MainMenuHackery");
90
91        this->scene_->getSceneManager()->destroyCamera(this->camera_);
92        this->scene_->destroy();
93    }
94
95    void GSMainMenu::activate()
96    {
97        // show main menu
98        GUIManager::getInstance().showGUI("MainMenu", true);
99        GUIManager::getInstance().setCamera(this->camera_);
100        GUIManager::getInstance().setBackgroundImage("MainMenuBackground", "Background");
101        GraphicsManager::getInstance().setCamera(this->camera_);
102
103        InputManager::getInstance().enterState("MainMenuHackery");
104
105        ModifyConsoleCommand(__CC_startStandalone_name).activate();
106        ModifyConsoleCommand(__CC_startServer_name    ).activate();
107        ModifyConsoleCommand(__CC_startClient_name    ).activate();
108        ModifyConsoleCommand(__CC_startDedicated_name ).activate();
109        ModifyConsoleCommand(__CC_setMainMenuSoundPath_name).setObject(this);
110
111        KeyBinderManager::getInstance().setToDefault();
112
113        this->setConfigValues();
114
115        if (GameMode::playsSound())
116        {
117            this->ambient_->setLooping(true);
118            this->ambient_->play(); // works without source
119        }
120    }
121
122    void GSMainMenu::deactivate()
123    {
124        if (GameMode::playsSound())
125        {
126            this->ambient_->stop();
127        }
128
129        InputManager::getInstance().leaveState("MainMenuHackery");
130
131        GUIManager::getInstance().setCamera(0);
132        GUIManager::getInstance().setBackgroundImage("");
133        GUIManager::hideGUI("MainMenu");
134        GraphicsManager::getInstance().setCamera(0);
135
136        ModifyConsoleCommand(__CC_startStandalone_name).deactivate();
137        ModifyConsoleCommand(__CC_startServer_name    ).deactivate();
138        ModifyConsoleCommand(__CC_startClient_name    ).deactivate();
139        ModifyConsoleCommand(__CC_startDedicated_name ).deactivate();
140        ModifyConsoleCommand(__CC_setMainMenuSoundPath_name).setObject(0);
141    }
142
143    void GSMainMenu::update(const Clock& time)
144    {
145    }
146
147    void GSMainMenu::setConfigValues()
148    {
149        SetConfigValue(soundPathMain_, "mainmenu.ogg")
150            .description("Contains the path to the main menu sound file.")
151            .callback(this, &GSMainMenu::reloadSound);
152    }
153
154    void GSMainMenu::reloadSound()
155    {
156        if (GameMode::playsSound())
157        {
158            this->ambient_->setAmbientSource(soundPathMain_);
159        }
160    }
161
162    const std::string& GSMainMenu::getMainMenuSoundPath()
163    {
164        return soundPathMain_;
165    }
166
167    void GSMainMenu::setMainMenuSoundPath(const std::string& path)
168    {
169        ModifyConfigValue(soundPathMain_, set, path);
170    }
171
172    /**
173    @brief
174        Start a level in standalone mode.
175    @param level
176        The filename of the level to be started. If empty, the default level is started.
177    */
178    void GSMainMenu::startStandalone(const std::string& level)
179    {
180        if(level != BLANKSTRING)
181            LevelManager::getInstance().setDefaultLevel(level);
182
183        // HACK
184        Game::getInstance().popState();
185        Game::getInstance().requestStates("standalone, level");
186    }
187
188    /**
189    @brief
190        Start a level in server mode.
191    @param level
192        The filename of the level to be started. If empty, the default level is started.
193    */
194    void GSMainMenu::startServer(const std::string& level)
195    {
196        if(level != BLANKSTRING)
197            LevelManager::getInstance().setDefaultLevel(level);
198
199        // HACK
200        Game::getInstance().popState();
201        Game::getInstance().requestStates("server, level");
202    }
203
204    /**
205    @brief
206        Connect to a game as client.
207    @param destination
208        The destination to connect to. If empty, the client connects to the default destination.
209    */
210    void GSMainMenu::startClient(const std::string& destination)
211    {
212        if(destination != BLANKSTRING)
213            Client::getInstance()->setDestination(destination, NETWORK_PORT);
214
215        // HACK
216        Game::getInstance().popState();
217        Game::getInstance().requestStates("client, level");
218    }
219
220    /**
221    @brief
222        Start a level in dedicated mode.
223    @param level
224        The filename of the level to be started. If empty, the default level is started.
225    */
226    void GSMainMenu::startDedicated(const std::string& level)
227    {
228        if(level != BLANKSTRING)
229            LevelManager::getInstance().setDefaultLevel(level);
230
231        // HACK
232        Game::getInstance().popState();
233        Game::getInstance().popState();
234        Game::getInstance().requestStates("server, level");
235    }
236
237}
Note: See TracBrowser for help on using the repository browser.