123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #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<float> 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<JBoolExpression>(
- zJson->zValue("condition"));
- }
- }
- void EntityGeneratorFactory::toJson(
- EntityGenerator* zObject, Framework::JSON::JSONObject* zResult) const
- {
- if (zObject->noiseConfig)
- {
- zResult->addValue("noise",
- dynamic_cast<Framework::JSON::JSONValue*>(
- 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<Framework::Text> 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<JBoolExpression>());
- }
|