Block.cpp 4.7 KB

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