#include "GrowingPlant.h" #include "Game.h" GrowthState::GrowthState() : percentage(1), model("", "", 0) {} GrowthState::GrowthState(float percentage, ModelInfo model) : percentage(percentage), model(model) {} GrowthState::GrowthState(const GrowthState& right) : percentage(right.percentage), model(right.model) {} GrowthState& GrowthState::operator=(const GrowthState& right) { percentage = right.percentage; model = ModelInfo(right.model); return *this; } GrowingPlantBlock::GrowingPlantBlock(int typeId, const ItemType* zTool, Framework::Vec3 pos, int maxTicks, const char* name, int blockTypeAfterGrowth) : Block(typeId, zTool, pos, 0), seblingTicks(0), seblingTicksMax(maxTicks), name(name), states(), blockTypeAfterGrowth(blockTypeAfterGrowth), plantSpawned(0) { tickSource = 1; } bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked) { float beforePercentage = seblingTicks / (float)seblingTicksMax; seblingTicks += (float)numTicks; if ((int)(seblingTicks / (float)seblingTicksMax * 100.f) != (int)(beforePercentage * 100.f)) { Game::INSTANCE->blockTargetChanged(this); } for (const GrowthState& state : states) { if ((state.percentage > beforePercentage) && state.percentage <= seblingTicks / (float)seblingTicksMax) { updateModel(state.model); } } return 1; } void GrowingPlantBlock::onPostTick() { if (seblingTicks >= (float)seblingTicksMax && !plantSpawned) { plantSpawned = 1; Game::INSTANCE->doLater([this]() { Game::INSTANCE->zDimension(getDimensionId()) ->placeBlock(getPos(), blockTypeAfterGrowth); }); } } void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage) { const GrowthState* current = 0; for (const GrowthState& state : states) { if (state.percentage <= seblingTicks / (float)seblingTicksMax) { current = &state; } } if (current) { zMessage->addressBlock(this); InMemoryBuffer buffer; current->model.writeTo(&buffer); char* msg = new char[(int)buffer.getSize() + 1]; msg[0] = 1; // hmodel change buffer.lese(msg + 1, (int)buffer.getSize()); zMessage->setMessage(msg, (int)buffer.getSize() + 1); } } Framework::Text GrowingPlantBlock::getTargetUIML() { return Text("") + name + "\n" + "Growth: " + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f)) + "%"; } GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState state) { int index = 0; for (const GrowthState& s : states) { if (s.percentage > state.percentage) { states.add(state, index); return this; } index++; } states.add(state); return this; } GrowingPlantBlockType::GrowingPlantBlockType(int typeId, ModelInfo model, const char* name, int blockTypeAfterGrowth, const char* readableName, int ticksNeeded, int mapColor) : BlockType(typeId, 0, model, 1, 10, 0, name, true, mapColor), transparent(1), passable(1), hardness(0.1f), zTool(0), speedModifier(0.3f), interactable(1), states(), blockTypeAfterGrowth(blockTypeAfterGrowth), readableName(readableName), ticksNeeded(ticksNeeded) {} GrowingPlantBlockType* GrowingPlantBlockType::setHardness(float hardness) { this->hardness = hardness; return this; } GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState( float growthPercentage, ModelInfo model) { states.add(GrowthState(growthPercentage, model)); return this; } void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const { GrowingPlantBlock* block = dynamic_cast(zBlock); block->transparent = transparent; block->passable = passable; block->hardness = hardness; block->zTool = zTool; block->speedModifier = speedModifier; block->interactable = interactable; BlockType::createSuperBlock(zBlock, zItem); } void GrowingPlantBlockType::loadSuperBlock( Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const { BlockType::loadSuperBlock(zBlock, zReader, dimensionId); GrowingPlantBlock* block = dynamic_cast(zBlock); zReader->lese((char*)&block->seblingTicks, 4); } void GrowingPlantBlockType::saveSuperBlock( Block* zBlock, Framework::StreamWriter* zWriter) const { BlockType::saveSuperBlock(zBlock, zWriter); GrowingPlantBlock* block = dynamic_cast(zBlock); zWriter->schreibe((char*)&block->seblingTicks, 4); } Item* GrowingPlantBlockType::createItem() const { return 0; } Block* GrowingPlantBlockType::createBlock(Framework::Vec3 position) const { GrowingPlantBlock* block = new GrowingPlantBlock(getId(), zTool, position, ticksNeeded, readableName, blockTypeAfterGrowth); for (const GrowthState& state : states) { block->addGrowthState(state); } return block; }