#include "TreeSeblingBlock.h" #include "BasicBlocks.h" #include "Game.h" #include "NoBlock.h" #include "RandNoise.h" #include "TreeTemplate.h" TreeSeblingBlock::TreeSeblingBlock(int typeId, const ItemType* zTool, Framework::Vec3 pos, int dimensionId, const BlockType* wood, const BlockType* leaves) : Block(typeId, zTool, pos, dimensionId, 0), seblingTicks(0), seblingTicksMax(10000), wood(wood), leaves(leaves) { tickSource = 1; } bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked) { int lastPercentage = (int)(seblingTicks / (float)seblingTicksMax * 100.f); seblingTicks += 1; if ((int)(seblingTicks / (float)seblingTicksMax * 100.f) != lastPercentage) { Game::INSTANCE->blockTargetChanged(this); } return 0; } void TreeSeblingBlock::onPostTick() { if ((int)seblingTicks >= seblingTicksMax) { Game::INSTANCE->doLater([wood = wood, leaves = leaves, pos = getPos(), dim = getDimensionId()]() { // the tree sebling object will be deleted during this operation RandNoise noise((int)time(0)); if (!Game::INSTANCE->zGenerator()->spawnStructure(pos, dim, [wood = wood, leaves = leaves](GeneratorTemplate* tmpl) { TreeTemplate* tree = dynamic_cast(tmpl); return tree && tree->getWoodType() == wood && tree->getLeavesType() == leaves; })) { Game::INSTANCE->zDimension(dim)->placeBlock( pos, BlockTypeEnum::AIR); } }); } } Framework::Text TreeSeblingBlock::getTargetUIML() { return Text("") + StaticRegistry::INSTANCE.zElement(typeId)->getName() + "\n" + "Growth: " + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f)) + "%"; } TreeSeblingBlockType::TreeSeblingBlockType(int typeId, int itemTypeId, ModelInfo model, int woodType, int leavesType, const char* name, int mapColor) : BlockType(typeId, 0, model, 1, 10, 0, name, false, mapColor), itemType(itemTypeId), transparent(true), passable(true), hardness(0.1f), zTool(0), speedModifier(0.5f), interactable(1), woodType(woodType), leavesType(leavesType) {} void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const { TreeSeblingBlock* block = dynamic_cast(zBlock); block->transparent = transparent; block->passable = passable; block->hp = (float)getInitialMaxHP(); block->maxHP = (float)getInitialMaxHP(); block->hardness = hardness; block->zTool = zTool; block->speedModifier = speedModifier; block->interactable = interactable; BlockType::createSuperBlock(zBlock, zItem); } void TreeSeblingBlockType::loadSuperBlock( Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const { TreeSeblingBlock* block = dynamic_cast(zBlock); zReader->lese((char*)&block->seblingTicks, 4); zReader->lese((char*)&block->seblingTicksMax, 4); int id; zReader->lese((char*)&id, 4); block->wood = StaticRegistry::INSTANCE.zElement(id); zReader->lese((char*)&id, 4); block->leaves = StaticRegistry::INSTANCE.zElement(id); BlockType::loadSuperBlock(zBlock, zReader, dimensionId); } void TreeSeblingBlockType::saveSuperBlock( Block* zBlock, Framework::StreamWriter* zWriter) const { TreeSeblingBlock* block = dynamic_cast(zBlock); zWriter->schreibe((char*)&block->seblingTicks, 4); zWriter->schreibe((char*)&block->seblingTicksMax, 4); int id = block->wood->getId(); zWriter->schreibe((char*)&id, 4); id = block->leaves->getId(); zWriter->schreibe((char*)&id, 4); BlockType::saveSuperBlock(zBlock, zWriter); } Item* TreeSeblingBlockType::createItem() const { return StaticRegistry::INSTANCE.zElement(itemType)->createItem(); } Block* TreeSeblingBlockType::createBlock( Framework::Vec3 position, int dimensionId) const { return new TreeSeblingBlock(getId(), zTool, position, dimensionId, StaticRegistry::INSTANCE.zElement(woodType), StaticRegistry::INSTANCE.zElement(leavesType)); } TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness) { this->hardness = hardness; return this; }