Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

OrxoBlox in the HUD

File size: 9.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 "OrxoBloxStones.h"
49
50namespace orxonox
51{
52    // Events to allow to react to scoring of a player, in the level-file.
53    CreateEventName(OrxoBloxCenterpoint, right);
54    CreateEventName(OrxoBloxCenterpoint, left);
55
56    RegisterUnloadableClass(OrxoBlox);
57
58    /**
59    @brief
60        Constructor. Registers and initializes the object.
61    */
62    OrxoBlox::OrxoBlox(Context* context) : Deathmatch(context)
63    {
64        RegisterObject(OrxoBlox);
65
66        this->center_ = nullptr;
67        this->ball_ = nullptr;
68        this->bat_[0] = 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(OrxoBloxBat);
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        orxout() << "Hello" << endl;
129        if (this->center_ != nullptr) // There needs to be a OrxoBloxCenterpoint, i.e. the area the game takes place.
130        {
131            if (this->ball_ == nullptr) // If there is no ball, create a new ball.
132            {
133                this->ball_ = new OrxoBloxBall(this->center_->getContext());
134                // Apply the template for the ball specified by the centerpoint.
135                this->ball_->addTemplate(this->center_->getBalltemplate());
136            }
137
138            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
139            this->center_->attach(this->ball_);
140            this->ball_->setPosition(0, 0, 0);
141            this->ball_->setFieldDimension(this->center_->getFieldDimension());
142            this->ball_->setSpeed(0);
143            this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor());
144            this->ball_->setBatLength(this->center_->getBatLength());
145
146            // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint.
147            for (WeakPtr<orxonox::OrxoBloxBat>& bat : this->bat_)
148            {
149                if (bat == nullptr)
150                {
151                    bat = new OrxoBloxBat(this->center_->getContext());
152                    bat->addTemplate(this->center_->getBattemplate());
153                }
154            }
155
156            // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to.
157            this->center_->attach(this->bat_[0]);
158            this->center_->attach(this->bat_[1]);
159            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
160            this->bat_[0]->yaw(Degree(-90));
161            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
162            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
163            this->bat_[0]->setLength(this->center_->getBatLength());
164
165            // Set the bats for the ball.
166            this->ball_->setBats(this->bat_);
167        }
168        else // If no centerpoint was specified, an error is thrown and the level is exited.
169        {
170            orxout(internal_error) << "OrxoBlox: No Centerpoint specified." << endl;
171            GSLevel::startMainMenu();
172            return;
173        }
174
175        // Start the timer. After it has expired the ball is started.
176        this->starttimer_.startTimer();
177
178        // Set variable to temporarily force the player to spawn.
179        bool temp = this->bForceSpawn_;
180        this->bForceSpawn_ = true;
181
182        // Call start for the parent class.
183        Deathmatch::start();
184
185        // Reset the variable.
186        this->bForceSpawn_ = temp;
187    }
188
189    /**
190    @brief
191        Ends the OrxoBlox minigame.
192    */
193    void OrxoBlox::end()
194    {
195        ChatManager::message("You suck!!");
196        this->cleanup();
197
198        // Call end for the parent class.
199        Deathmatch::end();
200    }
201
202    /**
203    @brief
204        Spawns players, and fills the rest up with bots.
205    */
206    void OrxoBlox::spawnPlayersIfRequested()
207    {
208        // first spawn human players to assign always the left bat to the player in singleplayer
209        for (const auto& mapEntry : this->players_)
210            if (mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_))
211                this->spawnPlayer(mapEntry.first);
212        // now spawn bots
213        for (const auto& mapEntry : this->players_)
214            if (!mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_))
215                this->spawnPlayer(mapEntry.first);
216    }
217
218    /**
219    @brief
220        Spawns the input player.
221    @param player
222        The player to be spawned.
223    */
224    void OrxoBlox::spawnPlayer(PlayerInfo* player)
225    {
226        assert(player);
227
228        // If the first (left) bat has no player.
229        if (this->bat_[0]->getPlayer() == nullptr)
230        {
231            player->startControl(this->bat_[0]);
232            this->players_[player].state_ = PlayerState::Alive;
233        }
234        else
235            return;
236    }
237
238    /**
239    @brief
240        Is called when the player scored.
241    */
242    void OrxoBlox::playerScored(PlayerInfo* player, int score)
243    {
244        Deathmatch::playerScored(player, score);
245
246        if (this->center_ != nullptr) // If there is a centerpoint.
247        {
248            // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks.
249           
250            if (player == this->getLeftPlayer())
251                this->center_->fireEvent(FireEventName(OrxoBloxCenterpoint, left));
252
253            // Also announce, that the player has scored.
254            if (player != nullptr)
255                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
256        }
257
258        // If there is a ball present, reset its position, velocity and acceleration.
259        if (this->ball_ != nullptr)
260        {
261            this->ball_->setPosition(Vector3::ZERO);
262            this->ball_->setVelocity(Vector3::ZERO);
263            this->ball_->setAcceleration(Vector3::ZERO);
264            this->ball_->setSpeed(0);
265        }
266
267        // If there are bats reset them to the middle position.
268        if (this->bat_[0] != nullptr && this->bat_[1] != nullptr)
269        {
270            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
271           
272        }
273
274
275        // Restart the timer to start the ball.
276        this->starttimer_.startTimer();
277    }
278
279    /**
280    @brief
281        Starts the ball with some default speed.
282    */
283    void OrxoBlox::startBall()
284    {
285        if (this->ball_ != nullptr && this->center_ != nullptr)
286            this->ball_->setSpeed(this->center_->getBallSpeed());
287    }
288
289    /**
290    @brief
291        Get the left player.
292    @return
293        Returns a pointer to the player playing on the left. If there is no left player, nullptr is returned.
294    */
295    PlayerInfo* OrxoBlox::getLeftPlayer() const
296    {
297        if (this->bat_[0] != nullptr)
298            return this->bat_[0]->getPlayer();
299        else
300            return nullptr;
301    }
302
303   
304}
Note: See TracBrowser for help on using the repository browser.