Block.cpp 4.4 KB

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