1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #pragma once
- #include <Either.h>
- #include <JSON.h>
- #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;
- protected:
- virtual Framework::Either<Block*, int> 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<Block*, int> 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;
- };
- template<typename S> class GeneratorRuleFactory
- : public SubTypeFactory<GeneratorRule, S>
- {
- public:
- GeneratorRuleFactory()
- : SubTypeFactory<GeneratorRule, S>()
- {}
- void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zResult);
- if (zJson->hasValue("noise"))
- {
- zRule->setNoiseConfig(zJson->getValue("noise")->asObject());
- }
- if (zJson->hasValue("threshold"))
- {
- zRule->setThreshold(
- (float)zJson->zValue("threshold")->asNumber()->getNumber());
- }
- zRule->setCondition(
- Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
- zJson->zValue("condition")));
- }
- void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- GeneratorRule* zRule = dynamic_cast<GeneratorRule*>(zObject);
- if (zRule->zNoiseConfig())
- {
- zResult->addValue("noise",
- dynamic_cast<Framework::JSON::JSONValue*>(
- zRule->zNoiseConfig()->getThis()));
- }
- zResult->addValue("threshold",
- new Framework::JSON::JSONNumber(zRule->getThreshold()));
- zResult->addValue("condition",
- Game::INSTANCE->zTypeRegistry()->toJson(
- dynamic_cast<Framework::JSON::JSONValue*>(
- zRule->zCondition()->getThis())));
- }
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- return builder
- ->withRequiredAttribute("noise", JNoise::getValidator(true))
- ->withRequiredAttribute("condition",
- Game::INSTANCE->zTypeRegistry()
- ->getValidator<JBoolExpression>())
- ->withRequiredNumber("threshold")
- ->whichIsOptional()
- ->whichIsGreaterOrEqual(0.0)
- ->whichIsLessOrEqual(1.0)
- ->finishNumber();
- }
- };
|