Block.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. void Block::remove()
  57. {
  58. if( visible )
  59. {
  60. visible = 0;
  61. currentGame->setVisibility( this, visible );
  62. }
  63. }
  64. bool Block::isVisible() const
  65. {
  66. return visible;
  67. }
  68. const BlockType* Block::zBlockType() const
  69. {
  70. return zType;
  71. }
  72. BasicBlockItem::BasicBlockItem( const ItemType* zType, const char* name )
  73. : Item( zType, name )
  74. {
  75. placeable = 1;
  76. transparent = 0;
  77. passable = 0;
  78. hp = 0;
  79. maxHP = 0;
  80. hardness = 0;
  81. toolId = 0;
  82. speedModifier = 0;
  83. }
  84. BasicBlockItemType::BasicBlockItemType( int id )
  85. : ItemType( id )
  86. {}
  87. void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const
  88. {
  89. ItemType::loadSuperItem( zItem, zReader );
  90. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  91. if( !item )
  92. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  93. zReader->lese( (char*)&item->transparent, 1 );
  94. zReader->lese( (char*)&item->passable, 1 );
  95. zReader->lese( (char*)&item->hp, 4 );
  96. zReader->lese( (char*)&item->maxHP, 4 );
  97. zReader->lese( (char*)&item->hardness, 4 );
  98. zReader->lese( (char*)&item->toolId, 4 );
  99. zReader->lese( (char*)&item->speedModifier, 4 );
  100. }