Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10066 was 10050, checked in by fvultier, 11 years ago

Added a whole bunch of code

File size: 10.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 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
62        //initialize sound
63        if (GameMode::isMaster())
64                 {
65                         this->defScoreSound_ = new WorldSound(this->getContext());
66                         this->defScoreSound_->setVolume(1.0f);
67                         this->defBatSound_ = new WorldSound(this->getContext());
68                         this->defBatSound_->setVolume(0.4f);
69                         this->defBoundarySound_ = new WorldSound(this->getContext());
70                         this->defBoundarySound_->setVolume(0.5f);
71                 }
72                 else
73                 {
74                         this->defScoreSound_ = 0;
75                         this->defBatSound_ = 0;
76                         this->defBoundarySound_ = 0;
77                 }
78
79        this->setPosition(Vector3(0,0,0));
80        this->setVelocity(Vector3(0,0,0));
81        this->setAcceleration(Vector3(0,0,0));
82    }
83
84    /**
85    @brief
86        Destructor.
87    */
88    JumpPlatform::~JumpPlatform()
89    {
90        /*if (this->isInitialized())
91        {
92            if (this->bDeleteBats_)
93                delete this->figure_;
94
95            delete[] this->batID_;
96        }*/
97    }
98
99    //xml port for loading sounds
100    void JumpPlatform::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(JumpPlatform, XMLPort, xmlelement, mode);
103        XMLPortParam(JumpPlatform, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
104        XMLPortParam(JumpPlatform, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
105        XMLPortParam(JumpPlatform, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
106    }
107
108    /**
109    @brief
110        Is called every tick.
111        Handles the movement of the ball and its interaction with the boundaries and bats.
112    @param dt
113        The time since the last tick.
114    */
115    void JumpPlatform::tick(float dt)
116    {
117        SUPER(JumpPlatform, tick, dt);
118
119        Vector3 platformPosition = this->getPosition();
120
121        if (figure_ != NULL)
122        {
123            Vector3 figurePosition = figure_->getPosition();
124            Vector3 figureVelocity = figure_->getVelocity();
125
126            if(figureVelocity.z < 0 && figurePosition.x > platformPosition.x-10 && figurePosition.x < platformPosition.x+10 && figurePosition.z > platformPosition.z-4 && figurePosition.z < platformPosition.z+4)
127            {
128                touchFigure();
129            }
130        }
131
132
133
134
135
136        /*
137        // 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).
138        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
139        {
140            defBoundarySound_->play(); //play boundary sound
141            // Its velocity in z-direction is inverted (i.e. it bounces off).
142            velocity.z = -velocity.z;
143            // And its position is set as to not overstep the boundary it has just crossed.
144            if (position.z > this->fieldHeight_ / 2)
145                position.z = this->fieldHeight_ / 2;
146            if (position.z < -this->fieldHeight_ / 2)
147                position.z = -this->fieldHeight_ / 2;
148
149            this->fireEvent();
150        }
151
152        // 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).
153        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
154        {
155            float distance = 0;
156
157            if (this->bat_ != NULL) // If there are bats.
158            {
159                // If the right boundary has been crossed.
160                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
161                {
162                    // 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%)
163                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
164                    if (fabs(distance) <= 1) // If the bat is there to parry.
165                    {
166                        defBatSound_->play(); //play bat sound
167                        // Set the ball to be exactly at the boundary.
168                        position.x = this->fieldWidth_ / 2;
169                        // Invert its velocity in x-direction (i.e. it bounces off).
170                        velocity.x = -velocity.x;
171                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
172                        velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_;
173                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
174
175                        this->fireEvent();
176                    }
177                    // If the left player scores.
178                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
179                    {
180                        defScoreSound_->play();//play score sound
181                        if (this->getGametype() && this->bat_[0])
182                        {
183                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
184                            return;
185                        }
186                    }
187                }
188                // If the left boundary has been crossed.
189                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
190                {
191                    // 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%)
192                    distance = (position.z - this->figure_->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
193                    if (fabs(distance) <= 1) // If the bat is there to parry.
194                    {
195                        defBatSound_->play(); //play bat sound
196                        // Set the ball to be exactly at the boundary.
197                        position.x = -this->fieldWidth_ / 2;
198                        // Invert its velocity in x-direction (i.e. it bounces off).
199                        velocity.x = -velocity.x;
200                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
201                        velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_;
202                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
203
204                        this->fireEvent();
205                    }
206                    // If the right player scores.
207                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
208                    {
209                        defScoreSound_->play();//play score sound
210                        if (this->getGametype() && this->bat_[1])
211                        {
212                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
213                            return;
214                        }
215                    }
216                }
217            }
218        }
219        */
220    }
221
222    /**
223    @brief
224        Set the bats for the ball.
225    @param bats
226        An array (of size 2) of weak pointers, to be set as the new bats.
227    */
228    void JumpPlatform::setFigure(WeakPtr<JumpFigure> newFigure)
229    {
230        figure_ = newFigure;
231    }
232
233    void JumpPlatform::accelerateFigure()
234    {
235        figure_->JumpFromPlatform(this);
236    }
237
238    void JumpPlatform::touchFigure()
239    {
240
241    }
242
243    void JumpPlatform::setDefScoreSound(const std::string &jumpSound)
244    {
245        if( defScoreSound_ )
246            defScoreSound_->setSource(jumpSound);
247        else
248            assert(0); // This should never happen, because soundpointer is only available on master
249    }
250
251    const std::string& JumpPlatform::getDefScoreSound()
252    {
253        if( defScoreSound_ )
254            return defScoreSound_->getSource();
255        else
256            assert(0);
257        return BLANKSTRING;
258    }
259
260    void JumpPlatform::setDefBatSound(const std::string &jumpSound)
261    {
262        if( defBatSound_ )
263            defBatSound_->setSource(jumpSound);
264        else
265            assert(0); // This should never happen, because soundpointer is only available on master
266    }
267
268    const std::string& JumpPlatform::getDefBatSound()
269    {
270        if( defBatSound_ )
271            return defBatSound_->getSource();
272        else
273            assert(0);
274        return BLANKSTRING;
275    }
276
277    void JumpPlatform::setDefBoundarySound(const std::string &jumpSound)
278    {
279        if( defBoundarySound_ )
280            defBoundarySound_->setSource(jumpSound);
281        else
282            assert(0); // This should never happen, because soundpointer is only available on master
283    }
284
285    const std::string& JumpPlatform::getDefBoundarySound()
286    {
287        if( defBoundarySound_ )
288            return defBoundarySound_->getSource();
289        else
290            assert(0);
291        return BLANKSTRING;
292    }
293}
Note: See TracBrowser for help on using the repository browser.