BlockInstanceGeneratorRule.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. Framework::RCArray<Framework::Text> blockTypeNames;
  51. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  52. {
  53. if (Game::INSTANCE->zBlockType(i))
  54. {
  55. blockTypeNames.add(
  56. new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  57. }
  58. }
  59. return GeneratorRuleFactory::addToValidator(
  60. builder->withRequiredString("blockType")
  61. ->whichIsOneOf(blockTypeNames)
  62. ->finishString());
  63. }
  64. const char* BlockInstanceGeneratorRuleFactory::getTypeToken() const
  65. {
  66. return "blockInstance";
  67. }