Block.cpp 2.8 KB

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