GeneratorTemplate.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <ReferenceCounter.h>
  3. #include <Vec3.h>
  4. #include "GeneratedStructure.h"
  5. #include "Noise.h"
  6. class GeneratorTemplate : public Framework::ReferenceCounter
  7. {
  8. private:
  9. float propability;
  10. Framework::Vec3<int> minPosOffset;
  11. Framework::Vec3<int> maxSize;
  12. public:
  13. GeneratorTemplate();
  14. protected:
  15. void setMinPosOffset(Framework::Vec3<int> minPosOffset);
  16. void setMaxSize(Framework::Vec3<int> maxSize);
  17. public:
  18. void setPropability(float propability);
  19. virtual bool canEffect(Framework::Vec3<int> location,
  20. Framework::Vec3<int> affectedLocation) const;
  21. virtual GeneratedStructure* generateAt(
  22. Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
  23. = 0;
  24. float getPropability() const;
  25. Framework::Vec3<int> getMinAffectedOffset() const;
  26. Framework::Vec3<int> getMaxAffectedOffset() const;
  27. };
  28. template<typename S> class GeneratorTemplateFactory
  29. : public SubTypeFactory<GeneratorTemplate, S>
  30. {
  31. public:
  32. GeneratorTemplateFactory()
  33. : SubTypeFactory<GeneratorTemplate, S>()
  34. {}
  35. S* fromJson(Framework::JSON::JSONObject* zJson) const override
  36. {
  37. S* result = createValue(zJson);
  38. GeneratorTemplate* zRecipie = dynamic_cast<GeneratorTemplate*>(result);
  39. zRecipie->setPropability(
  40. (float)zJson->zValue("propability")->asNumber()->getNumber());
  41. return result;
  42. }
  43. Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  44. {
  45. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  46. GeneratorTemplate* zRecipie = dynamic_cast<GeneratorTemplate*>(result);
  47. result->addValue("propability",
  48. new Framework::JSON::JSONNumber(zRecipie->getPropability()));
  49. return result;
  50. };
  51. JSONObjectValidationBuilder* addToValidator(
  52. JSONObjectValidationBuilder* builder) const override
  53. {
  54. return builder->withRequiredNumber("propability")
  55. ->whichIsGreaterThen(0)
  56. ->finishNumber();
  57. }
  58. protected:
  59. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  60. };