BlockInstanceGeneratorRule.cpp 2.0 KB

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