Allow recreation of kart animation from state if the required creation event missed

This commit is contained in:
Benau 2018-09-29 00:21:25 +08:00
parent addcb84802
commit 5041c6585d
8 changed files with 75 additions and 18 deletions

View File

@ -30,6 +30,14 @@
class AbstractKart;
enum KartAnimationType : uint8_t
{
KAT_RESCUE = 0,
KAT_EXPLOSION_DIRECT_HIT = 1,
KAT_EXPLOSION = 2,
KAT_CANNON = 3
};
/** The base class for all kart animation, like rescue, explosion, or cannon.
* Kart animations are done by removing the physics body from the physics
* world, and instead modifying the rotation and position of the kart
@ -94,10 +102,12 @@ public:
m_end_transform = t;
m_end_ticks = ticks;
}
// ----------------------------------------------------------------------------
void checkNetworkAnimationCreationSucceed(const btTransform& fallback_trans);
// ----------------------------------------------------------------------------
// ------------------------------------------------------------------------
void checkNetworkAnimationCreationSucceed(const btTransform& fb_trans);
// ------------------------------------------------------------------------
int getEndTicks() const { return m_end_ticks; }
// ------------------------------------------------------------------------
virtual KartAnimationType getAnimationType() const = 0;
}; // AbstractKartAnimation
#endif

View File

@ -81,6 +81,8 @@ public:
virtual ~CannonAnimation();
virtual void update(int ticks);
// ------------------------------------------------------------------------
virtual bool usePredefinedEndTransform() const { return false; }
virtual bool usePredefinedEndTransform() const { return false; }
// ------------------------------------------------------------------------
virtual KartAnimationType getAnimationType() const { return KAT_CANNON; }
}; // CannonAnimation
#endif

View File

@ -72,7 +72,7 @@ ExplosionAnimation *ExplosionAnimation::create(AbstractKart *kart)
// ----------------------------------------------------------------------------
ExplosionAnimation::ExplosionAnimation(AbstractKart *kart,
const Vec3 &explosion_position,
bool direct_hit)
bool direct_hit, bool from_state)
: AbstractKartAnimation(kart, "ExplosionAnimation")
{
m_direct_hit = direct_hit;
@ -148,7 +148,8 @@ ExplosionAnimation::ExplosionAnimation(AbstractKart *kart,
m_kart->getAttachment()->clear();
// Clear powerups when direct hit in CTF
addNetworkAnimationChecker(m_reset_ticks != -1);
if (!from_state)
addNetworkAnimationChecker(m_reset_ticks != -1);
} // ExplosionAnimation
//-----------------------------------------------------------------------------

View File

@ -74,9 +74,9 @@ protected:
Vec3 m_reset_xyz, m_reset_normal;
ExplosionAnimation(AbstractKart *kart);
ExplosionAnimation(AbstractKart *kart, const Vec3 &pos,
bool direct_hit);
public:
ExplosionAnimation(AbstractKart *kart, const Vec3 &pos,
bool direct_hit, bool from_state = false);
static ExplosionAnimation *create(AbstractKart *kart, const Vec3 &pos,
bool direct_hit);
static ExplosionAnimation *create(AbstractKart *kart);
@ -85,5 +85,8 @@ public:
virtual void update(int ticks);
bool hasResetAlready() const
{ return m_reset_ticks != -1 && m_timer < m_reset_ticks; }
// ------------------------------------------------------------------------
virtual KartAnimationType getAnimationType() const
{ return m_direct_hit ? KAT_EXPLOSION_DIRECT_HIT : KAT_EXPLOSION; }
}; // ExplosionAnimation
#endif

View File

@ -21,7 +21,8 @@
#include "items/attachment.hpp"
#include "items/powerup.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/abstract_kart_animation.hpp"
#include "karts/explosion_animation.hpp"
#include "karts/rescue_animation.hpp"
#include "karts/controller/player_controller.hpp"
#include "karts/kart_properties.hpp"
#include "karts/max_speed.hpp"
@ -52,6 +53,7 @@ KartRewinder::KartRewinder(const std::string& ident,
*/
void KartRewinder::reset()
{
m_last_animation_end_ticks = -1;
m_transfrom_from_network =
btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
Kart::reset();
@ -138,7 +140,9 @@ BareNetworkString* KartRewinder::saveState(std::vector<std::string>* ru)
buffer->add(trans.getOrigin());
btQuaternion quat = trans.getRotation();
buffer->add(quat);
buffer->addUInt32(ka->getEndTicks());
unsigned et = ka->getEndTicks() & 134217727;
et |= ka->getAnimationType() << 27;
buffer->addUInt32(et);
}
else
{
@ -207,10 +211,38 @@ void KartRewinder::restoreState(BareNetworkString *buffer, int count)
if (has_animation)
{
int end_ticks = buffer->getUInt32();
AbstractKartAnimation* ka = getKartAnimation();
if (ka)
ka->setEndTransformTicks(m_transfrom_from_network, end_ticks);
unsigned et = buffer->getUInt32();
int end_ticks = et & 134217727;
KartAnimationType kat = (KartAnimationType)(et >> 27);
if (!getKartAnimation() && end_ticks != m_last_animation_end_ticks)
{
Log::info("KartRewinder", "Creating animation %d from state", kat);
switch (kat)
{
case KAT_RESCUE:
new RescueAnimation(this, false/*is_auto_rescue*/,
true/*from_state*/);
break;
case KAT_EXPLOSION_DIRECT_HIT:
new ExplosionAnimation(this, getSmoothedXYZ(),
true/*direct_hit*/, true/*from_state*/);
break;
case KAT_EXPLOSION:
new ExplosionAnimation(this, getSmoothedXYZ(),
false/*direct_hit*/, true/*from_state*/);
break;
default:
Log::warn("KartRewinder", "Unknown animation %d from state",
kat);
break;
}
}
if (getKartAnimation())
{
getKartAnimation()->setEndTransformTicks(m_transfrom_from_network,
end_ticks);
}
m_last_animation_end_ticks = end_ticks;
}
Vec3 lv = buffer->getVec3();

View File

@ -33,6 +33,7 @@ private:
btTransform m_transfrom_from_network;
float m_prev_steering, m_steering_smoothing_dt, m_steering_smoothing_time;
int m_last_animation_end_ticks;
public:
KartRewinder(const std::string& ident, unsigned int world_kart_id,
int position, const btTransform& init_transform,

View File

@ -40,7 +40,8 @@
* and initialised the timer.
* \param kart Pointer to the kart which is animated.
*/
RescueAnimation::RescueAnimation(AbstractKart *kart, bool is_auto_rescue)
RescueAnimation::RescueAnimation(AbstractKart *kart, bool is_auto_rescue,
bool from_state)
: AbstractKartAnimation(kart, "RescueAnimation")
{
btTransform prev_trans = kart->getTrans();
@ -89,8 +90,11 @@ RescueAnimation::RescueAnimation(AbstractKart *kart, bool is_auto_rescue)
}
// Clear powerups when rescue in CTF
addNetworkAnimationChecker(race_manager->getMajorMode() ==
RaceManager::MAJOR_MODE_CAPTURE_THE_FLAG);
if (!from_state)
{
addNetworkAnimationChecker(race_manager->getMajorMode() ==
RaceManager::MAJOR_MODE_CAPTURE_THE_FLAG);
}
} // RescueAnimation
//-----------------------------------------------------------------------------

View File

@ -49,9 +49,13 @@ protected:
Referee *m_referee;
public:
RescueAnimation(AbstractKart *kart, bool is_auto_rescue=false);
RescueAnimation(AbstractKart *kart,
bool is_auto_rescue = false,
bool from_state = false);
float maximumHeight();
virtual ~RescueAnimation();
virtual void update(int ticks);
// ------------------------------------------------------------------------
virtual KartAnimationType getAnimationType() const { return KAT_RESCUE; }
}; // RescueAnimation
#endif