Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoBlox_FS19/src/modules/OrxoBlox/OrxoBlox.cc @ 12212

Last change on this file since 12212 was 12212, checked in by ahuwyler, 5 years ago

A new Game is born

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