forked from supertuxkart/stk-code
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbowling.cpp
253 lines (228 loc) · 8.42 KB
/
bowling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2007-2015 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "items/bowling.hpp"
#include "audio/sfx_base.hpp"
#include "audio/sfx_manager.hpp"
#include "graphics/hit_sfx.hpp"
#include "graphics/material.hpp"
#include "io/xml_node.hpp"
#include "karts/abstract_kart.hpp"
#include "guiengine/engine.hpp"
#include "config/stk_config.hpp"
// #include "modes/linear_world.hpp"
#include "utils/hit_processor.hpp"
#include "utils/log.hpp" //TODO: remove after debugging is done
float Bowling::m_st_max_distance; // maximum distance for a bowling ball to be attracted
float Bowling::m_st_max_distance_squared;
float Bowling::m_st_force_to_target;
// -----------------------------------------------------------------------------
Bowling::Bowling(AbstractKart *kart)
: Flyable(kart, PowerupManager::POWERUP_BOWLING, 50.0f /* mass */)
{
m_has_hit_kart = false;
m_roll_sfx = SFXManager::get()->createSoundSource("bowling_roll");
fixSFXSplitscreen(m_roll_sfx);
m_roll_sfx->play();
m_roll_sfx->setLoop(true);
} // Bowling
// ----------------------------------------------------------------------------
/** Destructor, removes any playing sfx.
*/
Bowling::~Bowling()
{
// This will stop the sfx and delete the object.
removeRollSfx();
} // ~RubberBall
// -----------------------------------------------------------------------------
/** Initialises this object with data from the power.xml file.
* \param node XML Node
* \param bowling The bowling ball mesh
*/
void Bowling::init(const XMLNode &node, scene::IMesh *bowling)
{
Flyable::init(node, bowling, PowerupManager::POWERUP_BOWLING);
m_st_max_distance = 20.0f;
m_st_max_distance_squared = 20.0f * 20.0f;
m_st_force_to_target = 10.0f;
node.get("max-distance", &m_st_max_distance );
m_st_max_distance_squared = m_st_max_distance*m_st_max_distance;
node.get("force-to-target", &m_st_force_to_target);
} // init
// ----------------------------------------------------------------------------
/** Updates the bowling ball ineach frame. If this function returns true, the
* object will be removed by the projectile manager.
* \param dt Time step size.
* \returns True of this object should be removed.
*/
bool Bowling::updateAndDelete(int ticks)
{
bool can_be_deleted = Flyable::updateAndDelete(ticks);
if (can_be_deleted)
{
removeRollSfx();
return true;
}
const AbstractKart *kart=0;
Vec3 direction;
float minDistance;
getClosestKart(&kart, &minDistance, &direction);
if(kart && minDistance<m_st_max_distance_squared) // move bowling towards kart
{
// limit angle, so that the bowling ball does not turn
// around to hit a kart behind
if(fabs(m_body->getLinearVelocity().angle(direction)) < 1.3)
{
direction*=1/direction.length()*m_st_force_to_target;
m_body->applyCentralForce(direction);
}
}
// Bowling balls lose energy (e.g. when hitting the track), so increase
// the speed if the ball is too slow, but only if it's not too high (if
// the ball is too high, it is 'pushed down', which can reduce the
// speed, which causes the speed to increase, which in turn causes
// the ball to fly higher and higher.
//btTransform trans = getTrans();
float hat = (getXYZ() - getHitPoint()).length();
if(hat-0.5f*m_extend.getY()<0.01f)
{
const Material *material = getMaterial();
if(!material || material->isDriveReset())
{
hit(NULL);
removeRollSfx();
return true;
}
}
btVector3 v = m_body->getLinearVelocity();
float vlen = v.length2();
if (hat<= m_max_height)
{
if(vlen<0.8*m_speed*m_speed)
{ // bowling lost energy (less than 80%), i.e. it's too slow - speed it up:
if(vlen==0.0f) {
v = btVector3(.5f, .0, 0.5f); // avoid 0 div.
}
// m_body->setLinearVelocity(v*(m_speed/sqrt(vlen)));
} // vlen < 0.8*m_speed*m_speed
} // hat< m_max_height
if(vlen<0.1)
{
hit(NULL);
removeRollSfx();
return true;
}
if (m_roll_sfx && m_roll_sfx->getStatus()==SFXBase::SFX_PLAYING)
m_roll_sfx->setPosition(getXYZ());
return false;
} // updateAndDelete
// -----------------------------------------------------------------------------
/** Callback from the physics in case that a kart or physical object is hit.
* The bowling ball triggers an explosion when hit.
* \param kart The kart hit (NULL if no kart was hit).
* \param object The object that was hit (NULL if none).
* \returns True if there was actually a hit (i.e. not owner, and target is
* not immune), false otherwise.
*/
bool Bowling::hit(AbstractKart* kart, PhysicalObject* obj)
{
auto hp = kart->getHitProcessor();
if (hp)
hp->setTeammateHitOwner(getOwnerId(),m_ticks_since_thrown);
bool was_real_hit = Flyable::hit(kart, obj);
if (was_real_hit)
{
if (kart && kart->isShielded())
{
kart->decreaseShieldTime();
if (hp)
{
hp->registerTeammateHit(kart->getWorldKartId());
hp->handleTeammateHits();
}
return true;
}
else
{
m_has_hit_kart = kart != NULL;
explode(kart, obj, /*hit_secondary*/false);
}
}
if (hp)
hp->handleTeammateHits();
return was_real_hit;
} // hit
// ----------------------------------------------------------------------------
void Bowling::removeRollSfx()
{
if (m_roll_sfx)
{
m_roll_sfx->deleteSFX();
m_roll_sfx = NULL;
}
} // removeRollSfx
// ----------------------------------------------------------------------------
/** Returns the hit effect object to use when this objects hits something.
* \returns The hit effect object, or NULL if no hit effect should be played.
*/
HitEffect* Bowling::getHitEffect() const
{
if (GUIEngine::isNoGraphics())
return NULL;
if (m_deleted_once)
return NULL;
if(m_has_hit_kart)
return new HitSFX(getXYZ(), "strike");
else
return new HitSFX(getXYZ(), "crash");
} // getHitEffect
// ----------------------------------------------------------------------------
void Bowling::onFireFlyable()
{
Flyable::onFireFlyable();
m_has_hit_kart = false;
float y_offset = 0.5f*m_owner->getKartLength() + m_extend.getZ()*0.5f;
// if the kart is looking backwards, release from the back
if( m_owner->getControls().getLookBack())
{
y_offset = -y_offset;
m_speed = -m_speed*2;
}
else
{
float min_speed = m_speed*4.0f;
/* make it go faster when throwing forward
so the player doesn't catch up with the ball
and explode by touching it */
m_speed = m_owner->getSpeed() + m_speed;
if(m_speed < min_speed) m_speed = min_speed;
}
const Vec3& normal = m_owner->getNormal();
createPhysics(y_offset, btVector3(0.0f, 0.0f, m_speed*2),
new btSphereShape(0.5f*m_extend.getY()),
0.4f /*restitution*/,
-70.0f*normal /*gravity*/,
true /*rotates*/);
// Even if the ball is fired backwards, m_speed must be positive,
// otherwise the ball can start to vibrate when energy is added.
m_speed = fabsf(m_speed);
// Do not adjust the up velociy depending on height above terrain, since
// this would disable gravity.
setAdjustUpVelocity(false);
// should not live forever, auto-destruct after 20 seconds
m_max_lifespan = stk_config->time2Ticks(20);
} // onFireFlyable