Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/modules/pong/PongBall.cc @ 10414

Last change on this file since 10414 was 7886, checked in by landauf, 14 years ago

simplification

  • Property svn:eol-style set to native
File size: 6.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#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->accelerationFactor_ = 1.0f;
49        this->bat_ = 0;
50        this->bDeleteBats_ = false;
51        this->batID_ = new unsigned int[2];
52        this->batID_[0] = OBJECTID_UNKNOWN;
53        this->batID_[1] = OBJECTID_UNKNOWN;
54        this->relMercyOffset_ = 0.05f;
55
56        this->registerVariables();
57    }
58
59    PongBall::~PongBall()
60    {
61        if (this->isInitialized())
62        {
63            if (this->bDeleteBats_)
64                delete[] this->bat_;
65
66            delete[] this->batID_;
67        }
68    }
69
70    void PongBall::registerVariables()
71    {
72        registerVariable( this->fieldWidth_ );
73        registerVariable( this->fieldHeight_ );
74        registerVariable( this->batlength_ );
75        registerVariable( this->speed_ );
76        registerVariable( this->relMercyOffset_ );
77        registerVariable( this->batID_[0] );
78        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
79    }
80
81    void PongBall::tick(float dt)
82    {
83        SUPER(PongBall, tick, dt);
84
85        Vector3 position = this->getPosition();
86        Vector3 velocity = this->getVelocity();
87        Vector3 acceleration = this->getAcceleration();
88
89        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
90        {
91            velocity.z = -velocity.z;
92            if (position.z > this->fieldHeight_ / 2)
93                position.z = this->fieldHeight_ / 2;
94            if (position.z < -this->fieldHeight_ / 2)
95                position.z = -this->fieldHeight_ / 2;
96
97            this->fireEvent();
98        }
99
100        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
101        {
102            float distance = 0;
103
104            if (this->bat_)
105            {
106                if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
107                {
108                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
109                    if (fabs(distance) <= 1)
110                    {
111                        position.x = this->fieldWidth_ / 2;
112                        velocity.x = -velocity.x;
113                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
114                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
115
116                        this->fireEvent();
117                    }
118                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
119                    {
120                        if (this->getGametype() && this->bat_[0])
121                        {
122                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
123                            return;
124                        }
125                    }
126                }
127                if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
128                {
129                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
130                    if (fabs(distance) <= 1)
131                    {
132                        position.x = -this->fieldWidth_ / 2;
133                        velocity.x = -velocity.x;
134                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
135                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
136
137                        this->fireEvent();
138                    }
139                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
140                    {
141                        if (this->getGametype() && this->bat_[1])
142                        {
143                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
144                            return;
145                        }
146                    }
147                }
148            }
149        }
150
151        if (acceleration != this->getAcceleration())
152            this->setAcceleration(acceleration);
153        if (velocity != this->getVelocity())
154            this->setVelocity(velocity);
155        if (position != this->getPosition())
156            this->setPosition(position);
157    }
158
159    void PongBall::setSpeed(float speed)
160    {
161        if (speed != this->speed_)
162        {
163            this->speed_ = speed;
164
165            Vector3 velocity = this->getVelocity();
166            if (velocity.x != 0)
167                velocity.x = sgn(velocity.x) * this->speed_;
168            else
169                velocity.x = this->speed_ * sgn(rnd(-1,1));
170
171            this->setVelocity(velocity);
172        }
173    }
174
175    void PongBall::setBats(WeakPtr<PongBat>* bats)
176    {
177        if (this->bDeleteBats_)
178        {
179            delete[] this->bat_;
180            this->bDeleteBats_ = false;
181        }
182
183        this->bat_ = bats;
184        this->batID_[0] = this->bat_[0]->getObjectID();
185        this->batID_[1] = this->bat_[1]->getObjectID();
186    }
187
188    void PongBall::applyBats()
189    {
190        if (!this->bat_)
191        {
192            this->bat_ = new WeakPtr<PongBat>[2];
193            this->bDeleteBats_ = true;
194        }
195
196        if (this->batID_[0] != OBJECTID_UNKNOWN)
197            this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
198        if (this->batID_[1] != OBJECTID_UNKNOWN)
199            this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
200    }
201}
Note: See TracBrowser for help on using the repository browser.