#include "BasicBlocks.h" #include "Game.h" #include "ItemEntity.h" #include "ItemStack.h" #include "ModelInfo.h" #include "TreeSeblingBlock.h" BasicBlock::BasicBlock(int typeId, Framework::Vec3 pos, int dimensionId) : BasicBlock(typeId, pos, dimensionId, false) {} BasicBlock::BasicBlock( int typeId, Framework::Vec3 pos, int dimensionId, bool hasInventory) : Block(typeId, pos, dimensionId, hasInventory) {} bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked) { return 0; } void BasicBlock::onPostTick() {} BasicBlockType::BasicBlockType() : BlockType(), itemTypeName(), transparent(0), passable(0), speedModifier(1.f), interactable(1) {} bool BasicBlockType::initialize(Game* zGame) { if (itemTypeName.getLength()) { itemTypeId = zGame->getItemTypeId(itemTypeName); } else { itemTypeId = 0; } return itemTypeId >= 0 && BlockType::initialize(zGame); } void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const { BasicBlock* block = dynamic_cast(zBlock); block->transparent = transparent; block->passable = passable; block->hp = (float)getInitialMaxHP(); block->maxHP = (float)getInitialMaxHP(); block->hardness = getHardness(); block->speedModifier = speedModifier; block->interactable = interactable; BlockType::createSuperBlock(zBlock, zItem); } Block* BasicBlockType::createBlock( Framework::Vec3 position, int dimensionId) const { return new BasicBlock(getId(), position, dimensionId); } Item* BasicBlockType::createItem() const { if (getItemTypeName().istGleich("")) { return 0; } return Game::INSTANCE->zItemType(itemTypeId)->createItem(); } Framework::Text BasicBlockType::getItemTypeName() const { return itemTypeName; } ItemType* BasicBlockType::createItemType() const { if (getItemTypeName().istGleich("")) { return 0; } return new BasicBlockItemType(getItemTypeName(), new ModelInfo(zModel()->getModelPath(), zModel()->getTexturePaths(), zModel()->isTransparent(), zModel()->getSize() / 2.f), transparent, passable, getHardness(), speedModifier, getItemTypeName(), 0, 50, getGroupNames()); } void BasicBlockType::setItemTypeName(Framework::Text itemTypeName) { this->itemTypeName = itemTypeName; } int BasicBlockType::getItemTypeId() const { return itemTypeId; } void BasicBlockType::setTransparent(bool transparent) { this->transparent = transparent; } bool BasicBlockType::isTransparent() const { return transparent; } void BasicBlockType::setPassable(bool passable) { this->passable = passable; } bool BasicBlockType::isPassable() const { return passable; } void BasicBlockType::setSpeedModifier(float speedModifier) { this->speedModifier = speedModifier; } float BasicBlockType::getSpeedModifier() const { return speedModifier; } void BasicBlockType::setInteractable(bool interactable) { this->interactable = interactable; } bool BasicBlockType::isInteractable() const { return interactable; } BasicBlockTypeFactory::BasicBlockTypeFactory() : BlockTypeFactoryBase() {} BasicBlockType* BasicBlockTypeFactory::createValue( Framework::JSON::JSONObject* zJson) const { return new BasicBlockType(); } BasicBlockType* BasicBlockTypeFactory::fromJson( Framework::JSON::JSONObject* zJson) const { BasicBlockType* result = BlockTypeFactoryBase::fromJson(zJson); if (zJson->zValue("itemType")->getType() == Framework::AbstractType::STRING) { result->setItemTypeName( zJson->zValue("itemType")->asString()->getString()); } else { result->setItemTypeName(""); } result->setTransparent(zJson->zValue("transparent")->asBool()->getBool()); result->setPassable(zJson->zValue("passable")->asBool()->getBool()); result->setSpeedModifier( (float)zJson->zValue("speedModifier")->asNumber()->getNumber()); result->setInteractable( (float)zJson->zValue("interactable")->asBool()->getBool()); return result; } Framework::JSON::JSONObject* BasicBlockTypeFactory::toJsonObject( BasicBlockType* zObject) const { Framework::JSON::JSONObject* result = BlockTypeFactoryBase::toJsonObject(zObject); if (zObject->getItemTypeName().istGleich("")) { result->addValue("itemType", new Framework::JSON::JSONValue()); } else { result->addValue("itemType", new Framework::JSON::JSONString(zObject->getItemTypeName())); } result->addValue( "transparent", new Framework::JSON::JSONBool(zObject->isTransparent())); result->addValue( "passable", new Framework::JSON::JSONBool(zObject->isPassable())); result->addValue("speedModifier", new Framework::JSON::JSONNumber(zObject->getSpeedModifier())); result->addValue("interactable", new Framework::JSON::JSONBool(zObject->isInteractable())); return result; } JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return BlockTypeFactoryBase::addToValidator( builder ->withRequiredAttribute("itemType", Game::INSTANCE->zTypeRegistry()->getValidator( ItemTypeNameFactory::TYPE_ID), true, false) ->withRequiredBool("transparent") ->withDefault(false) ->finishBool() ->withRequiredBool("passable") ->withDefault(false) ->finishBool() ->withRequiredNumber("speedModifier") ->withDefault(1.0) ->finishNumber() ->withRequiredBool("interactable") ->withDefault(true) ->finishBool()); } const char* BasicBlockTypeFactory::getTypeToken() const { return "basicBlock"; } AdditionalItemSpawningBlock::AdditionalItemSpawningBlock( int typeId, Framework::Vec3 pos, int dimensionId) : BasicBlock(typeId, pos, dimensionId) {} void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config) { spawns.add(config); } void AdditionalItemSpawningBlock::onDestroy() { for (const SpawnConfig& config : spawns) { if ((double)rand() / RAND_MAX < config.chance) { int amount = config.min + (int)((config.max - config.min) * ((double)rand() / RAND_MAX)); if (amount > 0) { ItemStack* spawnedItems = Game::INSTANCE->zItemType(config.typeId) ->createItemStack(amount); if (spawnedItems) { Game::INSTANCE->spawnItem( location + Framework::Vec3(0.5f, 0.5f, 0.5f), getDimensionId(), spawnedItems); } } } } BasicBlock::onDestroy(); } AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType() : BasicBlockType(), spawns() {} void AdditionalItemSpawningBlockType::createSuperBlock( Block* zBlock, Item* zItem) const { AdditionalItemSpawningBlock* block = dynamic_cast(zBlock); if (block) { for (const SpawnConfig& config : spawns) { block->addSpawn(config); } } BasicBlockType::createSuperBlock(zBlock, zItem); } bool AdditionalItemSpawningBlockType::initialize(Game* zGame) { for (auto iterator = spawns.begin(); iterator; iterator++) { int itemTypeId = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName); if (itemTypeId < 0) { return false; } iterator.set({iterator.val().min, iterator.val().max, iterator.val().chance, iterator.val().itemTypeName, itemTypeId}); } return BasicBlockType::initialize(zGame); } Block* AdditionalItemSpawningBlockType::createBlock( Framework::Vec3 position, int dimensionId) const { AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(getId(), position, dimensionId); return block; } void AdditionalItemSpawningBlockType::addSpawn(SpawnConfig config) { spawns.add(config); } const Framework::Array& AdditionalItemSpawningBlockType::getSpawns() const { return spawns; } AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory() : BasicBlockTypeFactory() {} BasicBlockType* AdditionalItemSpawningBlockTypeFactory::createValue( Framework::JSON::JSONObject* zJson) const { return new AdditionalItemSpawningBlockType(); } BasicBlockType* AdditionalItemSpawningBlockTypeFactory::fromJson( Framework::JSON::JSONObject* zJson) const { AdditionalItemSpawningBlockType* result = dynamic_cast( BasicBlockTypeFactory::fromJson(zJson)); Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray(); for (int i = 0; i < spawnsJson->getLength(); i++) { Framework::JSON::JSONObject* spawnJson = spawnsJson->zValue(i)->asObject(); result->addSpawn(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 result; } Framework::JSON::JSONObject* AdditionalItemSpawningBlockTypeFactory::toJsonObject( BasicBlockType* zObject) const { AdditionalItemSpawningBlockType* zType = dynamic_cast(zObject); Framework::JSON::JSONObject* result = BasicBlockTypeFactory::toJsonObject(zObject); Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray(); for (const SpawnConfig& config : zType->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; } JSONObjectValidationBuilder* AdditionalItemSpawningBlockTypeFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return BasicBlockTypeFactory::addToValidator(builder->withRequiredAttribute( "spawns", Framework::Validator::DataValidator::buildForArray() ->addAcceptedObjectInArray() ->withRequiredAttribute("itemType", Game::INSTANCE->zTypeRegistry()->getValidator( ItemTypeNameFactory::TYPE_ID)) ->withRequiredNumber("chance") ->finishNumber() ->withRequiredNumber("min") ->finishNumber() ->withRequiredNumber("max") ->finishNumber() ->finishObject() ->finishArray())); } const char* AdditionalItemSpawningBlockTypeFactory::getTypeToken() const { return "additionalItemsBlockType"; } const char* AdditionalItemSpawningBlockTypeFactory::getTypeName() const { return typeid(AdditionalItemSpawningBlockType).name(); }