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/ConsoleCommand.h" |
---|
33 | #include "core/input/InputManager.h" |
---|
34 | #include "core/input/SimpleInputState.h" |
---|
35 | #include "core/input/KeyBinder.h" |
---|
36 | #include "core/Loader.h" |
---|
37 | #include "overlays/console/InGameConsole.h" |
---|
38 | #include "gui/GUIManager.h" |
---|
39 | #include "objects/Backlight.h" |
---|
40 | #include "tools/ParticleInterface.h" |
---|
41 | #include "Radar.h" |
---|
42 | #include "Settings.h" |
---|
43 | #include "GraphicsEngine.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | GSLevel::GSLevel() |
---|
48 | : GameState("level") |
---|
49 | , timefactor_(1.0f) |
---|
50 | , keyBinder_(0) |
---|
51 | , radar_(0) |
---|
52 | , startLevel_(0) |
---|
53 | , hud_(0) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | GSLevel::~GSLevel() |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | void GSLevel::enter() |
---|
62 | { |
---|
63 | keyBinder_ = new KeyBinder(); |
---|
64 | keyBinder_->loadBindings(); |
---|
65 | InputManager::getInstance().createInputState<SimpleInputState>("game", 20)->setHandler(keyBinder_); |
---|
66 | |
---|
67 | // create Ogre SceneManager for the level |
---|
68 | GraphicsEngine::getInstance().createNewScene(); |
---|
69 | |
---|
70 | // Start the Radar |
---|
71 | this->radar_ = new Radar(); |
---|
72 | |
---|
73 | // Load the HUD |
---|
74 | COUT(3) << "Orxonox: Loading HUD" << std::endl; |
---|
75 | hud_ = new Level(Settings::getDataPath() + "overlay/hud.oxo"); |
---|
76 | Loader::load(hud_); |
---|
77 | |
---|
78 | // call the loader |
---|
79 | COUT(0) << "Loading level..." << std::endl; |
---|
80 | startLevel_ = new Level(Settings::getDataPath() + "levels/sample.oxw"); |
---|
81 | Loader::open(startLevel_); |
---|
82 | |
---|
83 | // add console commands |
---|
84 | FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor); |
---|
85 | functor->setObject(this); |
---|
86 | CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor, "setTimeFactor")); |
---|
87 | |
---|
88 | // level is loaded: we can start capturing the input |
---|
89 | InputManager::getInstance().requestEnterState("game"); |
---|
90 | } |
---|
91 | |
---|
92 | void GSLevel::leave() |
---|
93 | { |
---|
94 | InputManager::getInstance().requestLeaveState("game"); |
---|
95 | |
---|
96 | // TODO: Remove and destroy console command |
---|
97 | |
---|
98 | Loader::unload(startLevel_); |
---|
99 | delete this->startLevel_; |
---|
100 | |
---|
101 | Loader::unload(hud_); |
---|
102 | delete this->hud_; |
---|
103 | |
---|
104 | // this call will delete every BaseObject! |
---|
105 | // But currently this will call methods of objects that exist no more |
---|
106 | // The only 'memory leak' is the ParticleSpawer. They would be deleted here |
---|
107 | // and call a sceneNode method that has already been destroy by the corresponding space ship. |
---|
108 | //Loader::close(); |
---|
109 | |
---|
110 | delete this->radar_; |
---|
111 | |
---|
112 | // TODO: delete SceneManager |
---|
113 | |
---|
114 | InputManager::getInstance().destroyState("game"); |
---|
115 | delete this->keyBinder_; |
---|
116 | } |
---|
117 | |
---|
118 | bool GSLevel::tick(float dt) |
---|
119 | { |
---|
120 | // Call those objects that need the real time |
---|
121 | for (Iterator<TickableReal> it = ObjectList<TickableReal>::start(); it; ++it) |
---|
122 | it->tick(dt); |
---|
123 | // Call the scene objects |
---|
124 | for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it) |
---|
125 | it->tick(dt * this->timefactor_); |
---|
126 | |
---|
127 | // TODO: split file into server/client/standalone |
---|
128 | // call server/client with normal dt |
---|
129 | //if (client_g) |
---|
130 | // client_g->tick(dt * this->timefactor_); |
---|
131 | //if (server_g) |
---|
132 | // server_g->tick(dt * this->timefactor_); |
---|
133 | |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | @brief |
---|
139 | Changes the speed of Orxonox |
---|
140 | */ |
---|
141 | void GSLevel::setTimeFactor(float factor) |
---|
142 | { |
---|
143 | float change = factor / this->timefactor_; |
---|
144 | this->timefactor_ = factor; |
---|
145 | for (Iterator<ParticleInterface> it = ObjectList<ParticleInterface>::begin(); it; ++it) |
---|
146 | it->setSpeedFactor(it->getSpeedFactor() * change); |
---|
147 | |
---|
148 | for (Iterator<Backlight> it = ObjectList<Backlight>::begin(); it; ++it) |
---|
149 | it->setTimeFactor(timefactor_); |
---|
150 | } |
---|
151 | } |
---|