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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file OrxoBlox.cc |
---|
31 | @brief Implementation of the OrxoBlox class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "OrxoBlox.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/EventIncludes.h" |
---|
38 | #include "core/command/Executor.h" |
---|
39 | #include "core/config/ConfigValueIncludes.h" |
---|
40 | |
---|
41 | #include "gamestates/GSLevel.h" |
---|
42 | #include "chat/ChatManager.h" |
---|
43 | |
---|
44 | #include "OrxoBloxCenterpoint.h" |
---|
45 | #include "OrxoBloxBall.h" |
---|
46 | #include "OrxoBloxBat.h" |
---|
47 | #include "OrxoBloxBot.h" |
---|
48 | #include "OrxoBloxStones.h" |
---|
49 | #include "OrxoBloxWall.h" |
---|
50 | |
---|
51 | |
---|
52 | namespace orxonox |
---|
53 | { |
---|
54 | // Events to allow to react to scoring of a player, in the level-file. |
---|
55 | CreateEventName(OrxoBloxCenterpoint, right); |
---|
56 | CreateEventName(OrxoBloxCenterpoint, left); |
---|
57 | |
---|
58 | RegisterUnloadableClass(OrxoBlox); |
---|
59 | |
---|
60 | /** |
---|
61 | @brief |
---|
62 | Constructor. Registers and initializes the object. |
---|
63 | */ |
---|
64 | OrxoBlox::OrxoBlox(Context* context) : Deathmatch(context) |
---|
65 | { |
---|
66 | RegisterObject(OrxoBlox); |
---|
67 | |
---|
68 | this->center_ = nullptr; |
---|
69 | this->ball_ = nullptr; |
---|
70 | this->bat_[0] = nullptr; |
---|
71 | this->futureWall_ = nullptr; |
---|
72 | |
---|
73 | //this->setHUDTemplate("pongHUD"); |
---|
74 | //Error when specified |
---|
75 | |
---|
76 | // Pre-set the timer, but don't start it yet. |
---|
77 | this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&OrxoBlox::startBall, this))); |
---|
78 | this->starttimer_.stopTimer(); |
---|
79 | |
---|
80 | // Set the type of Bots for this particular Gametype. |
---|
81 | this->botclass_ = Class(OrxoBloxBat); |
---|
82 | |
---|
83 | this->scoreLimit_ = 10; |
---|
84 | this->setConfigValues(); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | @brief |
---|
89 | Destructor. Cleans up, if initialized. |
---|
90 | */ |
---|
91 | OrxoBlox::~OrxoBlox() |
---|
92 | { |
---|
93 | if (this->isInitialized()) |
---|
94 | this->cleanup(); |
---|
95 | } |
---|
96 | |
---|
97 | void OrxoBlox::setConfigValues() |
---|
98 | { |
---|
99 | SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins."); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | @brief |
---|
104 | Cleans up the Gametype by destroying the ball and the bats. |
---|
105 | */ |
---|
106 | void OrxoBlox::cleanup() |
---|
107 | { |
---|
108 | if (this->ball_ != nullptr) // Destroy the ball, if present. |
---|
109 | { |
---|
110 | this->ball_->destroy(); |
---|
111 | this->ball_ = nullptr; |
---|
112 | } |
---|
113 | |
---|
114 | // Destroy both bats, if present. |
---|
115 | for (size_t i = 0; i < 2; ++i) |
---|
116 | { |
---|
117 | if (this->bat_[0] != nullptr) |
---|
118 | { |
---|
119 | this->bat_[0]->destroy(); |
---|
120 | this->bat_[0] = nullptr; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | @brief |
---|
128 | Starts the OrxoBlox minigame. |
---|
129 | */ |
---|
130 | void OrxoBlox::start() |
---|
131 | { |
---|
132 | if (this->center_ != nullptr) // There needs to be a OrxoBloxCenterpoint, i.e. the area the game takes place. |
---|
133 | { |
---|
134 | if (this->ball_ == nullptr) // If there is no ball, create a new ball. |
---|
135 | { |
---|
136 | this->ball_ = new OrxoBloxBall(this->center_->getContext()); |
---|
137 | // Apply the template for the ball specified by the centerpoint. |
---|
138 | this->ball_->addTemplate(this->center_->getBalltemplate()); |
---|
139 | } |
---|
140 | |
---|
141 | // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to. |
---|
142 | this->center_->attach(this->ball_); |
---|
143 | this->ball_->setPosition(0,0,50); |
---|
144 | this->ball_->setFieldDimension(this->center_->getFieldDimension()); |
---|
145 | this->ball_->setSpeed(0); |
---|
146 | this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor()); |
---|
147 | this->ball_->setBatLength(this->center_->getBatLength()); |
---|
148 | |
---|
149 | // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint. |
---|
150 | for (WeakPtr<orxonox::OrxoBloxBat>& bat : this->bat_) |
---|
151 | { |
---|
152 | if (bat == nullptr) |
---|
153 | { |
---|
154 | bat = new OrxoBloxBat(this->center_->getContext()); |
---|
155 | bat->addTemplate(this->center_->getBattemplate()); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to. |
---|
160 | this->center_->attach(this->bat_[0]); |
---|
161 | this->center_->attach(this->bat_[1]); |
---|
162 | this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); |
---|
163 | this->bat_[0]->yaw(Degree(-90)); |
---|
164 | this->bat_[0]->setSpeed(this->center_->getBatSpeed()); |
---|
165 | this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y); |
---|
166 | this->bat_[0]->setLength(this->center_->getBatLength()); |
---|
167 | |
---|
168 | // Set the bats for the ball. |
---|
169 | this->ball_->setBats(this->bat_); |
---|
170 | } |
---|
171 | else // If no centerpoint was specified, an error is thrown and the level is exited. |
---|
172 | { |
---|
173 | orxout(internal_error) << "OrxoBlox: No Centerpoint specified." << endl; |
---|
174 | GSLevel::startMainMenu(); |
---|
175 | return; |
---|
176 | } |
---|
177 | |
---|
178 | // Start the timer. After it has expired the ball is started. |
---|
179 | this->starttimer_.startTimer(); |
---|
180 | |
---|
181 | // Set variable to temporarily force the player to spawn. |
---|
182 | bool temp = this->bForceSpawn_; |
---|
183 | this->bForceSpawn_ = true; |
---|
184 | |
---|
185 | // Call start for the parent class. |
---|
186 | Deathmatch::start(); |
---|
187 | |
---|
188 | // Reset the variable. |
---|
189 | this->bForceSpawn_ = temp; |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief |
---|
195 | Ends the OrxoBlox minigame. |
---|
196 | */ |
---|
197 | void OrxoBlox::end() |
---|
198 | { |
---|
199 | ChatManager::message("You suck!!"); |
---|
200 | this->cleanup(); |
---|
201 | |
---|
202 | // Call end for the parent class. |
---|
203 | Deathmatch::end(); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | @brief |
---|
208 | Spawns players, and fills the rest up with bots. |
---|
209 | */ |
---|
210 | void OrxoBlox::spawnPlayersIfRequested() |
---|
211 | { |
---|
212 | // first spawn human players to assign always the left bat to the player in singleplayer |
---|
213 | for (const auto& mapEntry : this->players_) |
---|
214 | if (mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
215 | this->spawnPlayer(mapEntry.first); |
---|
216 | // now spawn bots |
---|
217 | for (const auto& mapEntry : this->players_) |
---|
218 | if (!mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
219 | this->spawnPlayer(mapEntry.first); |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | @brief |
---|
224 | Spawns the input player. |
---|
225 | @param player |
---|
226 | The player to be spawned. |
---|
227 | */ |
---|
228 | void OrxoBlox::spawnPlayer(PlayerInfo* player) |
---|
229 | { |
---|
230 | assert(player); |
---|
231 | |
---|
232 | // If the first (left) bat has no player. |
---|
233 | if (this->bat_[0]->getPlayer() == nullptr) |
---|
234 | { |
---|
235 | player->startControl(this->bat_[0]); |
---|
236 | this->players_[player].state_ = PlayerState::Alive; |
---|
237 | } |
---|
238 | else |
---|
239 | return; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | @brief |
---|
244 | Is called when the player scored. |
---|
245 | */ |
---|
246 | void OrxoBlox::playerScored(PlayerInfo* player, int score) |
---|
247 | { |
---|
248 | Deathmatch::playerScored(player, score); |
---|
249 | |
---|
250 | if (this->center_ != nullptr) // If there is a centerpoint. |
---|
251 | { |
---|
252 | // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks. |
---|
253 | |
---|
254 | if (player == this->getLeftPlayer()) |
---|
255 | this->center_->fireEvent(FireEventName(OrxoBloxCenterpoint, left)); |
---|
256 | |
---|
257 | // Also announce, that the player has scored. |
---|
258 | if (player != nullptr) |
---|
259 | this->gtinfo_->sendAnnounceMessage(player->getName() + " scored"); |
---|
260 | } |
---|
261 | |
---|
262 | // If there is a ball present, reset its position, velocity and acceleration. |
---|
263 | if (this->ball_ != nullptr) |
---|
264 | { |
---|
265 | this->ball_->setPosition(Vector3::ZERO); |
---|
266 | this->ball_->setVelocity(Vector3::ZERO); |
---|
267 | this->ball_->setAcceleration(Vector3::ZERO); |
---|
268 | this->ball_->setSpeed(0); |
---|
269 | } |
---|
270 | |
---|
271 | // If there are bats reset them to the middle position. |
---|
272 | if (this->bat_[0] != nullptr && this->bat_[1] != nullptr) |
---|
273 | { |
---|
274 | this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | // Restart the timer to start the ball. |
---|
280 | this->starttimer_.startTimer(); |
---|
281 | } |
---|
282 | |
---|
283 | /*void OrxoBlox::createStonewall(void){ |
---|
284 | this->futureWall_ = new OrxoBolxWall(this->center_->getContext()); |
---|
285 | } |
---|
286 | |
---|
287 | /*void Tetris::createBrick(void) //TODO: random rotation offset between 0 and 3 (times 90°) |
---|
288 | { |
---|
289 | // create new futureBrick_ |
---|
290 | this->futureBrick_ = new TetrisBrick(this->center_->getContext()); |
---|
291 | |
---|
292 | |
---|
293 | // Apply the stone template to the stone. |
---|
294 | this->futureBrick_->addTemplate(this->center_->getBrickTemplate()); |
---|
295 | |
---|
296 | // Attach the brick to the Centerpoint and set the position of the brick to be at the left side. |
---|
297 | this->center_->attach(this->futureBrick_); |
---|
298 | float xPos = (this->center_->getWidth()*1.6f + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); |
---|
299 | float yPos = (this->center_->getHeight()-5.1f)*this->center_->getStoneSize(); |
---|
300 | |
---|
301 | this->futureBrick_->setPosition(xPos, yPos, 0.0f); |
---|
302 | this->futureBrick_->setGame(this); |
---|
303 | } |
---|
304 | |
---|
305 | }*/ |
---|
306 | |
---|
307 | /** |
---|
308 | @brief |
---|
309 | Starts the ball with some default speed. |
---|
310 | */ |
---|
311 | void OrxoBlox::startBall() |
---|
312 | { |
---|
313 | if (this->ball_ != nullptr && this->center_ != nullptr) |
---|
314 | this->ball_->setSpeed(this->center_->getBallSpeed()); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | @brief |
---|
319 | Get the left player. |
---|
320 | @return |
---|
321 | Returns a pointer to the player playing on the left. If there is no left player, nullptr is returned. |
---|
322 | */ |
---|
323 | PlayerInfo* OrxoBlox::getLeftPlayer() const |
---|
324 | { |
---|
325 | if (this->bat_[0] != nullptr) |
---|
326 | return this->bat_[0]->getPlayer(); |
---|
327 | else |
---|
328 | return nullptr; |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | } |
---|