BlockInstanceGeneratorRule.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "BlockInstanceGeneratorRule.h"
  2. #include "Game.h"
  3. BlockInstanceGeneratorRule::BlockInstanceGeneratorRule(
  4. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  5. : GeneratorRule(zConfig, zMemory)
  6. {
  7. blockType = BlockType::getTypeId(
  8. zConfig->asObject()->zValue("blockType")->asString()->getString());
  9. }
  10. Framework::Either<Block*, int> BlockInstanceGeneratorRule::createBlock(
  11. int x, int y, int z, int dimensionId)
  12. {
  13. return Game::INSTANCE->zBlockType(blockType)->createBlockAt(
  14. Framework::Vec3<int>(x, y, z), dimensionId, 0);
  15. }
  16. BlockInstanceGeneratorRuleFactory::BlockInstanceGeneratorRuleFactory()
  17. : ReferenceCounter()
  18. {}
  19. GeneratorRule* BlockInstanceGeneratorRuleFactory::createRule(
  20. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  21. {
  22. return new BlockInstanceGeneratorRule(zConfig, zMemory);
  23. }
  24. Framework::JSON::Validator::JSONValidator*
  25. BlockInstanceGeneratorRuleFactory::getValidator()
  26. {
  27. Framework::RCArray<Framework::Text> blockTypeNames;
  28. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  29. {
  30. if (Game::INSTANCE->zBlockType(i))
  31. {
  32. blockTypeNames.add(
  33. new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  34. }
  35. }
  36. return GeneratorRule::addToValidator(
  37. Framework::JSON::Validator::JSONValidator::buildForObject()
  38. ->withRequiredString("type")
  39. ->withExactMatch("blockInstance")
  40. ->finishString()
  41. ->withRequiredString("blockType")
  42. ->whichIsOneOf(blockTypeNames)
  43. ->finishString())
  44. ->finishObject();
  45. }