BlockReplacementDrop.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "BlockReplacementDrop.h"
  2. #include "Block.h"
  3. #include "BlockType.h"
  4. #include "Dimension.h"
  5. BlockReplacementDrop::BlockReplacementDrop(Framework::Text blockTypeName)
  6. : DropConfig(),
  7. blockTypeName(blockTypeName),
  8. zBlockType(0)
  9. {}
  10. void BlockReplacementDrop::initialize()
  11. {
  12. int id = Game::INSTANCE->getBlockTypeId(blockTypeName);
  13. zBlockType = Game::INSTANCE->zBlockType(id < 0 ? BlockTypeEnum::AIR : id);
  14. }
  15. Framework::Text BlockReplacementDrop::getBlockTypeName() const
  16. {
  17. return blockTypeName;
  18. }
  19. void BlockReplacementDrop::doDrop(Entity* zActor,
  20. Item* zItem,
  21. ItemSkill* zUsedSkill,
  22. Framework::Either<Block*, Entity*> zDestroyedObject) const
  23. {
  24. if (zBlockType && zDestroyedObject.isA())
  25. {
  26. Game::INSTANCE->zDimension(zDestroyedObject.getA()->getDimensionId())
  27. ->placeBlock(
  28. zDestroyedObject.getA()->getPos(), zBlockType->getId());
  29. }
  30. }
  31. BlockReplacementDropFactory::BlockReplacementDropFactory()
  32. : DropConfigFactory<BlockReplacementDrop>()
  33. {}
  34. JSONObjectValidationBuilder* BlockReplacementDropFactory::addToValidator(
  35. JSONObjectValidationBuilder* builder) const
  36. {
  37. return DropConfigFactory::addToValidator(builder)->withRequiredAttribute(
  38. "blockType",
  39. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  40. BlockTypeNameFactory::TYPE_ID));
  41. }
  42. const char* BlockReplacementDropFactory::getTypeToken() const
  43. {
  44. return "blockReplacement";
  45. }
  46. BlockReplacementDrop* BlockReplacementDropFactory::createInstance(
  47. Framework::JSON::JSONObject* zJson) const
  48. {
  49. return new BlockReplacementDrop(
  50. zJson->zValue("blockType")->asString()->getString());
  51. }
  52. void BlockReplacementDropFactory::addToJson(
  53. Framework::JSON::JSONObject* zJson, BlockReplacementDrop* zObject) const
  54. {
  55. zJson->addValue("blockTypeName",
  56. new Framework::JSON::JSONString(zObject->getBlockTypeName()));
  57. }