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", false); |
---|
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, bool countTickTime) |
---|
54 | : GameState(name, countTickTime) |
---|
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, client, 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 otherwise |
---|
112 | if (!loadLevel && !CommandLine::getValue("console").getBool()) |
---|
113 | { |
---|
114 | // Also load graphics |
---|
115 | Game::getInstance().requestState("graphics"); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | void GSRoot::deactivate() |
---|
120 | { |
---|
121 | /* |
---|
122 | if (this->ccSetTimeFactor_) |
---|
123 | { |
---|
124 | delete this->ccSetTimeFactor_; |
---|
125 | this->ccSetTimeFactor_ = 0; |
---|
126 | } |
---|
127 | |
---|
128 | if (this->ccPause_) |
---|
129 | { |
---|
130 | delete this->ccPause_; |
---|
131 | this->ccPause_ = 0; |
---|
132 | } |
---|
133 | */ |
---|
134 | } |
---|
135 | |
---|
136 | void GSRoot::update(const Clock& time) |
---|
137 | { |
---|
138 | if (this->getActivity().topState) |
---|
139 | { |
---|
140 | // This state can not 'survive' on its own. |
---|
141 | // Load a user interface therefore |
---|
142 | Game::getInstance().requestState("ioConsole"); |
---|
143 | } |
---|
144 | |
---|
145 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
146 | |
---|
147 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
148 | it->tick(time); |
---|
149 | |
---|
150 | /*** HACK *** HACK ***/ |
---|
151 | // Call the Tickable objects |
---|
152 | float leveldt = time.getDeltaTime(); |
---|
153 | if (leveldt > 1.0f) |
---|
154 | { |
---|
155 | // just loaded |
---|
156 | leveldt = 0.0f; |
---|
157 | } |
---|
158 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
159 | it->tick(leveldt * this->timeFactor_); |
---|
160 | /*** HACK *** HACK ***/ |
---|
161 | |
---|
162 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
163 | |
---|
164 | // Also add our tick time |
---|
165 | Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | @brief |
---|
170 | Changes the speed of Orxonox |
---|
171 | */ |
---|
172 | void GSRoot::setTimeFactor(float factor) |
---|
173 | { |
---|
174 | if (GameMode::isMaster()) |
---|
175 | { |
---|
176 | if (!this->bPaused_) |
---|
177 | { |
---|
178 | TimeFactorListener::timefactor_s = factor; |
---|
179 | |
---|
180 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
181 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
182 | |
---|
183 | this->timeFactor_ = factor; |
---|
184 | } |
---|
185 | else |
---|
186 | this->timeFactorPauseBackup_ = factor; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void GSRoot::pause() |
---|
191 | { |
---|
192 | if (GameMode::isMaster()) |
---|
193 | { |
---|
194 | if (!this->bPaused_) |
---|
195 | { |
---|
196 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
197 | this->setTimeFactor(0.0f); |
---|
198 | this->bPaused_ = true; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | this->bPaused_ = false; |
---|
203 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|