BlockInstanceGeneratorRule.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "BlockInstanceGeneratorRule.h"
  2. #include "Game.h"
  3. BlockInstanceGeneratorRule::BlockInstanceGeneratorRule()
  4. : GeneratorRule(),
  5. blockType(0)
  6. {}
  7. Framework::Either<Block*, int> BlockInstanceGeneratorRule::createBlock(
  8. int x, int y, int z, int dimensionId)
  9. {
  10. return Game::INSTANCE->zBlockType(blockType)->createBlockAt(
  11. Framework::Vec3<int>(x, y, z), dimensionId, 0);
  12. }
  13. void BlockInstanceGeneratorRule::setBlockTypeId(int blockType)
  14. {
  15. this->blockType = blockType;
  16. }
  17. int BlockInstanceGeneratorRule::getBlockTypeId() const
  18. {
  19. return blockType;
  20. }
  21. BlockInstanceGeneratorRuleFactory::BlockInstanceGeneratorRuleFactory()
  22. : GeneratorRuleFactory()
  23. {}
  24. BlockInstanceGeneratorRule* BlockInstanceGeneratorRuleFactory::createValue(
  25. Framework::JSON::JSONObject* zJson) const
  26. {
  27. return new BlockInstanceGeneratorRule();
  28. }
  29. BlockInstanceGeneratorRule* BlockInstanceGeneratorRuleFactory::fromJson(
  30. Framework::JSON::JSONObject* zJson) const
  31. {
  32. BlockInstanceGeneratorRule* result = GeneratorRuleFactory::fromJson(zJson);
  33. result->setBlockTypeId(Game::INSTANCE->getBlockTypeId(
  34. zJson->zValue("blockType")->asString()->getString()));
  35. return result;
  36. }
  37. Framework::JSON::JSONObject* BlockInstanceGeneratorRuleFactory::toJsonObject(
  38. BlockInstanceGeneratorRule* zObject) const
  39. {
  40. Framework::JSON::JSONObject* result
  41. = GeneratorRuleFactory::toJsonObject(zObject);
  42. result->addValue("blockType",
  43. new Framework::JSON::JSONString(
  44. Game::INSTANCE->zBlockType(zObject->getBlockTypeId())->getName()));
  45. return result;
  46. }
  47. JSONObjectValidationBuilder* BlockInstanceGeneratorRuleFactory::addToValidator(
  48. JSONObjectValidationBuilder* builder) const
  49. {
  50. return GeneratorRuleFactory::addToValidator(
  51. builder->withRequiredAttribute("blockType",
  52. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  53. BlockTypeNameFactory::TYPE_ID)));
  54. }
  55. const char* BlockInstanceGeneratorRuleFactory::getTypeToken() const
  56. {
  57. return "blockInstance";
  58. }