BlockInstanceGeneratorRule.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. void BlockInstanceGeneratorRuleFactory::fromJson(
  30. BlockInstanceGeneratorRule* zResult,
  31. Framework::JSON::JSONObject* zJson) const
  32. {
  33. zResult->setBlockTypeId(Game::INSTANCE->getBlockTypeId(
  34. zJson->zValue("blockType")->asString()->getString()));
  35. GeneratorRuleFactory::fromJson(zResult, zJson);
  36. }
  37. void BlockInstanceGeneratorRuleFactory::toJson(
  38. BlockInstanceGeneratorRule* zObject,
  39. Framework::JSON::JSONObject* zResult) const
  40. {
  41. zResult->addValue("blockType",
  42. new Framework::JSON::JSONString(
  43. Game::INSTANCE->zBlockType(zObject->getBlockTypeId())->getName()));
  44. GeneratorRuleFactory::toJson(zObject, zResult);
  45. }
  46. JSONObjectValidationBuilder* BlockInstanceGeneratorRuleFactory::addToValidator(
  47. JSONObjectValidationBuilder* builder) const
  48. {
  49. Framework::RCArray<Framework::Text> blockTypeNames;
  50. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  51. {
  52. if (Game::INSTANCE->zBlockType(i))
  53. {
  54. blockTypeNames.add(
  55. new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  56. }
  57. }
  58. return GeneratorRuleFactory::addToValidator(
  59. builder->withRequiredString("blockType")
  60. ->whichIsOneOf(blockTypeNames)
  61. ->finishString());
  62. }
  63. Framework::Text BlockInstanceGeneratorRuleFactory::getTypeToken() const
  64. {
  65. return "blockInstance";
  66. }