Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5892 was 5892, checked in by rgrieder, 15 years ago

Removed hard coded sound in Level, Engine and PongBall and replaced SoundMainMenu with a simple SoundBase.

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