BasicBlocks.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "BasicBlocks.h"
  2. #include "Game.h"
  3. #include "ItemEntity.h"
  4. #include "ItemStack.h"
  5. #include "ModelInfo.h"
  6. #include "TreeSeblingBlock.h"
  7. BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  8. : BasicBlock(typeId, pos, dimensionId, false)
  9. {}
  10. BasicBlock::BasicBlock(
  11. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  12. : Block(typeId, pos, dimensionId, hasInventory)
  13. {}
  14. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  15. {
  16. return 0;
  17. }
  18. void BasicBlock::onPostTick() {}
  19. BasicBlockType::BasicBlockType()
  20. : BlockType(),
  21. itemTypeName(),
  22. transparent(0),
  23. passable(0),
  24. speedModifier(1.f),
  25. interactable(1)
  26. {}
  27. bool BasicBlockType::initialize(Game* zGame)
  28. {
  29. if (itemTypeName.getLength())
  30. {
  31. itemTypeId = zGame->getItemTypeId(itemTypeName);
  32. }
  33. else
  34. {
  35. itemTypeId = 0;
  36. }
  37. return itemTypeId >= 0 && BlockType::initialize(zGame);
  38. }
  39. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  40. {
  41. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  42. block->transparent = transparent;
  43. block->passable = passable;
  44. block->hp = (float)getInitialMaxHP();
  45. block->maxHP = (float)getInitialMaxHP();
  46. block->hardness = getHardness();
  47. block->speedModifier = speedModifier;
  48. block->interactable = interactable;
  49. BlockType::createSuperBlock(zBlock, zItem);
  50. }
  51. Block* BasicBlockType::createBlock(
  52. Framework::Vec3<int> position, int dimensionId) const
  53. {
  54. return new BasicBlock(getId(), position, dimensionId);
  55. }
  56. Item* BasicBlockType::createItem() const
  57. {
  58. if (getItemTypeName().istGleich(""))
  59. {
  60. return 0;
  61. }
  62. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  63. }
  64. Framework::Text BasicBlockType::getItemTypeName() const
  65. {
  66. return itemTypeName;
  67. }
  68. ItemType* BasicBlockType::createItemType() const
  69. {
  70. if (getItemTypeName().istGleich(""))
  71. {
  72. return 0;
  73. }
  74. return new BasicBlockItemType(getItemTypeName(),
  75. new ModelInfo(zModel()->getModelPath(),
  76. zModel()->getTexturePaths(),
  77. zModel()->isTransparent(),
  78. zModel()->getSize() / 2.f),
  79. transparent,
  80. passable,
  81. getHardness(),
  82. speedModifier,
  83. getItemTypeName(),
  84. 0,
  85. 50,
  86. getGroupNames());
  87. }
  88. void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
  89. {
  90. this->itemTypeName = itemTypeName;
  91. }
  92. int BasicBlockType::getItemTypeId() const
  93. {
  94. return itemTypeId;
  95. }
  96. void BasicBlockType::setTransparent(bool transparent)
  97. {
  98. this->transparent = transparent;
  99. }
  100. bool BasicBlockType::isTransparent() const
  101. {
  102. return transparent;
  103. }
  104. void BasicBlockType::setPassable(bool passable)
  105. {
  106. this->passable = passable;
  107. }
  108. bool BasicBlockType::isPassable() const
  109. {
  110. return passable;
  111. }
  112. void BasicBlockType::setSpeedModifier(float speedModifier)
  113. {
  114. this->speedModifier = speedModifier;
  115. }
  116. float BasicBlockType::getSpeedModifier() const
  117. {
  118. return speedModifier;
  119. }
  120. void BasicBlockType::setInteractable(bool interactable)
  121. {
  122. this->interactable = interactable;
  123. }
  124. bool BasicBlockType::isInteractable() const
  125. {
  126. return interactable;
  127. }
  128. BasicBlockTypeFactory::BasicBlockTypeFactory()
  129. : BlockTypeFactoryBase()
  130. {}
  131. BasicBlockType* BasicBlockTypeFactory::createValue(
  132. Framework::JSON::JSONObject* zJson) const
  133. {
  134. return new BasicBlockType();
  135. }
  136. BasicBlockType* BasicBlockTypeFactory::fromJson(
  137. Framework::JSON::JSONObject* zJson) const
  138. {
  139. BasicBlockType* result = BlockTypeFactoryBase::fromJson(zJson);
  140. if (zJson->zValue("itemType")->getType() == Framework::AbstractType::STRING)
  141. {
  142. result->setItemTypeName(
  143. zJson->zValue("itemType")->asString()->getString());
  144. }
  145. else
  146. {
  147. result->setItemTypeName("");
  148. }
  149. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  150. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  151. result->setSpeedModifier(
  152. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  153. result->setInteractable(
  154. (float)zJson->zValue("interactable")->asBool()->getBool());
  155. return result;
  156. }
  157. Framework::JSON::JSONObject* BasicBlockTypeFactory::toJsonObject(
  158. BasicBlockType* zObject) const
  159. {
  160. Framework::JSON::JSONObject* result
  161. = BlockTypeFactoryBase::toJsonObject(zObject);
  162. if (zObject->getItemTypeName().istGleich(""))
  163. {
  164. result->addValue("itemType", new Framework::JSON::JSONValue());
  165. }
  166. else
  167. {
  168. result->addValue("itemType",
  169. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  170. }
  171. result->addValue(
  172. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  173. result->addValue(
  174. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  175. result->addValue("speedModifier",
  176. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  177. result->addValue("interactable",
  178. new Framework::JSON::JSONBool(zObject->isInteractable()));
  179. return result;
  180. }
  181. JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
  182. JSONObjectValidationBuilder* builder) const
  183. {
  184. return BlockTypeFactoryBase::addToValidator(builder
  185. ->withRequiredAttribute("itemType",
  186. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  187. ItemTypeNameFactory::TYPE_ID),
  188. true,
  189. false)
  190. ->withRequiredBool("transparent")
  191. ->withDefault(false)
  192. ->finishBool()
  193. ->withRequiredBool("passable")
  194. ->withDefault(false)
  195. ->finishBool()
  196. ->withRequiredNumber("speedModifier")
  197. ->withDefault(1.0)
  198. ->finishNumber()
  199. ->withRequiredBool("interactable")
  200. ->withDefault(true)
  201. ->finishBool());
  202. }
  203. const char* BasicBlockTypeFactory::getTypeToken() const
  204. {
  205. return "basicBlock";
  206. }