Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickupsFS14/src/modules/jump/JumpPlatform.cc @ 10041

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

Level wird zufaellig generiert, HUD funktionier(Punkte werden angezeigt), Diverse bugs behoben, Figur springt von allen Platformen korrekt ab.

File size: 11.7 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 JumpPlatform.cc
31    @brief Implementation of the JumpPlatform class.
32*/
33
34#include "JumpPlatform.h"
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "graphics/Model.h"
39#include "gametypes/Gametype.h"
40
41#include "JumpFigure.h"
42
43#include "sound/WorldSound.h"
44#include "core/XMLPort.h"
45
46namespace orxonox
47{
48    RegisterClass(JumpPlatform);
49
50    const float JumpPlatform::MAX_REL_Z_VELOCITY = 1.5;
51
52    /**
53    @brief
54        Constructor. Registers and initializes the object.
55    */
56    JumpPlatform::JumpPlatform(Context* context) : MovableEntity(context)
57    {
58        RegisterObject(JumpPlatform);
59
60        this->figure_ = 0;
61        this->bDeleteBats_ = false;
62        this->batID_ = new unsigned int[1];
63        this->batID_[0] = OBJECTID_UNKNOWN;
64        this->relMercyOffset_ = 0.05f;
65
66        this->registerVariables();
67
68        //initialize sound
69        if (GameMode::isMaster())
70                 {
71                         this->defScoreSound_ = new WorldSound(this->getContext());
72                         this->defScoreSound_->setVolume(1.0f);
73                         this->defBatSound_ = new WorldSound(this->getContext());
74                         this->defBatSound_->setVolume(0.4f);
75                         this->defBoundarySound_ = new WorldSound(this->getContext());
76                         this->defBoundarySound_->setVolume(0.5f);
77                 }
78                 else
79                 {
80                         this->defScoreSound_ = 0;
81                         this->defBatSound_ = 0;
82                         this->defBoundarySound_ = 0;
83                 }
84
85        this->setPosition(Vector3(0,0,0));
86        this->setVelocity(Vector3(0,0,0));
87        this->setAcceleration(Vector3(0,0,0));
88
89        model = NULL;
90    }
91
92    /**
93    @brief
94        Destructor.
95    */
96    JumpPlatform::~JumpPlatform()
97    {
98        /*if (this->isInitialized())
99        {
100            if (this->bDeleteBats_)
101                delete this->figure_;
102
103            delete[] this->batID_;
104        }*/
105    }
106
107    //xml port for loading sounds
108    void JumpPlatform::XMLPort(Element& xmlelement, XMLPort::Mode mode)
109    {
110        SUPER(JumpPlatform, XMLPort, xmlelement, mode);
111        XMLPortParam(JumpPlatform, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
112        XMLPortParam(JumpPlatform, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
113        XMLPortParam(JumpPlatform, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
114    }
115
116    /**
117    @brief
118        Register variables to synchronize over the network.
119    */
120    void JumpPlatform::registerVariables()
121    {
122        registerVariable( this->fieldWidth_ );
123        registerVariable( this->fieldHeight_ );
124        registerVariable( this->relMercyOffset_ );
125        registerVariable( this->batID_[0] );
126        //registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<JumpPlatform>( this, &JumpPlatform::applyBats) );
127    }
128
129    /**
130    @brief
131        Is called every tick.
132        Handles the movement of the ball and its interaction with the boundaries and bats.
133    @param dt
134        The time since the last tick.
135    */
136    void JumpPlatform::tick(float dt)
137    {
138        SUPER(JumpPlatform, tick, dt);
139
140        Vector3 platformPosition = this->getPosition();
141
142        if (figure_ != NULL)
143        {
144            Vector3 figurePosition = figure_->getPosition();
145            Vector3 figureVelocity = figure_->getVelocity();
146
147            if(figureVelocity.z < 0 && figurePosition.x > platformPosition.x-10 && figurePosition.x < platformPosition.x+10 && figurePosition.z > platformPosition.z-4 && figurePosition.z < platformPosition.z+4)
148            {
149                figure_->JumpFromPlatform(200.0f);
150            }
151        }
152
153
154
155
156
157        /*
158        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
159        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
160        {
161            defBoundarySound_->play(); //play boundary sound
162            // Its velocity in z-direction is inverted (i.e. it bounces off).
163            velocity.z = -velocity.z;
164            // And its position is set as to not overstep the boundary it has just crossed.
165            if (position.z > this->fieldHeight_ / 2)
166                position.z = this->fieldHeight_ / 2;
167            if (position.z < -this->fieldHeight_ / 2)
168                position.z = -this->fieldHeight_ / 2;
169
170            this->fireEvent();
171        }
172
173        // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry).
174        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
175        {
176            float distance = 0;
177
178            if (this->bat_ != NULL) // If there are bats.
179            {
180                // If the right boundary has been crossed.
181                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
182                {
183                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
184                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
185                    if (fabs(distance) <= 1) // If the bat is there to parry.
186                    {
187                        defBatSound_->play(); //play bat sound
188                        // Set the ball to be exactly at the boundary.
189                        position.x = this->fieldWidth_ / 2;
190                        // Invert its velocity in x-direction (i.e. it bounces off).
191                        velocity.x = -velocity.x;
192                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
193                        velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_;
194                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
195
196                        this->fireEvent();
197                    }
198                    // If the left player scores.
199                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
200                    {
201                        defScoreSound_->play();//play score sound
202                        if (this->getGametype() && this->bat_[0])
203                        {
204                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
205                            return;
206                        }
207                    }
208                }
209                // If the left boundary has been crossed.
210                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
211                {
212                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
213                    distance = (position.z - this->figure_->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
214                    if (fabs(distance) <= 1) // If the bat is there to parry.
215                    {
216                        defBatSound_->play(); //play bat sound
217                        // Set the ball to be exactly at the boundary.
218                        position.x = -this->fieldWidth_ / 2;
219                        // Invert its velocity in x-direction (i.e. it bounces off).
220                        velocity.x = -velocity.x;
221                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
222                        velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_;
223                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
224
225                        this->fireEvent();
226                    }
227                    // If the right player scores.
228                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
229                    {
230                        defScoreSound_->play();//play score sound
231                        if (this->getGametype() && this->bat_[1])
232                        {
233                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
234                            return;
235                        }
236                    }
237                }
238            }
239        }
240        */
241    }
242
243    /**
244    @brief
245        Set the bats for the ball.
246    @param bats
247        An array (of size 2) of weak pointers, to be set as the new bats.
248    */
249    void JumpPlatform::setFigure(WeakPtr<JumpFigure> newFigure)
250    {
251        if (this->bDeleteBats_) // If there are already some bats, delete them.
252        {
253            delete this->figure_;
254            this->bDeleteBats_ = false;
255        }
256
257        this->figure_ = newFigure;
258        // Also store their object IDs, for synchronization.
259        this->batID_[0] = this->figure_->getObjectID();
260    }
261
262    /**
263    @brief
264        Get the bats over the network.
265    */
266    void JumpPlatform::applyBats()
267    {
268        // Make space for the bats, if they don't exist, yet.
269        if (this->figure_ == NULL)
270        {
271            this->figure_ = *(new WeakPtr<JumpFigure>);
272            this->bDeleteBats_ = true;
273        }
274
275        if (this->batID_[0] != OBJECTID_UNKNOWN)
276        {
277                // WAR IM PONG NICHT AUSKOMMENTIERT!!!
278            //this->figure_ = orxonox_cast<JumpFigure>(Synchronisable::getSynchronisable(this->batID_[0]));
279        }
280    }
281
282    void JumpPlatform::setDefScoreSound(const std::string &jumpSound)
283    {
284        if( defScoreSound_ )
285            defScoreSound_->setSource(jumpSound);
286        else
287            assert(0); // This should never happen, because soundpointer is only available on master
288    }
289
290    const std::string& JumpPlatform::getDefScoreSound()
291    {
292        if( defScoreSound_ )
293            return defScoreSound_->getSource();
294        else
295            assert(0);
296        return BLANKSTRING;
297    }
298
299    void JumpPlatform::setDefBatSound(const std::string &jumpSound)
300    {
301        if( defBatSound_ )
302            defBatSound_->setSource(jumpSound);
303        else
304            assert(0); // This should never happen, because soundpointer is only available on master
305    }
306
307    const std::string& JumpPlatform::getDefBatSound()
308    {
309        if( defBatSound_ )
310            return defBatSound_->getSource();
311        else
312            assert(0);
313        return BLANKSTRING;
314    }
315
316    void JumpPlatform::setDefBoundarySound(const std::string &jumpSound)
317    {
318        if( defBoundarySound_ )
319            defBoundarySound_->setSource(jumpSound);
320        else
321            assert(0); // This should never happen, because soundpointer is only available on master
322    }
323
324    const std::string& JumpPlatform::getDefBoundarySound()
325    {
326        if( defBoundarySound_ )
327            return defBoundarySound_->getSource();
328        else
329            assert(0);
330        return BLANKSTRING;
331    }
332}
Note: See TracBrowser for help on using the repository browser.