GeneratorRule.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Framework::Text bottomLayer;
  16. Framework::Text topLayer;
  17. protected:
  18. virtual Framework::Either<Block*, int> createBlock(
  19. int x, int y, int z, int dimensionId)
  20. = 0;
  21. public:
  22. GeneratorRule();
  23. ~GeneratorRule();
  24. void initialize(JExpressionMemory* zMemory);
  25. bool checkCondition(int x, int y, int z, JExpressionMemory* zMemory);
  26. Framework::Either<Block*, int> generateBlock(
  27. int x, int y, int z, int dimensionId);
  28. void setNoiseConfig(Framework::JSON::JSONObject* noiseConfig);
  29. Framework::JSON::JSONObject* zNoiseConfig() const;
  30. void setThreshold(float threshold);
  31. float getThreshold() const;
  32. void setCondition(JBoolExpression* condition);
  33. JBoolExpression* zCondition() const;
  34. void setBottomLayer(Framework::Text bottomLayer);
  35. Framework::Text getBottomLayer() const;
  36. void setTopLayer(Framework::Text topLayer);
  37. Framework::Text getTopLayer() const;
  38. };
  39. template<typename S> class GeneratorRuleFactory
  40. : public SubTypeFactory<GeneratorRule, S>
  41. {
  42. public:
  43. GeneratorRuleFactory()
  44. : SubTypeFactory<GeneratorRule, S>()
  45. {}
  46. S* fromJson(Framework::JSON::JSONObject* zJson) const override
  47. {
  48. S* result = createValue(zJson);
  49. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(result);
  50. if (zJson->hasValue("noise"))
  51. {
  52. zRule->setNoiseConfig(zJson->getValue("noise")->asObject());
  53. }
  54. if (zJson->hasValue("threshold"))
  55. {
  56. zRule->setThreshold(
  57. (float)zJson->zValue("threshold")->asNumber()->getNumber());
  58. }
  59. if (zJson->hasValue("condition"))
  60. {
  61. zRule->setCondition(
  62. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  63. zJson->zValue("condition")));
  64. }
  65. if (zJson->hasValue("bottomLayer"))
  66. {
  67. zRule->setBottomLayer(
  68. (float)zJson->zValue("bottomLayer")->asString()->getString());
  69. }
  70. if (zJson->hasValue("topLayer"))
  71. {
  72. zRule->setTopLayer(
  73. (float)zJson->zValue("topLayer")->asString()->getString());
  74. }
  75. return result;
  76. }
  77. Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  78. {
  79. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  80. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zObject);
  81. if (zRule->zNoiseConfig())
  82. {
  83. result->addValue("noise",
  84. dynamic_cast<Framework::JSON::JSONValue*>(
  85. zRule->zNoiseConfig()->getThis()));
  86. }
  87. result->addValue("threshold",
  88. new Framework::JSON::JSONNumber(zRule->getThreshold()));
  89. if (zRule->zCondition())
  90. {
  91. result->addValue("condition",
  92. Game::INSTANCE->zTypeRegistry()->toJson(
  93. dynamic_cast<Framework::JSON::JSONValue*>(
  94. zRule->zCondition()->getThis())));
  95. }
  96. result->addValue("bottomLayer",
  97. new Framework::JSON::JSONString(zRule->getBottomLayer()));
  98. result->addValue(
  99. "topLayer", new Framework::JSON::JSONString(zRule->getTopLayer()));
  100. return result;
  101. }
  102. JSONObjectValidationBuilder* addToValidator(
  103. JSONObjectValidationBuilder* builder) const override
  104. {
  105. return builder
  106. ->withRequiredAttribute(
  107. "noise", JNoise::getValidator(true), false, true)
  108. ->withRequiredAttribute("condition",
  109. Game::INSTANCE->zTypeRegistry()
  110. ->getValidator<JBoolExpression>(),
  111. false,
  112. true)
  113. ->withRequiredNumber("threshold")
  114. ->whichIsOptional()
  115. ->whichIsGreaterOrEqual(0.0)
  116. ->whichIsLessOrEqual(1.0)
  117. ->finishNumber()
  118. ->withRequiredString("topLayer")
  119. ->whichIsOptional()
  120. ->finishString()
  121. ->withRequiredString("bottomLayer")
  122. ->whichIsOptional()
  123. ->finishString();
  124. }
  125. protected:
  126. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  127. };