123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #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<int> pos,
- int dimensionId,
- int maxTicks,
- const char* name,
- int blockTypeAfterGrowth)
- : Block(typeId, zTool, pos, dimensionId, 0),
- seblingTicks(0),
- seblingTicksMax(maxTicks),
- name(name),
- states(),
- blockTypeAfterGrowth(blockTypeAfterGrowth),
- plantSpawned(0),
- lastSendState(-1)
- {
- 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);
- }
- int index = 0;
- int currentIndex = 0;
- for (const GrowthState& state : states)
- {
- if (state.percentage <= seblingTicks / (float)seblingTicksMax)
- {
- currentIndex = index;
- }
- else
- {
- break;
- }
- index++;
- }
- if (lastSendState != currentIndex)
- {
- updateModel(states.get(currentIndex).model);
- lastSendState = currentIndex;
- }
- 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)
- {
- GrowthState current;
- bool found = 0;
- for (const GrowthState& state : states)
- {
- if (state.percentage <= seblingTicks / (float)seblingTicksMax)
- {
- found = 1;
- current = state;
- }
- }
- if (found)
- {
- 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("<targetInfo><text width=\"auto\" height=\"auto\">") + name
- + "\n" + "Growth: "
- + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
- + "%</text></targetInfo>";
- }
- 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<GrowingPlantBlock*>(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<GrowingPlantBlock*>(zBlock);
- zReader->lese((char*)&block->seblingTicks, 4);
- }
- void GrowingPlantBlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- BlockType::saveSuperBlock(zBlock, zWriter);
- GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
- zWriter->schreibe((char*)&block->seblingTicks, 4);
- }
- Item* GrowingPlantBlockType::createItem() const
- {
- return 0;
- }
- Block* GrowingPlantBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
- zTool,
- position,
- dimensionId,
- ticksNeeded,
- readableName,
- blockTypeAfterGrowth);
- for (const GrowthState& state : states)
- {
- block->addGrowthState(state);
- }
- return block;
- }
|