123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "Grass.h"
- #include "AddEntityUpdate.h"
- #include "Game.h"
- #include "ItemEntity.h"
- GrassBlock::GrassBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
- : AdditionalItemSpawningBlock(typeId, pos, dimensionId)
- {
- tickSource = 1;
- transparent = 1;
- }
- bool GrassBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- // TODO: spread to neighbor blocks if light level is hight enought
- return 0;
- }
- void GrassBlock::onPostTick() {}
- void GrassBlock::filterPassingLight(unsigned char rgb[3]) const
- {
- rgb[0] = (unsigned char)(rgb[0] * 0.7);
- rgb[1] = (unsigned char)(rgb[1] * 0.9);
- rgb[2] = (unsigned char)(rgb[2] * 0.7);
- }
- GrassBlockType::GrassBlockType(ModelInfo* model,
- Framework::Text name,
- int mapColor,
- Framework::Array<SpawnConfig> spawns,
- Framework::RCArray<Framework::Text> groupNames)
- : AdditionalItemSpawningBlockType(
- "", model, name, mapColor, false, 0.1f, spawns, groupNames)
- {
- setTransparent(true);
- setPassable(true);
- setSpeedModifier(0.5f);
- }
- ItemType* GrassBlockType::createItemType() const
- {
- return 0;
- }
- void GrassBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
- {
- AdditionalItemSpawningBlockType::createSuperBlock(zBlock, zItem);
- }
- void GrassBlockType::loadSuperBlock(
- Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
- {
- AdditionalItemSpawningBlockType::loadSuperBlock(
- zBlock, zReader, dimensionId);
- }
- void GrassBlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- AdditionalItemSpawningBlockType::saveSuperBlock(zBlock, zWriter);
- }
- Item* GrassBlockType::createItem() const
- {
- return 0;
- }
- Block* GrassBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- return new GrassBlock(getId(), position, dimensionId);
- }
- GrassBlockTypeFactory::GrassBlockTypeFactory()
- : SubTypeFactory()
- {}
- GrassBlockType* GrassBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- Framework::Array<SpawnConfig> spawns;
- Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
- for (int i = 0; i < spawnsJson->getLength(); i++)
- {
- Framework::JSON::JSONObject* spawnJson
- = spawnsJson->zValue(i)->asObject();
- spawns.add(SpawnConfig{
- (int)spawnJson->zValue("min")->asNumber()->getNumber(),
- (int)spawnJson->zValue("max")->asNumber()->getNumber(),
- (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
- spawnJson->zValue("itemType")->asString()->getString(),
- 0,
- });
- }
- return new GrassBlockType(
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- spawns,
- groupNames);
- }
- Framework::JSON::JSONObject* GrassBlockTypeFactory::toJson(
- GrassBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
- for (const SpawnConfig& config : zObject->getSpawns())
- {
- Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
- spawn->addValue(
- "itemType", new Framework::JSON::JSONString(config.itemTypeName));
- spawn->addValue(
- "chance", new Framework::JSON::JSONNumber(config.chance));
- spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
- spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
- spawns->addValue(spawn);
- }
- result->addValue("spawns", spawns);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* GrassBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredString("mapColor")
- ->finishString()
- ->withRequiredAttribute("spawns",
- JSON::Validator::JSONValidator::buildForArray()
- ->addAcceptedObjectInArray()
- ->withRequiredString("itemType")
- ->finishString()
- ->withRequiredNumber("chance")
- ->finishNumber()
- ->withRequiredNumber("min")
- ->finishNumber()
- ->withRequiredNumber("max")
- ->finishNumber()
- ->finishObject()
- ->finishArray())
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text GrassBlockTypeFactory::getTypeToken() const
- {
- return "grass";
- }
|