Restore rubber band with hit kart and reserve mode included

This commit is contained in:
Benau 2018-09-28 17:05:17 +08:00
parent 689fab6bc8
commit ae4ed9e2e7
3 changed files with 42 additions and 6 deletions

View File

@ -256,7 +256,7 @@ BareNetworkString* Plunger::saveState(std::vector<std::string>* ru)
BareNetworkString* buffer = Flyable::saveState(ru); BareNetworkString* buffer = Flyable::saveState(ru);
buffer->addUInt16(m_keep_alive).addUInt8(m_moved_to_infinity ? 1 : 0); buffer->addUInt16(m_keep_alive).addUInt8(m_moved_to_infinity ? 1 : 0);
if (m_rubber_band) if (m_rubber_band)
buffer->addUInt8(m_rubber_band->getRubberBandTo()); buffer->addUInt8(m_rubber_band->get8BitState());
else else
buffer->addUInt8(255); buffer->addUInt8(255);
return buffer; return buffer;
@ -268,7 +268,20 @@ void Plunger::restoreState(BareNetworkString *buffer, int count)
Flyable::restoreState(buffer, count); Flyable::restoreState(buffer, count);
m_keep_alive = buffer->getUInt16(); m_keep_alive = buffer->getUInt16();
m_moved_to_infinity = buffer->getUInt8() == 1; m_moved_to_infinity = buffer->getUInt8() == 1;
int8_t rbt = buffer->getUInt8(); uint8_t bit_state = buffer->getUInt8();
if (rbt != -1 && m_rubber_band) if (bit_state == 255 && m_rubber_band)
m_rubber_band->setRubberBandTo((RubberBand::RubberBandTo)rbt); {
delete m_rubber_band;
m_rubber_band = NULL;
if (!m_reverse_mode)
m_reverse_mode = true;
}
else if (bit_state != 255 && !m_rubber_band)
{
m_rubber_band = new RubberBand(this, m_owner);
if (m_reverse_mode)
m_reverse_mode = false;
}
if (bit_state != 255)
m_rubber_band->set8BitState(bit_state);
} // restoreState } // restoreState

View File

@ -45,6 +45,7 @@
RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart) RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
: m_plunger(plunger), m_owner(kart) : m_plunger(plunger), m_owner(kart)
{ {
m_hit_kart = NULL;
m_attached_state = RB_TO_PLUNGER; m_attached_state = RB_TO_PLUNGER;
updatePosition(); updatePosition();
@ -276,6 +277,7 @@ void RubberBand::hit(AbstractKart *kart_hit, const Vec3 *track_xyz)
// ================= // =================
m_hit_position = *track_xyz; m_hit_position = *track_xyz;
m_attached_state = RB_TO_TRACK; m_attached_state = RB_TO_TRACK;
m_hit_kart = NULL;
} // hit } // hit
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -289,3 +291,24 @@ void RubberBand::remove()
} }
#endif #endif
} // remove } // remove
// ----------------------------------------------------------------------------
uint8_t RubberBand::get8BitState() const
{
uint8_t state = (uint8_t)(m_attached_state & 3);
state |= m_attached_state == RB_TO_KART && m_hit_kart ?
(m_hit_kart->getWorldKartId() << 3) : 0;
return state;
} // get8BitState
// ----------------------------------------------------------------------------
void RubberBand::set8BitState(uint8_t bit_state)
{
m_hit_kart = NULL;
m_attached_state = (RubberBandTo)(bit_state & 3);
if (m_attached_state == RB_TO_KART)
{
unsigned kart = bit_state >> 3;
m_hit_kart = World::getWorld()->getKart(kart);
}
} // set8BitState

View File

@ -73,8 +73,8 @@ public:
void updateGraphics(float dt); void updateGraphics(float dt);
void update(int ticks); void update(int ticks);
void hit(AbstractKart *kart_hit, const Vec3 *track_xyz=NULL); void hit(AbstractKart *kart_hit, const Vec3 *track_xyz=NULL);
RubberBandTo getRubberBandTo() const { return m_attached_state; } uint8_t get8BitState() const;
void setRubberBandTo(RubberBandTo rbt) { m_attached_state = rbt; } void set8BitState(uint8_t bit_state);
void remove(); void remove();
}; // RubberBand }; // RubberBand
#endif #endif