SpecificItemDrop.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "SpecificItemDrop.h"
  2. #include "Block.h"
  3. #include "Entity.h"
  4. #include "Inventory.h"
  5. #include "ItemStack.h"
  6. SpecificItemDrop::SpecificItemDrop(
  7. Framework::Text itemTypeName, ItemModifier* modifier, int amount)
  8. : DropConfig(),
  9. itemTypeName(itemTypeName),
  10. zType(0),
  11. modifier(modifier),
  12. amount(amount)
  13. {}
  14. SpecificItemDrop::~SpecificItemDrop()
  15. {
  16. if (modifier) modifier->release();
  17. }
  18. void SpecificItemDrop::initialize()
  19. {
  20. if (!zType)
  21. {
  22. int id = Game::INSTANCE->getItemTypeId(itemTypeName);
  23. if (id >= 0)
  24. {
  25. zType = Game::INSTANCE->zItemType(id);
  26. }
  27. }
  28. }
  29. Framework::Text SpecificItemDrop::getItemTypeName() const
  30. {
  31. return itemTypeName;
  32. }
  33. const ItemModifier* SpecificItemDrop::zModifier() const
  34. {
  35. return modifier;
  36. }
  37. int SpecificItemDrop::getAmount() const
  38. {
  39. return amount;
  40. }
  41. void SpecificItemDrop::doDrop(Entity* zActor,
  42. Item* zItem,
  43. ItemSkill* zUsedSkill,
  44. Framework::Either<Block*, Entity*> zDestroyedObject) const
  45. {
  46. if (zType)
  47. {
  48. Item* item = zType->createItem();
  49. if (item)
  50. {
  51. if (modifier)
  52. {
  53. modifier->applyOn(item);
  54. }
  55. ItemStack* stack = new ItemStack(item, amount);
  56. Inventory* inventory;
  57. if (zDestroyedObject.isA())
  58. {
  59. inventory = dynamic_cast<Inventory*>(zDestroyedObject.getA());
  60. }
  61. else
  62. {
  63. inventory = dynamic_cast<Inventory*>(zDestroyedObject.getB());
  64. }
  65. Game::INSTANCE->spawnItem(
  66. inventory->getLocation()
  67. + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  68. inventory->getDimensionId(),
  69. stack);
  70. }
  71. }
  72. }
  73. SpecificItemDropFactory::SpecificItemDropFactory()
  74. : DropConfigFactory()
  75. {}
  76. JSONObjectValidationBuilder* SpecificItemDropFactory::addToValidator(
  77. JSONObjectValidationBuilder* builder) const
  78. {
  79. return DropConfigFactory::addToValidator(builder)
  80. ->withRequiredAttribute("itemType",
  81. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  82. ItemTypeNameFactory::TYPE_ID))
  83. ->withRequiredAttribute("modifier",
  84. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>(),
  85. false,
  86. true)
  87. ->withRequiredNumber("amount")
  88. ->whichIsGreaterThen(0.0)
  89. ->withDefault(1.0)
  90. ->finishNumber();
  91. }
  92. const char* SpecificItemDropFactory::getTypeToken() const
  93. {
  94. return "specificItem";
  95. }
  96. SpecificItemDrop* SpecificItemDropFactory::createInstance(
  97. Framework::JSON::JSONObject* zJson) const
  98. {
  99. return new SpecificItemDrop(
  100. zJson->zValue("itemType")->asString()->getString(),
  101. zJson->hasValue("modifier")
  102. ? Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
  103. zJson->zValue("modifier"))
  104. : 0,
  105. (int)zJson->zValue("amount")->asNumber()->getNumber());
  106. }
  107. void SpecificItemDropFactory::addToJson(
  108. Framework::JSON::JSONObject* zJson, SpecificItemDrop* zObject) const
  109. {
  110. zJson->addValue("itemType",
  111. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  112. if (zObject->zModifier())
  113. {
  114. zJson->addValue("modifier",
  115. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
  116. }
  117. zJson->addValue(
  118. "amount", new Framework::JSON::JSONNumber(zObject->getAmount()));
  119. }