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 "OrxonoxStableHeaders.h" |
---|
30 | #include "GSRoot.h" |
---|
31 | |
---|
32 | #include "util/Exception.h" |
---|
33 | #include "util/Debug.h" |
---|
34 | #include "core/Clock.h" |
---|
35 | #include "core/Game.h" |
---|
36 | #include "core/GameMode.h" |
---|
37 | #include "core/CommandLine.h" |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "tools/TimeFactorListener.h" |
---|
40 | #include "tools/Timer.h" |
---|
41 | #include "objects/Tickable.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | AddGameState(GSRoot, "root"); |
---|
46 | SetCommandLineSwitch(console); |
---|
47 | // Shortcuts for easy direct loading |
---|
48 | SetCommandLineSwitch(server); |
---|
49 | SetCommandLineSwitch(client); |
---|
50 | SetCommandLineSwitch(dedicated); |
---|
51 | SetCommandLineSwitch(standalone); |
---|
52 | |
---|
53 | GSRoot::GSRoot(const std::string& name) |
---|
54 | : GameState(name) |
---|
55 | , timeFactor_(1.0f) |
---|
56 | , bPaused_(false) |
---|
57 | , timeFactorPauseBackup_(1.0f) |
---|
58 | { |
---|
59 | this->ccSetTimeFactor_ = 0; |
---|
60 | this->ccPause_ = 0; |
---|
61 | } |
---|
62 | |
---|
63 | GSRoot::~GSRoot() |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | void GSRoot::activate() |
---|
68 | { |
---|
69 | // reset game speed to normal |
---|
70 | this->timeFactor_ = 1.0f; |
---|
71 | |
---|
72 | { |
---|
73 | // time factor console command |
---|
74 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); |
---|
75 | functor->setObject(this); |
---|
76 | this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); |
---|
77 | CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); |
---|
78 | } |
---|
79 | |
---|
80 | { |
---|
81 | // time factor console command |
---|
82 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); |
---|
83 | functor->setObject(this); |
---|
84 | this->ccPause_ = createConsoleCommand(functor, "pause"); |
---|
85 | CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); |
---|
86 | } |
---|
87 | |
---|
88 | // Load level directly? |
---|
89 | bool loadLevel = false; |
---|
90 | if (CommandLine::getValue("standalone").getBool()) |
---|
91 | { |
---|
92 | Game::getInstance().requestStates("graphics, standalone, level"); |
---|
93 | loadLevel = true; |
---|
94 | } |
---|
95 | if (CommandLine::getValue("server").getBool()) |
---|
96 | { |
---|
97 | Game::getInstance().requestStates("graphics, server, level"); |
---|
98 | loadLevel = true; |
---|
99 | } |
---|
100 | if (CommandLine::getValue("client").getBool()) |
---|
101 | { |
---|
102 | Game::getInstance().requestStates("graphics, standalone, level"); |
---|
103 | loadLevel = true; |
---|
104 | } |
---|
105 | if (CommandLine::getValue("dedicated").getBool()) |
---|
106 | { |
---|
107 | Game::getInstance().requestStates("dedicated, level"); |
---|
108 | loadLevel = true; |
---|
109 | } |
---|
110 | |
---|
111 | // Determine where to start |
---|
112 | if (!loadLevel) |
---|
113 | { |
---|
114 | if (CommandLine::getValue("console").getBool()) |
---|
115 | { |
---|
116 | // Start the game in the console without 3D graphics |
---|
117 | Game::getInstance().requestState("ioConsole"); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | // Start in GUI mode |
---|
122 | Game::getInstance().requestStates("graphics, mainMenu"); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void GSRoot::deactivate() |
---|
128 | { |
---|
129 | if (this->ccSetTimeFactor_) |
---|
130 | { |
---|
131 | delete this->ccSetTimeFactor_; |
---|
132 | this->ccSetTimeFactor_ = 0; |
---|
133 | } |
---|
134 | |
---|
135 | if (this->ccPause_) |
---|
136 | { |
---|
137 | delete this->ccPause_; |
---|
138 | this->ccPause_ = 0; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | void GSRoot::update(const Clock& time) |
---|
143 | { |
---|
144 | if (this->getActivity().topState) |
---|
145 | { |
---|
146 | // This state can not 'survive' on its own. |
---|
147 | // Load a user interface therefore |
---|
148 | Game::getInstance().requestState("ioConsole"); |
---|
149 | } |
---|
150 | |
---|
151 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
152 | |
---|
153 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
154 | it->tick(time); |
---|
155 | |
---|
156 | /*** HACK *** HACK ***/ |
---|
157 | // Call the Tickable objects |
---|
158 | float leveldt = time.getDeltaTime(); |
---|
159 | if (leveldt > 1.0f) |
---|
160 | { |
---|
161 | // just loaded |
---|
162 | leveldt = 0.0f; |
---|
163 | } |
---|
164 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
165 | it->tick(leveldt * this->timeFactor_); |
---|
166 | /*** HACK *** HACK ***/ |
---|
167 | |
---|
168 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
169 | |
---|
170 | // Also add our tick time |
---|
171 | Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | @brief |
---|
176 | Changes the speed of Orxonox |
---|
177 | */ |
---|
178 | void GSRoot::setTimeFactor(float factor) |
---|
179 | { |
---|
180 | if (GameMode::isMaster()) |
---|
181 | { |
---|
182 | if (!this->bPaused_) |
---|
183 | { |
---|
184 | TimeFactorListener::timefactor_s = factor; |
---|
185 | |
---|
186 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
187 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
188 | |
---|
189 | this->timeFactor_ = factor; |
---|
190 | } |
---|
191 | else |
---|
192 | this->timeFactorPauseBackup_ = factor; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | void GSRoot::pause() |
---|
197 | { |
---|
198 | if (GameMode::isMaster()) |
---|
199 | { |
---|
200 | if (!this->bPaused_) |
---|
201 | { |
---|
202 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
203 | this->setTimeFactor(0.0f); |
---|
204 | this->bPaused_ = true; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | this->bPaused_ = false; |
---|
209 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|