Block.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. #include "Globals.h"
  4. Block::Block( const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory )
  5. : Inventory( pos, hasInventory ),
  6. Model3D(),
  7. zType( zType )
  8. {
  9. visible = false;
  10. transparent = false;
  11. passable = false;
  12. hp = 1;
  13. maxHP = 1;
  14. hardness = 1;
  15. this->zTool = zTool;
  16. speedModifier = 1;
  17. memset( zNeighbours, 0, sizeof( Block* ) * 6 );
  18. memset( neighbourTypes, 0, sizeof( int ) * 6 );
  19. Model3D::setPosition( (Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f} );
  20. }
  21. Block::~Block()
  22. {}
  23. bool Block::updateVisibility()
  24. {
  25. bool v = 1;
  26. for( int d = 0; d < 6; d++ )
  27. v |= CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->isVisible() && (CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->transparent || CONST_BLOCK( zNeighbours[ d ], neighbourTypes[ d ] )->passable);
  28. if( v != visible )
  29. {
  30. visible = v;
  31. currentGame->setVisibility( this, visible );
  32. return true;
  33. }
  34. return false;
  35. }
  36. void Block::setNeighbour( Direction dir, Framework::Either<Block*, int> neighbour )
  37. {
  38. if( neighbour.isA() )
  39. setNeighbourBlock( dir, neighbour );
  40. else
  41. {
  42. setNeighbourBlock( dir, 0 );
  43. setNeighbourType( dir, neighbour );
  44. }
  45. }
  46. void Block::setNeighbourBlock( Direction dir, Block* zN )
  47. {
  48. if( zN )
  49. setNeighbourType( dir, zN->zBlockType()->getId() );
  50. zNeighbours[ getDirectionIndex( dir ) ] = zN;
  51. }
  52. void Block::setNeighbourType( Direction dir, int type )
  53. {
  54. neighbourTypes[ getDirectionIndex( dir ) ] = type;
  55. }
  56. bool Block::isTransparent() const
  57. {
  58. return transparent;
  59. }
  60. void Block::remove()
  61. {
  62. if( visible )
  63. {
  64. visible = 0;
  65. currentGame->setVisibility( this, visible );
  66. }
  67. }
  68. bool Block::isVisible() const
  69. {
  70. return visible;
  71. }
  72. const BlockType* Block::zBlockType() const
  73. {
  74. return zType;
  75. }
  76. BasicBlockItem::BasicBlockItem( const ItemType* zType, const char* name )
  77. : Item( zType, name )
  78. {
  79. placeable = 1;
  80. transparent = 0;
  81. passable = 0;
  82. hp = 0;
  83. maxHP = 0;
  84. hardness = 0;
  85. toolId = 0;
  86. speedModifier = 0;
  87. }
  88. BasicBlockItemType::BasicBlockItemType( int id )
  89. : ItemType( id )
  90. {}
  91. void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const
  92. {
  93. ItemType::loadSuperItem( zItem, zReader );
  94. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  95. if( !item )
  96. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  97. zReader->lese( (char*)&item->transparent, 1 );
  98. zReader->lese( (char*)&item->passable, 1 );
  99. zReader->lese( (char*)&item->hp, 4 );
  100. zReader->lese( (char*)&item->maxHP, 4 );
  101. zReader->lese( (char*)&item->hardness, 4 );
  102. zReader->lese( (char*)&item->toolId, 4 );
  103. zReader->lese( (char*)&item->speedModifier, 4 );
  104. }