#pragma once #include #include #include "Block.h" #include "JNoise.h" #include "JsonExpression.h" #include "TypeRegistry.h" class GeneratorRule : public virtual Framework::ReferenceCounter { private: Framework::JSON::JSONObject* noiseConfig; Noise* noise; float threshold; JBoolExpression* condition; Framework::Text bottomLayer; Framework::Text topLayer; protected: virtual Framework::Either createBlock( int x, int y, int z, int dimensionId) = 0; public: GeneratorRule(); ~GeneratorRule(); void initialize(JExpressionMemory* zMemory); bool checkCondition(int x, int y, int z, JExpressionMemory* zMemory); Framework::Either generateBlock( int x, int y, int z, int dimensionId); void setNoiseConfig(Framework::JSON::JSONObject* noiseConfig); Framework::JSON::JSONObject* zNoiseConfig() const; void setThreshold(float threshold); float getThreshold() const; void setCondition(JBoolExpression* condition); JBoolExpression* zCondition() const; void setBottomLayer(Framework::Text bottomLayer); Framework::Text getBottomLayer() const; void setTopLayer(Framework::Text topLayer); Framework::Text getTopLayer() const; }; template class GeneratorRuleFactory : public SubTypeFactory { public: GeneratorRuleFactory() : SubTypeFactory() {} S* fromJson(Framework::JSON::JSONObject* zJson) const override { S* result = createValue(zJson); GeneratorRule* zRule = dynamic_cast(result); if (zJson->hasValue("noise")) { zRule->setNoiseConfig(zJson->getValue("noise")->asObject()); } if (zJson->hasValue("threshold")) { zRule->setThreshold( (float)zJson->zValue("threshold")->asNumber()->getNumber()); } if (zJson->hasValue("condition")) { zRule->setCondition( Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("condition"))); } if (zJson->hasValue("bottomLayer")) { zRule->setBottomLayer( zJson->zValue("bottomLayer")->asString()->getString()); } if (zJson->hasValue("topLayer")) { zRule->setTopLayer( zJson->zValue("topLayer")->asString()->getString()); } return result; } Framework::JSON::JSONObject* toJsonObject(S* zObject) const override { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); GeneratorRule* zRule = dynamic_cast(zObject); if (zRule->zNoiseConfig()) { result->addValue("noise", dynamic_cast( zRule->zNoiseConfig()->getThis())); } result->addValue("threshold", new Framework::JSON::JSONNumber(zRule->getThreshold())); if (zRule->zCondition()) { result->addValue("condition", Game::INSTANCE->zTypeRegistry()->toJson( dynamic_cast( zRule->zCondition()->getThis()))); } result->addValue("bottomLayer", new Framework::JSON::JSONString(zRule->getBottomLayer())); result->addValue( "topLayer", new Framework::JSON::JSONString(zRule->getTopLayer())); return result; } JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override { return builder ->withRequiredAttribute( "noise", JNoise::getValidator(true), false, true) ->withRequiredAttribute("condition", Game::INSTANCE->zTypeRegistry() ->getValidator(), false, true) ->withRequiredNumber("threshold") ->whichIsOptional() ->whichIsGreaterOrEqual(0.0) ->whichIsLessOrEqual(1.0) ->finishNumber() ->withRequiredString("topLayer") ->whichIsOptional() ->finishString() ->withRequiredString("bottomLayer") ->whichIsOptional() ->finishString(); } protected: virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0; };