GeneratorRule.h 4.4 KB

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