GeneratorRule.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include <Either.h>
  3. #include <JSON.h>
  4. #include "Block.h"
  5. #include "JNoise.h"
  6. #include "JsonExpression.h"
  7. #include "TypeRegistry.h"
  8. class GeneratorRule : public virtual Framework::ReferenceCounter
  9. {
  10. private:
  11. Framework::JSON::JSONObject* noiseConfig;
  12. Noise* noise;
  13. float threshold;
  14. JBoolExpression* condition;
  15. protected:
  16. virtual Framework::Either<Block*, int> createBlock(
  17. int x, int y, int z, int dimensionId)
  18. = 0;
  19. public:
  20. GeneratorRule();
  21. ~GeneratorRule();
  22. void initialize(JExpressionMemory* zMemory);
  23. bool checkCondition(int x, int y, int z, JExpressionMemory* zMemory);
  24. Framework::Either<Block*, int> generateBlock(
  25. int x, int y, int z, int dimensionId);
  26. void setNoiseConfig(Framework::JSON::JSONObject* noiseConfig);
  27. Framework::JSON::JSONObject* zNoiseConfig() const;
  28. void setThreshold(float threshold);
  29. float getThreshold() const;
  30. void setCondition(JBoolExpression* condition);
  31. JBoolExpression* zCondition() const;
  32. };
  33. template<typename S> class GeneratorRuleFactory
  34. : public SubTypeFactory<GeneratorRule, S>
  35. {
  36. public:
  37. GeneratorRuleFactory()
  38. : SubTypeFactory<GeneratorRule, S>()
  39. {}
  40. S* fromJson(Framework::JSON::JSONObject* zJson) const override
  41. {
  42. S* result = createValue(zJson);
  43. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(result);
  44. if (zJson->hasValue("noise"))
  45. {
  46. zRule->setNoiseConfig(zJson->getValue("noise")->asObject());
  47. }
  48. if (zJson->hasValue("threshold"))
  49. {
  50. zRule->setThreshold(
  51. (float)zJson->zValue("threshold")->asNumber()->getNumber());
  52. }
  53. zRule->setCondition(
  54. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  55. zJson->zValue("condition")));
  56. return result;
  57. }
  58. Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  59. {
  60. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  61. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zObject);
  62. if (zRule->zNoiseConfig())
  63. {
  64. result->addValue("noise",
  65. dynamic_cast<Framework::JSON::JSONValue*>(
  66. zRule->zNoiseConfig()->getThis()));
  67. }
  68. result->addValue("threshold",
  69. new Framework::JSON::JSONNumber(zRule->getThreshold()));
  70. result->addValue("condition",
  71. Game::INSTANCE->zTypeRegistry()->toJson(
  72. dynamic_cast<Framework::JSON::JSONValue*>(
  73. zRule->zCondition()->getThis())));
  74. return result;
  75. }
  76. JSONObjectValidationBuilder* addToValidator(
  77. JSONObjectValidationBuilder* builder) const override
  78. {
  79. return builder
  80. ->withRequiredAttribute("noise", JNoise::getValidator(true))
  81. ->withRequiredAttribute("condition",
  82. Game::INSTANCE->zTypeRegistry()
  83. ->getValidator<JBoolExpression>())
  84. ->withRequiredNumber("threshold")
  85. ->whichIsOptional()
  86. ->whichIsGreaterOrEqual(0.0)
  87. ->whichIsLessOrEqual(1.0)
  88. ->finishNumber();
  89. }
  90. protected:
  91. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  92. };