Minor refactoring to move more code into KartGFX.

This commit is contained in:
hiker 2015-06-30 16:59:48 +10:00
parent 1bafdf528f
commit 42114c163c
3 changed files with 46 additions and 24 deletions

View File

@ -2538,36 +2538,25 @@ void Kart::updateGraphics(float dt, const Vec3& offset_xyz,
// Upate particle effects (creation rate, and emitter size
// depending on speed)
// --------------------------------------------------------
float nitro_frac = 0;
if ( (m_controls.m_nitro || m_min_nitro_time > 0.0f) &&
isOnGround() && m_collected_energy > 0 )
{
// fabs(speed) is important, otherwise the negative number will
// become a huge unsigned number in the particle scene node!
float f = fabsf(getSpeed())/(m_kart_properties->getMaxSpeed() *
m_difficulty->getMaxSpeed());
nitro_frac = fabsf(getSpeed())/(m_kart_properties->getMaxSpeed() *
m_difficulty->getMaxSpeed() );
// The speed of the kart can be higher (due to powerups) than
// the normal maximum speed of the kart.
if(f>1.0f) f = 1.0f;
m_kart_gfx->setCreationRateRelative(KartGFX::KGFX_NITRO1, f);
m_kart_gfx->setCreationRateRelative(KartGFX::KGFX_NITRO2, f);
m_kart_gfx->setCreationRateRelative(KartGFX::KGFX_NITROSMOKE1, f);
m_kart_gfx->setCreationRateRelative(KartGFX::KGFX_NITROSMOKE2, f);
if(nitro_frac>1.0f) nitro_frac = 1.0f;
m_nitro_light->setVisible(true);
}
else
{
m_kart_gfx->setCreationRateAbsolute(KartGFX::KGFX_NITRO1, 0);
m_kart_gfx->setCreationRateAbsolute(KartGFX::KGFX_NITRO2, 0);
m_kart_gfx->setCreationRateAbsolute(KartGFX::KGFX_NITROSMOKE1, 0);
m_kart_gfx->setCreationRateAbsolute(KartGFX::KGFX_NITROSMOKE2, 0);
m_nitro_light->setVisible(false);
}
m_kart_gfx->resizeBox(KartGFX::KGFX_NITRO1, getSpeed(), dt);
m_kart_gfx->resizeBox(KartGFX::KGFX_NITRO2, getSpeed(), dt);
m_kart_gfx->resizeBox(KartGFX::KGFX_NITROSMOKE1, getSpeed(), dt);
m_kart_gfx->resizeBox(KartGFX::KGFX_NITROSMOKE2, getSpeed(), dt);
m_kart_gfx->resizeBox(KartGFX::KGFX_ZIPPER, getSpeed(), dt);
// speed * dt is the new size of the box in which particles start
m_kart_gfx->updateNitroGraphics(nitro_frac, getSpeed()*dt);
// Handle leaning of karts
// -----------------------

View File

@ -243,13 +243,12 @@ void KartGFX::setCreationRateRelative(KartGFXType type, float f)
* we want the particles to be emitted in a smooth, continuous flame and not
* in blobs.
* \param type The particle effect for which to resize the emitting box.
* \param speed Current speed of the kart.
* \param dt Time step size.
* \param new_size New size of the box, typically speed*dt.
*/
void KartGFX::resizeBox(KartGFXType type, float speed, float dt)
void KartGFX::resizeBox(KartGFXType type, float new_size)
{
if(m_all_emitters[type])
m_all_emitters[type]->resizeBox(std::max(0.25f, speed*dt));
m_all_emitters[type]->resizeBox(std::max(0.25f, new_size));
} // resizeBox
// ----------------------------------------------------------------------------
@ -304,11 +303,44 @@ void KartGFX::update(float dt)
{
m_wheel_toggle = 1 - m_wheel_toggle;
for(unsigned int i=0; i<m_all_emitters.size(); i++)
for (unsigned int i = 0; i < m_all_emitters.size(); i++)
{
if(m_all_emitters[i])
if (m_all_emitters[i])
m_all_emitters[i]->update(dt);
}
} // update
// ----------------------------------------------------------------------------
/** Updates nitro dependent particle effects (and box sizes).
* \param nitro_frac Nitro fraction/
* \param new_size New size of the box in which new particles are emitted.
*/
void KartGFX::updateNitroGraphics(float nitro_frac, float new_size)
{
// Upate particle effects (creation rate, and emitter size
// depending on speed)
// --------------------------------------------------------
if (nitro_frac > 0)
{
setCreationRateRelative(KartGFX::KGFX_NITRO1, nitro_frac);
setCreationRateRelative(KartGFX::KGFX_NITRO2, nitro_frac);
setCreationRateRelative(KartGFX::KGFX_NITROSMOKE1, nitro_frac);
setCreationRateRelative(KartGFX::KGFX_NITROSMOKE2, nitro_frac);
}
else
{
setCreationRateAbsolute(KartGFX::KGFX_NITRO1, 0);
setCreationRateAbsolute(KartGFX::KGFX_NITRO2, 0);
setCreationRateAbsolute(KartGFX::KGFX_NITROSMOKE1, 0);
setCreationRateAbsolute(KartGFX::KGFX_NITROSMOKE2, 0);
}
resizeBox(KartGFX::KGFX_NITRO1, new_size);
resizeBox(KartGFX::KGFX_NITRO2, new_size);
resizeBox(KartGFX::KGFX_NITROSMOKE1, new_size);
resizeBox(KartGFX::KGFX_NITROSMOKE2, new_size);
resizeBox(KartGFX::KGFX_ZIPPER, new_size);
} // updateGraphics

View File

@ -71,6 +71,7 @@ private:
void addEffect(KartGFXType type, const std::string &file_name,
const Vec3 &position, bool important);
void resizeBox(const KartGFXType type, float new_size);
public:
KartGFX(const AbstractKart *kart);
@ -81,8 +82,8 @@ public:
void setXYZ(const KartGFXType type, const Vec3 &xyz);
void setCreationRateAbsolute(const KartGFXType type, float f);
void setCreationRateRelative(const KartGFXType type, float f);
void resizeBox(const KartGFXType type, float speed, float dt);
void updateTerrain(const ParticleKind *pk);
void update(float dt);
void updateNitroGraphics(float f, float new_size);
}; // KartWGFX
#endif