Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/gamestates/GSLevel.cc @ 2006

Last change on this file since 2006 was 2006, checked in by landauf, 16 years ago

many changes, can't remember everything, but these are the most important:

  • attaching entities to other entities works
  • displaying models, lights and shadows works
  • controlling a spectator works
  • removed an update hack in PositionableEntity because I've found a much better solution

and with "works" I mean: it works on client, server and standalone

  • Property svn:eol-style set to native
File size: 7.3 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 <OgreSceneManager.h>
33#include <OgreRoot.h>
34#include "core/input/InputManager.h"
35#include "core/input/SimpleInputState.h"
36#include "core/input/KeyBinder.h"
37#include "core/Loader.h"
38#include "core/CommandExecutor.h"
39#include "core/ConsoleCommand.h"
40#include "core/ConfigValueIncludes.h"
41#include "core/CoreIncludes.h"
42//#include "objects/Backlight.h"
43#include "objects/Tickable.h"
44#include "objects/Radar.h"
45//#include "tools/ParticleInterface.h"
46#include "GraphicsEngine.h"
47#include "Settings.h"
48
49namespace orxonox
50{
51    GSLevel::GSLevel(const std::string& name)
52        : GameState<GSGraphics>(name)
53        , timeFactor_(1.0f)
54        , sceneManager_(0)
55        , keyBinder_(0)
56        , inputState_(0)
57        , radar_(0)
58        , startLevel_(0)
59    {
60        RegisterObject(GSLevel);
61        setConfigValues();
62    }
63
64    GSLevel::~GSLevel()
65    {
66    }
67
68    void GSLevel::setConfigValues()
69    {
70        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
71    }
72
73    void GSLevel::enter()
74    {
75        inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
76        keyBinder_ = new KeyBinder();
77        keyBinder_->loadBindings("keybindings.ini");
78        inputState_->setHandler(keyBinder_);
79
80        // create Ogre SceneManager for the level
81        this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, "LevelSceneManager");
82        COUT(4) << "Created SceneManager: " << sceneManager_->getName() << std::endl;
83
84        this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
85
86        // temporary hack
87        GraphicsEngine::getInstance().setLevelSceneManager(this->sceneManager_);
88
89        // Start the Radar
90        this->radar_ = new Radar();
91
92        // reset game speed to normal
93        timeFactor_ = 1.0f;
94
95        // TODO: insert slomo console command with
96        // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
97
98        // keybind console command
99        FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
100        functor1->setObject(this);
101        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
102        FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
103        functor2->setObject(this);
104        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
105        // set our console command as callback for the key detector
106        InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
107    }
108
109    void GSLevel::leave()
110    {
111        // this call will delete every BaseObject!
112        // But currently this will call methods of objects that exist no more
113        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
114        // and call a sceneNode method that has already been destroy by the corresponding space ship.
115        //Loader::close();
116
117        delete this->radar_;
118
119        Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
120
121        inputState_->setHandler(0);
122        InputManager::getInstance().requestDestroyState("game");
123        delete this->keyBinder_;
124    }
125
126    void GSLevel::ticked(const Clock& time)
127    {
128        // Call the scene objects
129        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
130            it->tick(time.getDeltaTime() * this->timeFactor_);
131    }
132
133    /**
134    @brief
135        Changes the speed of Orxonox
136    */
137    void GSLevel::setTimeFactor(float factor)
138    {
139/*
140        float change = factor / this->timeFactor_;
141*/
142        this->timeFactor_ = factor;
143/*
144        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
145            it->setSpeedFactor(it->getSpeedFactor() * change);
146
147        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
148            it->setTimeFactor(timeFactor_);
149*/
150    }
151
152    void GSLevel::loadLevel()
153    {
154        // call the loader
155        COUT(0) << "Loading level..." << std::endl;
156        startLevel_ = new Level(Settings::getDataPath() + "levels/sample2.oxw");
157        Loader::open(startLevel_);
158    }
159
160    void GSLevel::unloadLevel()
161    {
162        Loader::unload(startLevel_);
163        delete this->startLevel_;
164    }
165
166    void GSLevel::keybind(const std::string &command)
167    {
168        this->keybindInternal(command, false);
169    }
170
171    void GSLevel::tkeybind(const std::string &command)
172    {
173        this->keybindInternal(command, true);
174    }
175
176    /**
177    @brief
178        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
179    @param command
180        Command string that can be executed by the CommandExecutor
181        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
182        the key/button/axis that has been activated. This is configured above in enter().
183    */
184    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
185    {
186        static std::string bindingString = "";
187        static bool bTemporarySaved = false;
188        static bool bound = true;
189        // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
190        // Howerver there will be no real issue if it happens anyway.
191        if (command.find(keyDetectorCallbackCode_) != 0)
192        {
193            if (bound)
194            {
195                COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
196                InputManager::getInstance().requestEnterState("detector");
197                bindingString = command;
198                bTemporarySaved = bTemporary;
199                bound = false;
200            }
201            //else:  We're still in a keybind command. ignore this call.
202        }
203        else
204        {
205            if (!bound)
206            {
207                // user has pressed the key
208                std::string name = command.substr(this->keyDetectorCallbackCode_.size());
209                COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
210                this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
211                InputManager::getInstance().requestLeaveState("detector");
212                bound = true;
213            }
214            // else: A key was pressed within the same tick, ignore it.
215        }
216    }
217}
Note: See TracBrowser for help on using the repository browser.