#include "Block.h" #include "Inventory.h" #include "Globals.h" Block::Block( const BlockType* zType, ItemType* zTool, Framework::Vec3 pos, bool hasInventory ) : Inventory( pos, hasInventory ), Model3D(), zType( zType ) { visible = false; transparent = false; passable = false; hp = 1; maxHP = 1; hardness = 1; this->zTool = zTool; speedModifier = 1; memset( zNeighbours, 0, sizeof( Block* ) * 6 ); memset( neighbourTypes, 0, sizeof( int ) * 6 ); Model3D::setPosition( (Framework::Vec3)pos + Framework::Vec3{0.5f, 0.5f, 0.5f} ); } Block::~Block() {} bool Block::updateVisibility() { bool v = 1; for( int d = 0; d < 6; d++ ) v |= CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->isVisible() && (CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->transparent || CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->passable); if( v != visible ) { visible = v; currentGame->setVisibility( this, visible ); return true; } return false; } void Block::setNeighbour( Direction dir, Framework::Either neighbour ) { if( neighbour.isA() ) setNeighbourBlock( dir, neighbour ); else { setNeighbourBlock( dir, 0 ); setNeighbourType( dir, neighbour ); } } void Block::setNeighbourBlock( Direction dir, Block* zN ) { if( zN ) setNeighbourType( dir, zN->zBlockType()->getId() ); zNeighbours[ getDirectionIndex( dir ) ] = zN; } void Block::setNeighbourType( Direction dir, int type ) { neighbourTypes[ getDirectionIndex( dir ) ] = type; } void Block::remove() { if( visible ) { visible = 0; currentGame->setVisibility( this, visible ); } } bool Block::isVisible() const { return visible; } const BlockType* Block::zBlockType() const { return zType; } BasicBlockItem::BasicBlockItem( const ItemType* zType, const char* name ) : Item( zType, name ) { placeable = 1; transparent = 0; passable = 0; hp = 0; maxHP = 0; hardness = 0; toolId = 0; speedModifier = 0; } BasicBlockItemType::BasicBlockItemType( int id ) : ItemType( id ) {} void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const { ItemType::loadSuperItem( zItem, zReader ); BasicBlockItem* item = dynamic_cast(zItem); if( !item ) throw "BasicBlockItemType::loadSuperItem was called with an invalid item"; zReader->lese( (char*)&item->transparent, 1 ); zReader->lese( (char*)&item->passable, 1 ); zReader->lese( (char*)&item->hp, 4 ); zReader->lese( (char*)&item->maxHP, 4 ); zReader->lese( (char*)&item->hardness, 4 ); zReader->lese( (char*)&item->toolId, 4 ); zReader->lese( (char*)&item->speedModifier, 4 ); }