Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/pong/PongBall.cc @ 5889

Last change on this file since 5889 was 5889, checked in by landauf, 15 years ago

Using named events in Pong. Also firing events in PongBall now.

  • Property svn:eol-style set to native
File size: 7.2 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#include "PongBall.h"
30
31#include "core/CoreIncludes.h"
32#include "core/GameMode.h"
33#include "gametypes/Gametype.h"
34#include "PongBat.h"
35#include "sound/SoundBase.h"
36
37namespace orxonox
38{
39    CreateFactory(PongBall);
40
41    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
42
43    PongBall::PongBall(BaseObject* creator)
44        : MovableEntity(creator)
45        , sidesound_(NULL)
46        , batsound_(NULL)
47        , scoresound_(NULL)
48    {
49        RegisterObject(PongBall);
50
51        this->speed_ = 0;
52        this->bat_ = 0;
53        this->batID_ = new unsigned int[2];
54        this->batID_[0] = OBJECTID_UNKNOWN;
55        this->batID_[1] = OBJECTID_UNKNOWN;
56        this->relMercyOffset_ = 0.05f;
57
58        this->registerVariables();
59
60        if (GameMode::playsSound())
61        {
62            this->sidesound_ = new SoundBase(this);
63            this->sidesound_->loadFile("sounds/pong_side.wav");
64
65            this->batsound_ = new SoundBase(this);
66            this->batsound_->loadFile("sounds/pong_bat.wav");
67
68            this->scoresound_ = new SoundBase(this);
69            this->scoresound_->loadFile("sounds/pong_score.wav");
70        }
71    }
72
73    PongBall::~PongBall()
74    {
75        if (this->sidesound_)
76            delete this->sidesound_;
77        if (this->batsound_)
78            delete this->batsound_;
79        if (this->scoresound_)
80            delete this->scoresound_;
81    }
82
83    void PongBall::registerVariables()
84    {
85        registerVariable( this->fieldWidth_ );
86        registerVariable( this->fieldHeight_ );
87        registerVariable( this->batlength_ );
88        registerVariable( this->speed_ );
89        registerVariable( this->relMercyOffset_ );
90        registerVariable( this->batID_[0] );
91        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
92    }
93
94    void PongBall::tick(float dt)
95    {
96        SUPER(PongBall, tick, dt);
97
98        Vector3 position = this->getPosition();
99        Vector3 velocity = this->getVelocity();
100
101        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
102        {
103            velocity.z = -velocity.z;
104            if (position.z > this->fieldHeight_ / 2)
105                position.z = this->fieldHeight_ / 2;
106            if (position.z < -this->fieldHeight_ / 2)
107                position.z = -this->fieldHeight_ / 2;
108
109            this->fireEvent();
110            if (GameMode::playsSound())
111                this->sidesound_->play();
112        }
113
114        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
115        {
116            float distance = 0;
117
118            if (this->bat_)
119            {
120                if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
121                {
122                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
123                    if (fabs(distance) <= 1)
124                    {
125                        position.x = this->fieldWidth_ / 2;
126                        velocity.x = -velocity.x;
127                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
128                       
129                        this->fireEvent();
130                        if (GameMode::playsSound())
131                            this->batsound_->play();
132                    }
133                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
134                    {
135                        if (this->getGametype() && this->bat_[0])
136                        {
137                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
138                            if (GameMode::playsSound())
139                                this->scoresound_->play();
140                            return;
141                        }
142                    }
143                }
144                if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
145                {
146                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
147                    if (fabs(distance) <= 1)
148                    {
149                        position.x = -this->fieldWidth_ / 2;
150                        velocity.x = -velocity.x;
151                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
152
153                        this->fireEvent();
154                        if (GameMode::playsSound())
155                            this->batsound_->play();
156                    }
157                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
158                    {
159                        if (this->getGametype() && this->bat_[1])
160                        {
161                            if (GameMode::playsSound())
162                                this->scoresound_->play();
163                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
164                            return;
165                        }
166                    }
167                }
168            }
169        }
170
171        if (velocity != this->getVelocity())
172            this->setVelocity(velocity);
173        if (position != this->getPosition())
174            this->setPosition(position);
175    }
176
177    void PongBall::setSpeed(float speed)
178    {
179        if (speed != this->speed_)
180        {
181            this->speed_ = speed;
182
183            Vector3 velocity = this->getVelocity();
184            if (velocity.x != 0)
185                velocity.x = sgn(velocity.x) * this->speed_;
186            else
187                velocity.x = this->speed_ * sgn(rnd(-1,1));
188
189            this->setVelocity(velocity);
190        }
191    }
192
193    void PongBall::setBats(PongBat** bats)
194    {
195        this->bat_ = bats;
196        this->batID_[0] = this->bat_[0]->getObjectID();
197        this->batID_[1] = this->bat_[1]->getObjectID();
198    }
199
200    void PongBall::applyBats()
201    {
202        if (!this->bat_)
203            this->bat_ = new PongBat*[2];
204        if (this->batID_[0] != OBJECTID_UNKNOWN)
205            this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
206        if (this->batID_[1] != OBJECTID_UNKNOWN)
207            this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
208    }
209}
Note: See TracBrowser for help on using the repository browser.