/* $Id: bonusshield.cpp,v 1.12 2005/10/16 17:53:05 pohlt Exp $ */ #include "bonusshield.hpp" #include "avatar.hpp" /**********************************************************/ #define SHIELDBONUS_PROTECTION_TIME 400 #define SHIELDBONUS_HEALTH 300000 /**********************************************************/ BonusShield::BonusShield( World* wp ) : m_protectionTime( SHIELDBONUS_PROTECTION_TIME ), m_corona( NULL ) { m_worldPointer = wp; // set health for the health bonus m_health = SHIELDBONUS_HEALTH; // for mass and collission rectangle take the // default values from base class Bonus // THIS CALL MUST BE PART OF EACH CONSTRUCTOR // OF BONI, DERIVED FROM ITS BASE CLASS Bonus registerAtManager(); } /**********************************************************/ BonusShield::~BonusShield() { // remove corona object from world if( m_corona ) m_corona->setRemoveMeAfterUpdate( true ); // THIS CALL MUST BE PART OF EACH DESTRUCTOR // OF BONI, DERIVED FROM ITS BASE CLASS Bonus unregisterAtManager(); } /**********************************************************/ void BonusShield::add( const Bonus *const bonus ) { const BonusShield *const shield = dynamic_cast(bonus); DBG(1) ASSERT( shield != NULL, "BonusShield::add: attempt to add a bonus of type " "%s to a shield bonus", bonus->getIDString() ); m_protectionTime += shield->m_protectionTime; } /**********************************************************/ bool BonusShield::updateInBonusPack( Avatar *const avatar ) { // check, whether there already exists a corona object if( m_corona == NULL ) { // if the avatar and the corona have been deserialized, // we have to find the pointer to the corresponding // shield corona object for( int i = 0; i < avatar->getNAttachedObjects(); i++ ) { if( SHIELD_CORONA == avatar->getAttachedObject(i)->getID() ) { m_corona = (ShieldCorona*)avatar->getAttachedObject( i ); break; } } // if there is not a corona object yet if( m_corona == NULL ) { // create a new corona object ... m_corona = (ShieldCorona*)m_worldPointer->newObject( SHIELD_CORONA ); // ... and attach it to the protected avatar m_corona->attachTo( avatar ); } } // set the coordinates in the corona object m_corona->setPos( avatar->getPos() + Vector( avatar->getCollRectX() + 0.5 * avatar->getCollRectWidth(), avatar->getCollRectY() + 0.5 * avatar->getCollRectHeight() )); return --m_protectionTime > 0; } /**********************************************************/ Uint32 BonusShield::getSerializeBufferSize() const { return Bonus::getSerializeBufferSize() + Serialize::sizeOf( m_protectionTime ) // adds additional size for debugging (see serialize.hpp), // but only, if the tags are used PLUS_TAG_SIZE( 1 ); } /**********************************************************/ void BonusShield::serialize( Uint8*& bufferPointer ) const { // expands to a check of the buffer movement START_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer ); Bonus::serialize( bufferPointer ); Serialize::serialize( m_protectionTime, bufferPointer ); // expands to tag serialization SERIALIZE_OBJECT_TAG( bufferPointer ); // expands to a check of the buffer movement END_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer, BonusShield ); } /**********************************************************/ void BonusShield::deserialize( Uint8*& bufferPointer ) { Bonus::deserialize( bufferPointer ); Serialize::deserialize( bufferPointer, m_protectionTime ); // expands to tag deserialization DESERIALIZE_OBJECT_TAG( bufferPointer ); } /**********************************************************/