Block.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. #include "Globals.h"
  4. Block::Block( ItemType *zTool, Framework::Vec3<int> pos )
  5. : Inventory( pos )
  6. {
  7. visible = false;
  8. transparent = false;
  9. passable = false;
  10. hp = 1;
  11. maxHP = 1;
  12. hardness = 1;
  13. this->zTool = zTool;
  14. speedModifier = 1;
  15. }
  16. Block::~Block()
  17. {}
  18. bool Block::updateVisibility()
  19. {
  20. bool v = 0;
  21. for( int d = 0; d < 6; d++ )
  22. {
  23. if( IS_BLOCK( zNeighbours[ d ] ) )
  24. v |= zNeighbours[ d ]->isVisible() && ( zNeighbours[ d ]->transparent || zNeighbours[ d ]->passable );
  25. else if( zNeighbours[ d ] )
  26. v = 1;
  27. }
  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, Block *zN )
  37. {
  38. zNeighbours[ dir ] = zN;
  39. }
  40. bool Block::isVisible() const
  41. {
  42. return visible;
  43. }
  44. BasicBlockItem::BasicBlockItem( const ItemType *zType, const char *name )
  45. : Item( zType, name )
  46. {
  47. placeable = 1;
  48. transparent = 0;
  49. passable = 0;
  50. hp = 0;
  51. maxHP = 0;
  52. hardness = 0;
  53. toolId = 0;
  54. speedModifier = 0;
  55. }
  56. BasicBlockItemType::BasicBlockItemType( int id )
  57. : ItemType( id )
  58. {}
  59. void BasicBlockItemType::loadSuperItem( Item *zItem, Framework::StreamReader *zReader ) const
  60. {
  61. ItemType::loadSuperItem( zItem, zReader );
  62. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  63. if( !item )
  64. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  65. zReader->lese( (char *)&item->transparent, 1 );
  66. zReader->lese( (char *)&item->passable, 1 );
  67. zReader->lese( (char *)&item->hp, 4 );
  68. zReader->lese( (char *)&item->maxHP, 4 );
  69. zReader->lese( (char *)&item->hardness, 4 );
  70. zReader->lese( (char *)&item->toolId, 4 );
  71. zReader->lese( (char *)&item->speedModifier, 4 );
  72. }