GeneratorRule.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
  41. {
  42. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zResult);
  43. if (zJson->hasValue("noise"))
  44. {
  45. zRule->setNoiseConfig(zJson->getValue("noise")->asObject());
  46. }
  47. if (zJson->hasValue("threshold"))
  48. {
  49. zRule->setThreshold(
  50. (float)zJson->zValue("threshold")->asNumber()->getNumber());
  51. }
  52. zRule->setCondition(
  53. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  54. zJson->zValue("condition")));
  55. }
  56. void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
  57. {
  58. GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zObject);
  59. if (zRule->zNoiseConfig())
  60. {
  61. zResult->addValue("noise",
  62. dynamic_cast<Framework::JSON::JSONValue*>(
  63. zRule->zNoiseConfig()->getThis()));
  64. }
  65. zResult->addValue("threshold",
  66. new Framework::JSON::JSONNumber(zRule->getThreshold()));
  67. zResult->addValue("condition",
  68. Game::INSTANCE->zTypeRegistry()->toJson(
  69. dynamic_cast<Framework::JSON::JSONValue*>(
  70. zRule->zCondition()->getThis())));
  71. }
  72. JSONObjectValidationBuilder* addToValidator(
  73. JSONObjectValidationBuilder* builder) const override
  74. {
  75. return builder
  76. ->withRequiredAttribute("noise", JNoise::getValidator(true))
  77. ->withRequiredAttribute("condition",
  78. Game::INSTANCE->zTypeRegistry()
  79. ->getValidator<JBoolExpression>())
  80. ->withRequiredNumber("threshold")
  81. ->whichIsOptional()
  82. ->whichIsGreaterOrEqual(0.0)
  83. ->whichIsLessOrEqual(1.0)
  84. ->finishNumber();
  85. }
  86. };