123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include "BlockType.h"
- #include "ItemType.h"
- #include "BasicBlocks.h"
- #include "Block.h"
- using namespace Framework;
- BlockType::BlockType( int id, Block* defaultBlock )
- : ReferenceCounter(),
- id( id ),
- defaultBlock( defaultBlock )
- {
- StaticRegistry<BlockType>::INSTANCE.registerT( this, id );
- }
- BlockType::~BlockType()
- {
- if( defaultBlock )
- defaultBlock->release();
- }
- void BlockType::loadSuperBlock( Block* zBlock, Framework::StreamReader* zReader ) const
- {
- zBlock->loadInventory( zReader );
- zReader->lese( (char*)&zBlock->transparent, 1 );
- zReader->lese( (char*)&zBlock->passable, 1 );
- zReader->lese( (char*)&zBlock->hp, 4 );
- zReader->lese( (char*)&zBlock->maxHP, 4 );
- zReader->lese( (char*)&zBlock->hardness, 4 );
- zReader->lese( (char*)&zBlock->speedModifier, 4 );
- int effectiveToolId;
- zReader->lese( (char*)&effectiveToolId, 4 );
- if( effectiveToolId >= 0 )
- zBlock->zTool = StaticRegistry<ItemType>::INSTANCE.zElement( effectiveToolId );
- else
- zBlock->zTool = 0;
- }
- void BlockType::saveSuperBlock( Block* zBlock, Framework::StreamWriter* zWriter ) const
- {
- zBlock->saveInventory( zWriter );
- zWriter->schreibe( (char*)&zBlock->transparent, 1 );
- zWriter->schreibe( (char*)&zBlock->passable, 1 );
- zWriter->schreibe( (char*)&zBlock->hp, 4 );
- zWriter->schreibe( (char*)&zBlock->maxHP, 4 );
- zWriter->schreibe( (char*)&zBlock->hardness, 4 );
- zWriter->schreibe( (char*)&zBlock->speedModifier, 4 );
- int effectiveToolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
- zWriter->schreibe( (char*)&effectiveToolId, 4 );
- }
- void BlockType::createSuperBlock( Block* zBlock, Item* zItem ) const
- {
- BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
- if( !item )
- {
- throw "BlockType::createSuperBlock was called with an item witch was not an instance of BasicBlockItem";
- }
- zBlock->transparent = item->transparent;
- zBlock->passable = item->passable;
- zBlock->hp = item->hp;
- zBlock->maxHP = item->maxHP;
- zBlock->hardness = item->hardness;
- zBlock->speedModifier = item->speedModifier;
- zBlock->zTool = item->toolId >= 0 ? StaticRegistry<ItemType>::INSTANCE.zElement( item->toolId ) : 0;
- zBlock->interactable = item->interactable;
- }
- void BlockType::createSuperItem( Block* zBlock, Item* zItem ) const
- {
- BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
- if( !item )
- {
- throw "BlockType::createSuperItem was called with an item witch was not an instance of BasicBlockItem";
- }
- item->transparent = zBlock->transparent;
- item->passable = zBlock->passable;
- item->hp = zBlock->maxHP; // reset hp
- item->maxHP = zBlock->maxHP;
- item->hardness = zBlock->hardness;
- item->speedModifier = zBlock->speedModifier;
- item->toolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
- item->interactable = zBlock->interactable;
- }
- Block* BlockType::loadBlock( Framework::Vec3<int> position, Framework::StreamReader* zReader ) const
- {
- Block* result = createBlock( position );
- loadSuperBlock( result, zReader );
- return result;
- }
- void BlockType::saveBlock( Block* zBlock, Framework::StreamWriter* zWriter ) const
- {
- saveSuperBlock( zBlock, zWriter );
- }
- Item* BlockType::getItemFromBlock( Block* zBlock ) const
- {
- Item* result = createItem();
- createSuperItem( zBlock, result );
- return result;
- }
- Block* BlockType::createBlockAt( Framework::Vec3<int> position, Item* zUsedItem ) const
- {
- Block* result = createBlock( position );
- createSuperBlock( result, zUsedItem );
- return result;
- }
- int BlockType::getId() const
- {
- return id;
- }
- const Block* BlockType::zDefault() const
- {
- return defaultBlock;
- }
- const Block* getDefaultBlock( Either<Block*, int> b )
- {
- if( b.isA() )
- return b;
- else
- return StaticRegistry<BlockType>::INSTANCE.zElement( b )->zDefault();
- }
|