Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickupsFS14/src/modules/jump/Jump.cc @ 10040

Last change on this file since 10040 was 10040, checked in by fvultier, 10 years ago

Bewegung mit WASD und Absprung von einer Platform in der Mitte funktioniert.

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