Grass.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "Grass.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. GrassBlock::GrassBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  6. : AdditionalItemSpawningBlock(typeId, pos, dimensionId)
  7. {
  8. tickSource = 1;
  9. transparent = 1;
  10. }
  11. bool GrassBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  12. {
  13. // TODO: spread to neighbor blocks if light level is hight enought
  14. return 0;
  15. }
  16. void GrassBlock::onPostTick() {}
  17. void GrassBlock::filterPassingLight(unsigned char rgb[3]) const
  18. {
  19. rgb[0] = (unsigned char)(rgb[0] * 0.7);
  20. rgb[1] = (unsigned char)(rgb[1] * 0.9);
  21. rgb[2] = (unsigned char)(rgb[2] * 0.7);
  22. }
  23. GrassBlockType::GrassBlockType(ModelInfo* model,
  24. Framework::Text name,
  25. int mapColor,
  26. Framework::Array<SpawnConfig> spawns,
  27. Framework::RCArray<Framework::Text> groupNames)
  28. : AdditionalItemSpawningBlockType(
  29. "", model, name, mapColor, false, 0.1f, spawns, groupNames)
  30. {
  31. setTransparent(true);
  32. setPassable(true);
  33. setSpeedModifier(0.5f);
  34. }
  35. ItemType* GrassBlockType::createItemType() const
  36. {
  37. return 0;
  38. }
  39. void GrassBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  40. {
  41. AdditionalItemSpawningBlockType::createSuperBlock(zBlock, zItem);
  42. }
  43. void GrassBlockType::loadSuperBlock(
  44. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  45. {
  46. AdditionalItemSpawningBlockType::loadSuperBlock(
  47. zBlock, zReader, dimensionId);
  48. }
  49. void GrassBlockType::saveSuperBlock(
  50. Block* zBlock, Framework::StreamWriter* zWriter) const
  51. {
  52. AdditionalItemSpawningBlockType::saveSuperBlock(zBlock, zWriter);
  53. }
  54. Item* GrassBlockType::createItem() const
  55. {
  56. return 0;
  57. }
  58. Block* GrassBlockType::createBlock(
  59. Framework::Vec3<int> position, int dimensionId) const
  60. {
  61. return new GrassBlock(getId(), position, dimensionId);
  62. }
  63. GrassBlockTypeFactory::GrassBlockTypeFactory()
  64. : SubTypeFactory()
  65. {}
  66. GrassBlockType* GrassBlockTypeFactory::fromJson(
  67. Framework::JSON::JSONObject* zJson) const
  68. {
  69. Framework::RCArray<Framework::Text> groupNames;
  70. for (Framework::JSON::JSONValue* value :
  71. *zJson->zValue("groupNames")->asArray())
  72. {
  73. groupNames.add(new Framework::Text(value->asString()->getString()));
  74. }
  75. Framework::Array<SpawnConfig> spawns;
  76. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  77. for (int i = 0; i < spawnsJson->getLength(); i++)
  78. {
  79. Framework::JSON::JSONObject* spawnJson
  80. = spawnsJson->zValue(i)->asObject();
  81. spawns.add(SpawnConfig{
  82. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  83. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  84. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  85. spawnJson->zValue("itemType")->asString()->getString(),
  86. 0,
  87. });
  88. }
  89. return new GrassBlockType(
  90. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  91. zJson->zValue("model")),
  92. zJson->zValue("name")->asString()->getString(),
  93. (int)zJson->zValue("mapColor")->asString()->getString(),
  94. spawns,
  95. groupNames);
  96. }
  97. Framework::JSON::JSONObject* GrassBlockTypeFactory::toJson(
  98. GrassBlockType* zObject) const
  99. {
  100. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  101. result->addValue(
  102. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  103. result->addValue(
  104. "name", new Framework::JSON::JSONString(zObject->getName()));
  105. result->addValue(
  106. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  107. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  108. for (Framework::Text* groupName : zObject->getGroupNames())
  109. {
  110. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  111. }
  112. result->addValue("groupNames", groupNames);
  113. Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
  114. for (const SpawnConfig& config : zObject->getSpawns())
  115. {
  116. Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
  117. spawn->addValue(
  118. "itemType", new Framework::JSON::JSONString(config.itemTypeName));
  119. spawn->addValue(
  120. "chance", new Framework::JSON::JSONNumber(config.chance));
  121. spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
  122. spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
  123. spawns->addValue(spawn);
  124. }
  125. result->addValue("spawns", spawns);
  126. return result;
  127. }
  128. Framework::JSON::Validator::JSONValidator* GrassBlockTypeFactory::getValidator(
  129. Framework::JSON::Validator::ObjectValidationBuilder<
  130. Framework::JSON::Validator::JSONValidator>* builder) const
  131. {
  132. return builder
  133. ->withRequiredAttribute(
  134. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  135. ->withRequiredString("name")
  136. ->finishString()
  137. ->withRequiredString("mapColor")
  138. ->finishString()
  139. ->withRequiredAttribute("spawns",
  140. JSON::Validator::JSONValidator::buildForArray()
  141. ->addAcceptedObjectInArray()
  142. ->withRequiredString("itemType")
  143. ->finishString()
  144. ->withRequiredNumber("chance")
  145. ->finishNumber()
  146. ->withRequiredNumber("min")
  147. ->finishNumber()
  148. ->withRequiredNumber("max")
  149. ->finishNumber()
  150. ->finishObject()
  151. ->finishArray())
  152. ->withRequiredArray("groupNames")
  153. ->withDefault(new Framework::JSON::JSONArray())
  154. ->addAcceptedStringInArray()
  155. ->finishString()
  156. ->finishArray()
  157. ->finishObject();
  158. }
  159. Framework::Text GrassBlockTypeFactory::getTypeToken() const
  160. {
  161. return "grass";
  162. }