Save powerup state in kart status.

This commit is contained in:
hiker 2016-08-09 17:46:06 +10:00
parent f4f2f11943
commit 7c598df8d1
3 changed files with 59 additions and 2 deletions

View File

@ -69,6 +69,45 @@ void Powerup::reset()
set( (PowerupManager::PowerupType)type, number ); set( (PowerupManager::PowerupType)type, number );
} // reset } // reset
//-----------------------------------------------------------------------------
/** Save the powerup state. Called from the kart rewinder when saving the kart
* state or when a new powerup even is saved.
* \param buffer The buffer into which to save the state.
*/
void Powerup::saveState(BareNetworkString *buffer)
{
buffer->addUInt8(uint8_t(m_type));
if(m_type!=PowerupManager::POWERUP_NOTHING)
{
buffer->addUInt8(m_number); // number is <=255
}
} // saveState
//-----------------------------------------------------------------------------
/** Restore a powerup state. Called from the kart rewinder when restoring a
* state.
* \param buffer Buffer with the state of this powerup object.
*/
void Powerup::rewindTo(BareNetworkString *buffer)
{
PowerupManager::PowerupType new_type =
PowerupManager::PowerupType(buffer->getUInt8());
int n=0;
if(new_type==PowerupManager::POWERUP_NOTHING)
{
set(new_type, 0);
return;
}
n = buffer->getUInt8();
if(m_type == new_type)
m_number = n;
else
{
m_number = 0;
set(new_type, n);
}
} // rewindTo
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Sets the collected items. The number of items is increased if the same /** Sets the collected items. The number of items is increased if the same
* item is currently collected, otherwise replaces the existing item. It also * item is currently collected, otherwise replaces the existing item. It also
@ -81,9 +120,15 @@ void Powerup::set(PowerupManager::PowerupType type, int n)
if (m_type==type) if (m_type==type)
{ {
m_number+=n; m_number+=n;
// Limit to 255 (save space in network state saving)
if(m_number>255) m_number = 255;
return; return;
} }
m_type=type; m_type=type;
// Limit to 255 (save space in network state saving)
if(n>255) n = 255;
m_number=n; m_number=n;
if(m_sound_use != NULL) if(m_sound_use != NULL)

View File

@ -26,6 +26,7 @@
#include "utils/random_generator.hpp" #include "utils/random_generator.hpp"
class AbstractKart; class AbstractKart;
class BareNetworkString;
class Item; class Item;
class SFXBase; class SFXBase;
@ -56,9 +57,12 @@ public:
void set (PowerupManager::PowerupType _type, int n=1); void set (PowerupManager::PowerupType _type, int n=1);
void reset (); void reset ();
Material* getIcon () const; Material* getIcon () const;
void adjustSound (); void adjustSound ();
void use (); void use ();
void hitBonusBox (const Item &item, int newC=-1); void hitBonusBox (const Item &item, int newC=-1);
void saveState(BareNetworkString *buffer);
void rewindTo(BareNetworkString *buffer);
/** Returns the number of powerups. */ /** Returns the number of powerups. */
int getNum () const {return m_number;} int getNum () const {return m_number;}

View File

@ -19,6 +19,7 @@
#include "karts/kart_rewinder.hpp" #include "karts/kart_rewinder.hpp"
#include "karts/abstract_kart.hpp" #include "karts/abstract_kart.hpp"
#include "items/attachment.hpp" #include "items/attachment.hpp"
#include "items/powerup.hpp"
#include "modes/world.hpp" #include "modes/world.hpp"
#include "network/rewind_manager.hpp" #include "network/rewind_manager.hpp"
#include "network/network_string.hpp" #include "network/network_string.hpp"
@ -50,7 +51,7 @@ void KartRewinder::reset()
*/ */
BareNetworkString* KartRewinder::saveState() const BareNetworkString* KartRewinder::saveState() const
{ {
const int MEMSIZE = 13*sizeof(float) + 9; const int MEMSIZE = 13*sizeof(float) + 9+2;
BareNetworkString *buffer = new BareNetworkString(MEMSIZE); BareNetworkString *buffer = new BareNetworkString(MEMSIZE);
const btRigidBody *body = m_kart->getBody(); const btRigidBody *body = m_kart->getBody();
@ -71,6 +72,10 @@ BareNetworkString* KartRewinder::saveState() const
// 3) Attachment // 3) Attachment
// ------------- // -------------
m_kart->getAttachment()->saveState(buffer); m_kart->getAttachment()->saveState(buffer);
// 4) Powerup
// ----------
m_kart->getPowerup()->saveState(buffer);
return buffer; return buffer;
} // saveState } // saveState
@ -98,6 +103,9 @@ void KartRewinder::rewindToState(BareNetworkString *buffer)
// ------------- // -------------
m_kart->getAttachment()->rewindTo(buffer); m_kart->getAttachment()->rewindTo(buffer);
// 4) Powerup
// ----------
m_kart->getPowerup()->rewindTo(buffer);
return; return;
} // rewindToState } // rewindToState