DropConfig.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include <ReferenceCounter.h>
  3. #include "DropCondition.h"
  4. #include "Game.h"
  5. #include "TypeRegistry.h"
  6. class DropConfig : public virtual Framework::ReferenceCounter
  7. {
  8. private:
  9. DropCondition* condition;
  10. public:
  11. DropConfig();
  12. virtual ~DropConfig();
  13. void setCondition(DropCondition* condition);
  14. virtual void initialize();
  15. const DropCondition* zCondition() const;
  16. void onObjectDestroyed(Entity* zActor,
  17. Item* zItem,
  18. ItemSkill* zUsedSkill,
  19. Framework::Either<Block*, Entity*> zDestroyedObject) const;
  20. protected:
  21. virtual void doDrop(Entity* zActor,
  22. Item* zItem,
  23. ItemSkill* zUsedSkill,
  24. Framework::Either<Block*, Entity*> zDestroyedObject) const
  25. = 0;
  26. };
  27. template<typename T> class DropConfigFactory
  28. : public SubTypeFactory<DropConfig, T>
  29. {
  30. public:
  31. DropConfigFactory()
  32. : SubTypeFactory<DropConfig, T>()
  33. {}
  34. virtual JSONObjectValidationBuilder* addToValidator(
  35. JSONObjectValidationBuilder* builder) const override
  36. {
  37. return builder->withRequiredAttribute("condition",
  38. Game::INSTANCE->zTypeRegistry()->getValidator<DropCondition>());
  39. }
  40. T* fromJson(Framework::JSON::JSONObject* zJson) const
  41. {
  42. T* result = createInstance(zJson);
  43. DropCondition* condition
  44. = Game::INSTANCE->zTypeRegistry()->fromJson<DropCondition>(
  45. zJson->zValue("condition"));
  46. result->setCondition(condition);
  47. return result;
  48. }
  49. Framework::JSON::JSONObject* toJsonObject(T* zObject) const override
  50. {
  51. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  52. result->addValue("condition",
  53. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zCondition()));
  54. addToJson(result, zObject);
  55. return result;
  56. }
  57. protected:
  58. virtual T* createInstance(Framework::JSON::JSONObject* zObject) const = 0;
  59. virtual void addToJson(Framework::JSON::JSONObject* zJson, T* zObject) const
  60. = 0;
  61. };