Block.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. Block::Block( const BlockType *zType, ItemType *zTool, Framework::Vec3<int> pos )
  4. : Inventory( pos )
  5. {
  6. transparent = false;
  7. passable = false;
  8. hp = 1;
  9. maxHP = 1;
  10. hardness = 1;
  11. this->zType = zType;
  12. this->zTool = zTool;
  13. speedModifier = 1;
  14. ticksLeftCounter = 0;
  15. wasTicked = 0;
  16. onTickCalled = 0;
  17. minTickTimeout = -1;
  18. maxTickTimeout = -1;
  19. tickSource = 0;
  20. currentTickTimeout = 0;
  21. }
  22. void Block::tick( TickQueue *zQueue )
  23. {
  24. if( wasTicked )
  25. return;
  26. wasTicked = 1;
  27. ticksLeftCounter++;
  28. if( minTickTimeout >= 0 )
  29. {
  30. if( currentTickTimeout < ticksLeftCounter )
  31. {
  32. onTickCalled = 1;
  33. bool blocked = 0;
  34. bool result = onTick( zQueue, ticksLeftCounter, blocked );
  35. if( blocked )
  36. {
  37. wasTicked = 0;
  38. ticksLeftCounter--;
  39. onTickCalled = 0;
  40. return;
  41. }
  42. if( result )
  43. currentTickTimeout = MAX( MIN( currentTickTimeout - 1, maxTickTimeout ), MAX( minTickTimeout, 0 ) );
  44. else
  45. currentTickTimeout = MAX( MIN( currentTickTimeout + 1, maxTickTimeout ), MAX( minTickTimeout, 0 ) );
  46. ticksLeftCounter = 0;
  47. }
  48. }
  49. }
  50. void Block::postTick()
  51. {
  52. wasTicked = 0;
  53. if( onTickCalled )
  54. {
  55. onPostTick();
  56. onTickCalled = 0;
  57. }
  58. }
  59. bool Block::isTickSource() const
  60. {
  61. return tickSource;
  62. }
  63. const BlockType *Block::zBlockType() const
  64. {
  65. return zType;
  66. }
  67. bool Block::isTransparent() const
  68. {
  69. return transparent;
  70. }
  71. bool Block::isPassable() const
  72. {
  73. return passable;
  74. }
  75. float Block::getHP() const
  76. {
  77. return hp;
  78. }
  79. float Block::getMaxHP() const
  80. {
  81. return maxHP;
  82. }
  83. float Block::getHardness() const
  84. {
  85. return hardness;
  86. }
  87. ItemType *Block::zEffectiveTool() const
  88. {
  89. return zTool;
  90. }
  91. float Block::getSpeedModifier() const
  92. {
  93. return speedModifier;
  94. }
  95. const Framework::Vec3<int> Block::getPos() const
  96. {
  97. return location;
  98. }
  99. BasicBlockItem::BasicBlockItem( const ItemType *zType, const char *name )
  100. : Item( zType, name )
  101. {
  102. placeable = 1;
  103. }
  104. bool BasicBlockItem::canBeStackedWith( Item *zItem ) const
  105. {
  106. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  107. if( item )
  108. {
  109. return Item::canBeStackedWith( zItem ) &&
  110. transparent == item->transparent &&
  111. passable == item->passable &&
  112. hp == item->hp &&
  113. maxHP == item->maxHP &&
  114. hardness == item->hardness &&
  115. toolId == item->toolId &&
  116. speedModifier == item->speedModifier;
  117. }
  118. return 0;
  119. }
  120. BasicBlockItemType::BasicBlockItemType( int id, ItemSkillLevelUpRule *levelUpRule, const ItemType *zBrokenType )
  121. : ItemType( id, levelUpRule, zBrokenType )
  122. {}
  123. void BasicBlockItemType::loadSuperItem( Item *zItem, Framework::Reader *zReader ) const
  124. {
  125. ItemType::loadSuperItem( zItem, zReader );
  126. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  127. if( !item )
  128. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  129. zReader->lese( (char *)&item->transparent, 1 );
  130. zReader->lese( (char *)&item->passable, 1 );
  131. zReader->lese( (char *)&item->hp, 4 );
  132. zReader->lese( (char *)&item->maxHP, 4 );
  133. zReader->lese( (char *)&item->hardness, 4 );
  134. zReader->lese( (char *)&item->toolId, 4 );
  135. zReader->lese( (char *)&item->speedModifier, 4 );
  136. }
  137. void BasicBlockItemType::saveSuperItem( Item *zItem, Framework::Writer *zWriter ) const
  138. {
  139. ItemType::saveSuperItem( zItem, zWriter );
  140. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  141. if( !item )
  142. throw "BasicBlockItemType::saveSuperItem was called with an invalid item";
  143. zWriter->schreibe( (char *)&item->transparent, 1 );
  144. zWriter->schreibe( (char *)&item->passable, 1 );
  145. zWriter->schreibe( (char *)&item->hp, 4 );
  146. zWriter->schreibe( (char *)&item->maxHP, 4 );
  147. zWriter->schreibe( (char *)&item->hardness, 4 );
  148. zWriter->schreibe( (char *)&item->toolId, 4 );
  149. zWriter->schreibe( (char *)&item->speedModifier, 4 );
  150. }
  151. void BasicBlockItemType::initializeItem( BasicBlockItem *zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const
  152. {
  153. zItem->transparent = transparent;
  154. zItem->passable = passable;
  155. zItem->hp = hp;
  156. zItem->maxHP = maxHP;
  157. zItem->hardness = hardness;
  158. zItem->toolId = toolId;
  159. zItem->speedModifier = speedModifier;
  160. }