#include "EntityGenerator.h" #include "EntityType.h" #include "Game.h" #include "JNoise.h" #include "JsonExpression.h" EntityGenerator::EntityGenerator() : ReferenceCounter(), noise(0), noiseConfig(0), threshold(0.0), zType(0), condition(0) {} EntityGenerator::~EntityGenerator() { if (condition) { condition->release(); } if (noise) { noise->release(); } if (noiseConfig) { noiseConfig->release(); } } void EntityGenerator::initialize(JExpressionMemory* zMemory) { if (noiseConfig) { if (noise) noise->release(); noise = JNoise::parseNoise(noiseConfig, zMemory); } } bool EntityGenerator::isGenerated( int x, int y, int z, int dimensionId, JExpressionMemory* zMemory) { return (!noise || noise->getNoise((double)x, (double)y, (double)z) <= threshold) && condition->getValue(zMemory); } Entity* EntityGenerator::generate(Framework::Vec3 pos, int dimesnionId) { return zType->createEntityAt(pos, dimesnionId); } EntityGeneratorFactory::EntityGeneratorFactory() : TypeFactory() {} EntityGenerator* EntityGeneratorFactory::createValue( Framework::JSON::JSONObject* zJson) const { return new EntityGenerator(); } void EntityGeneratorFactory::fromJson( EntityGenerator* zResult, Framework::JSON::JSONObject* zJson) const { if (zJson->hasValue("noise")) { zResult->noiseConfig = zJson->getValue("noise")->asObject(); } if (zJson->hasValue("threshold")) { zResult->threshold = zJson->zValue("threshold")->asNumber()->getNumber(); } if (zJson->hasValue("type")) { zResult->zType = Game::INSTANCE->zEntityType(Game::INSTANCE->getEntityTypeId( zJson->zValue("type")->asString()->getString())); } if (zJson->hasValue("condition")) { zResult->condition = Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("condition")); } } void EntityGeneratorFactory::toJson( EntityGenerator* zObject, Framework::JSON::JSONObject* zResult) const { if (zObject->noiseConfig) { zResult->addValue("noise", dynamic_cast( zObject->noiseConfig->getThis())); } zResult->addValue( "threshold", new Framework::JSON::JSONNumber(zObject->threshold)); zResult->addValue( "type", new Framework::JSON::JSONString(zObject->zType->getName())); zResult->addValue("condition", Game::INSTANCE->zTypeRegistry()->toJson(zObject->condition)); } JSONObjectValidationBuilder* EntityGeneratorFactory::addToValidator( JSONObjectValidationBuilder* builder) const { Framework::RCArray entityTypeNames; for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++) { if (Game::INSTANCE->zEntityType(i)) { entityTypeNames.add( new Framework::Text(Game::INSTANCE->zEntityType(i)->getName())); } } return builder->withRequiredAttribute("noise", JNoise::getValidator(true)) ->withRequiredNumber("threshold") ->whichIsOptional() ->whichIsGreaterOrEqual(0.0) ->whichIsLessOrEqual(1.0) ->finishNumber() ->withRequiredString("type") ->whichIsOneOf(entityTypeNames) ->finishString() ->withRequiredAttribute("condition", Game::INSTANCE->zTypeRegistry()->getValidator()); }