123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #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<int> pos,
- const BlockType* wood,
- const BlockType* leaves)
- : Block(typeId, zTool, pos, 0), wood(wood), leaves(leaves)
- {
- tickSource = 1;
- }
- bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- seblingTicks += 1;
- 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](GenerationTemplate* tmpl) {
- TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
- return tree && tree->getWoodType() == wood
- && tree->getLeavesType() == leaves;
- }))
- {
- Game::INSTANCE->zDimension(dim)->placeBlock(
- pos, BlockTypeEnum::AIR);
- }
- });
- }
- }
- TreeSeblingBlockType::TreeSeblingBlockType(
- int typeId, int itemTypeId, ModelInfo model, int woodType, int leavesType)
- : BlockType(typeId, 0, model, 1, 10, 0), 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<TreeSeblingBlock*>(zBlock);
- if (!block)
- throw "TreeSeblingBlockType::createSuperBlock was called with a block "
- "witch is not an instance of TreeSeblingBlock";
- 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<TreeSeblingBlock*>(zBlock);
- if (!block)
- throw "TreeSeblingBlockType::loadSuperBlock was called with a block "
- "witch is not an instance of TreeSeblingBlock";
- zReader->lese((char*)&block->seblingTicks, 4);
- zReader->lese((char*)&block->seblingTicksMax, 4);
- int id;
- zReader->lese((char*)&id, 4);
- block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
- zReader->lese((char*)&id, 4);
- block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
- BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
- }
- void TreeSeblingBlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
- if (!block)
- throw "TreeSeblingBlockType::saveSuperBlock was called with a block "
- "witch is not an instance of TreeSeblingBlock";
- 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<ItemType>::INSTANCE.zElement(itemType)->createItem();
- }
- Block* TreeSeblingBlockType::createBlock(Framework::Vec3<int> position) const
- {
- return new TreeSeblingBlock(getId(),
- zTool,
- position,
- StaticRegistry<BlockType>::INSTANCE.zElement(woodType),
- StaticRegistry<BlockType>::INSTANCE.zElement(leavesType));
- }
- TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness)
- {
- this->hardness = hardness;
- return this;
- }
|