Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/miniprojects/src/orxonox/objects/worldentities/PongBall.cc @ 2825

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

eol-style:native

  • Property svn:eol-style set to native
File size: 4.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 "OrxonoxStableHeaders.h"
30#include "PongBall.h"
31
32#include "core/CoreIncludes.h"
33#include "objects/worldentities/PongBat.h"
34#include "objects/gametypes/Gametype.h"
35
36namespace orxonox
37{
38    CreateFactory(PongBall);
39
40    PongBall::PongBall(BaseObject* creator) : MovableEntity(creator)
41    {
42        RegisterObject(PongBall);
43
44        this->speed_ = 0;
45        this->bat_ = 0;
46    }
47
48    void PongBall::tick(float dt)
49    {
50        SUPER(PongBall, tick, dt);
51
52        if (Core::isMaster())
53        {
54            Vector3 position = this->getPosition();
55            Vector3 velocity = this->getVelocity();
56
57            if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
58            {
59                velocity.z = -velocity.z;
60
61                if (position.z > this->fieldHeight_ / 2)
62                    position.z = this->fieldHeight_ / 2;
63                if (position.z < -this->fieldHeight_ / 2)
64                    position.z = -this->fieldHeight_ / 2;
65            }
66
67            if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
68            {
69                velocity.x = -velocity.x;
70                float distance = 0;
71
72                if (position.x > this->fieldWidth_ / 2)
73                {
74                    position.x = this->fieldWidth_ / 2;
75                    if (this->bat_ && this->bat_[1])
76                    {
77                        distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * this->batlength_ / 2);
78                        if (this->getGametype() && this->bat_[0] && fabs(distance) > 1)
79                        {
80                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
81                            return;
82                        }
83                    }
84                }
85                if (position.x < -this->fieldWidth_ / 2)
86                {
87                    position.x = -this->fieldWidth_ / 2;
88                    if (this->bat_ && this->bat_[0])
89                    {
90                        distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * this->batlength_ / 2);
91                        if (this->getGametype() && this->bat_[1] && fabs(distance) > 1)
92                        {
93                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
94                            return;
95                        }
96                    }
97                }
98
99                velocity.z = distance * distance * sgn(distance) * 1.5 * this->speed_;
100            }
101
102            if (velocity != this->getVelocity())
103                this->setVelocity(velocity);
104            if (position != this->getPosition())
105                this->setPosition(position);
106        }
107    }
108
109    void PongBall::setSpeed(float speed)
110    {
111        if (speed != this->speed_)
112        {
113            this->speed_ = speed;
114
115            Vector3 velocity = this->getVelocity();
116            if (velocity.x != 0)
117                velocity.x = sgn(velocity.x) * this->speed_;
118            else
119                velocity.x = this->speed_ * sgn(rnd(-1,1));
120
121            this->setVelocity(velocity);
122        }
123    }
124}
Note: See TracBrowser for help on using the repository browser.