Block.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. memset( zNeighbours, 0, sizeof( Block * ) * 6 );
  16. }
  17. Block::~Block()
  18. {}
  19. bool Block::updateVisibility()
  20. {
  21. bool v = 0;
  22. for( int d = 0; d < 6; d++ )
  23. {
  24. if( IS_BLOCK( zNeighbours[ d ] ) )
  25. v |= zNeighbours[ d ]->isVisible() && ( zNeighbours[ d ]->transparent || zNeighbours[ d ]->passable );
  26. else if( zNeighbours[ d ] )
  27. v = 1;
  28. }
  29. if( v != visible )
  30. {
  31. visible = v;
  32. currentGame->setVisibility( this, visible );
  33. return true;
  34. }
  35. return false;
  36. }
  37. void Block::setNeighbour( Direction dir, Block *zN )
  38. {
  39. zNeighbours[ dir ] = zN;
  40. }
  41. bool Block::isVisible() const
  42. {
  43. return visible;
  44. }
  45. BasicBlockItem::BasicBlockItem( const ItemType *zType, const char *name )
  46. : Item( zType, name )
  47. {
  48. placeable = 1;
  49. transparent = 0;
  50. passable = 0;
  51. hp = 0;
  52. maxHP = 0;
  53. hardness = 0;
  54. toolId = 0;
  55. speedModifier = 0;
  56. }
  57. BasicBlockItemType::BasicBlockItemType( int id )
  58. : ItemType( id )
  59. {}
  60. void BasicBlockItemType::loadSuperItem( Item *zItem, Framework::StreamReader *zReader ) const
  61. {
  62. ItemType::loadSuperItem( zItem, zReader );
  63. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  64. if( !item )
  65. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  66. zReader->lese( (char *)&item->transparent, 1 );
  67. zReader->lese( (char *)&item->passable, 1 );
  68. zReader->lese( (char *)&item->hp, 4 );
  69. zReader->lese( (char *)&item->maxHP, 4 );
  70. zReader->lese( (char *)&item->hardness, 4 );
  71. zReader->lese( (char *)&item->toolId, 4 );
  72. zReader->lese( (char *)&item->speedModifier, 4 );
  73. }