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);
buffer->addUInt16(m_keep_alive).addUInt8(m_moved_to_infinity ? 1 : 0);
if (m_rubber_band)
buffer->addUInt8(m_rubber_band->getRubberBandTo());
buffer->addUInt8(m_rubber_band->get8BitState());
else
buffer->addUInt8(255);
return buffer;
@ -268,7 +268,20 @@ void Plunger::restoreState(BareNetworkString *buffer, int count)
Flyable::restoreState(buffer, count);
m_keep_alive = buffer->getUInt16();
m_moved_to_infinity = buffer->getUInt8() == 1;
int8_t rbt = buffer->getUInt8();
if (rbt != -1 && m_rubber_band)
m_rubber_band->setRubberBandTo((RubberBand::RubberBandTo)rbt);
uint8_t bit_state = buffer->getUInt8();
if (bit_state == 255 && m_rubber_band)
{
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

View File

@ -45,6 +45,7 @@
RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
: m_plunger(plunger), m_owner(kart)
{
m_hit_kart = NULL;
m_attached_state = RB_TO_PLUNGER;
updatePosition();
@ -276,6 +277,7 @@ void RubberBand::hit(AbstractKart *kart_hit, const Vec3 *track_xyz)
// =================
m_hit_position = *track_xyz;
m_attached_state = RB_TO_TRACK;
m_hit_kart = NULL;
} // hit
// ----------------------------------------------------------------------------
@ -289,3 +291,24 @@ void RubberBand::remove()
}
#endif
} // 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 update(int ticks);
void hit(AbstractKart *kart_hit, const Vec3 *track_xyz=NULL);
RubberBandTo getRubberBandTo() const { return m_attached_state; }
void setRubberBandTo(RubberBandTo rbt) { m_attached_state = rbt; }
uint8_t get8BitState() const;
void set8BitState(uint8_t bit_state);
void remove();
}; // RubberBand
#endif