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 | |
---|
36 | namespace 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 | this->relMercyOffset_ = 0.05; |
---|
47 | } |
---|
48 | |
---|
49 | void PongBall::tick(float dt) |
---|
50 | { |
---|
51 | SUPER(PongBall, tick, dt); |
---|
52 | |
---|
53 | if (Core::isMaster()) |
---|
54 | { |
---|
55 | Vector3 position = this->getPosition(); |
---|
56 | Vector3 velocity = this->getVelocity(); |
---|
57 | |
---|
58 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
59 | { |
---|
60 | velocity.z = -velocity.z; |
---|
61 | |
---|
62 | if (position.z > this->fieldHeight_ / 2) |
---|
63 | position.z = this->fieldHeight_ / 2; |
---|
64 | if (position.z < -this->fieldHeight_ / 2) |
---|
65 | position.z = -this->fieldHeight_ / 2; |
---|
66 | } |
---|
67 | |
---|
68 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
69 | { |
---|
70 | float distance = 0; |
---|
71 | |
---|
72 | if (this->bat_) |
---|
73 | { |
---|
74 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
75 | { |
---|
76 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
77 | if (fabs(distance) <= 1) |
---|
78 | { |
---|
79 | position.x = this->fieldWidth_ / 2; |
---|
80 | velocity.x = -velocity.x; |
---|
81 | velocity.z = distance * distance * sgn(distance) * 1.5 * this->speed_; |
---|
82 | } |
---|
83 | else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
84 | { |
---|
85 | if (this->getGametype() && this->bat_[0]) |
---|
86 | { |
---|
87 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
88 | return; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
93 | { |
---|
94 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
95 | if (fabs(distance) <= 1) |
---|
96 | { |
---|
97 | position.x = -this->fieldWidth_ / 2; |
---|
98 | velocity.x = -velocity.x; |
---|
99 | velocity.z = distance * distance * sgn(distance) * 1.5 * this->speed_; |
---|
100 | } |
---|
101 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
102 | { |
---|
103 | if (this->getGametype() && this->bat_[1]) |
---|
104 | { |
---|
105 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
106 | return; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | if (velocity != this->getVelocity()) |
---|
114 | this->setVelocity(velocity); |
---|
115 | if (position != this->getPosition()) |
---|
116 | this->setPosition(position); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void PongBall::setSpeed(float speed) |
---|
121 | { |
---|
122 | if (speed != this->speed_) |
---|
123 | { |
---|
124 | this->speed_ = speed; |
---|
125 | |
---|
126 | Vector3 velocity = this->getVelocity(); |
---|
127 | if (velocity.x != 0) |
---|
128 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
129 | else |
---|
130 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
131 | |
---|
132 | this->setVelocity(velocity); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|