123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- #include "GrowingPlant.h"
- #include "Dimension.h"
- #include "Game.h"
- GrowthState::GrowthState(float percentage, ModelInfo* model)
- : ReferenceCounter(),
- percentage(percentage),
- model(model)
- {}
- GrowthState::~GrowthState()
- {
- model->release();
- }
- float GrowthState::getPercentage() const
- {
- return percentage;
- }
- ModelInfo* GrowthState::zModel() const
- {
- return model;
- }
- GrowingPlantBlock::GrowingPlantBlock(int typeId,
- Framework::Vec3<int> pos,
- int dimensionId,
- int maxTicks,
- Framework::Text name,
- int blockTypeAfterGrowth)
- : Block(typeId, pos, dimensionId, 0),
- seblingTicks(0),
- seblingTicksMax(maxTicks),
- name(name),
- states(),
- blockTypeAfterGrowth(blockTypeAfterGrowth),
- plantSpawned(0),
- lastSendState(-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 (GrowthState* state : states)
- {
- if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
- {
- currentIndex = index;
- }
- else
- {
- break;
- }
- index++;
- }
- if (lastSendState != currentIndex)
- {
- updateModel(states.z(currentIndex)->zModel());
- 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 = 0;
- for (GrowthState* state : states)
- {
- if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
- {
- current = state;
- }
- }
- if (current)
- {
- zMessage->addressBlock(this);
- Framework::InMemoryBuffer buffer;
- current->zModel()->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);
- }
- }
- TickSourceType GrowingPlantBlock::isTickSource() const
- {
- return TickSourceType::EACH_TICK;
- }
- Framework::Text GrowingPlantBlock::getTargetUIML()
- {
- return Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
- + name + "\n" + "Growth: "
- + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
- + "%</text></targetInfo>";
- }
- GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState* state)
- {
- int index = 0;
- for (GrowthState* s : states)
- {
- if (s->getPercentage() > state->getPercentage())
- {
- states.add(state, index);
- return this;
- }
- index++;
- }
- states.add(state);
- return this;
- }
- GrowingPlantBlockType::GrowingPlantBlockType()
- : BlockType(),
- transparent(1),
- passable(1),
- speedModifier(0.3f),
- interactable(1),
- ticksNeeded(0)
- {}
- void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
- {
- GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
- block->transparent = transparent;
- block->passable = passable;
- block->hardness = getHardness();
- 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(),
- position,
- dimensionId,
- ticksNeeded,
- readableName,
- blockTypeIdAfterGrowth);
- for (GrowthState* state : states)
- {
- block->addGrowthState(dynamic_cast<GrowthState*>(state->getThis()));
- }
- return block;
- }
- GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
- float growthPercentage, ModelInfo* model)
- {
- states.add(new GrowthState(growthPercentage, model));
- return this;
- }
- Framework::Text GrowingPlantBlockType::getBlockTypeNameAfterGrowth() const
- {
- return blockTypeNameAfterGrowth;
- }
- void GrowingPlantBlockType::setReadableName(Framework::Text readableName)
- {
- this->readableName = readableName;
- }
- Framework::Text GrowingPlantBlockType::getReadableName() const
- {
- return readableName;
- }
- void GrowingPlantBlockType::setTicksNeeded(int ticksNeeded)
- {
- this->ticksNeeded = ticksNeeded;
- }
- int GrowingPlantBlockType::getTicksNeeded() const
- {
- return ticksNeeded;
- }
- void GrowingPlantBlockType::setTransparent(bool transparent)
- {
- this->transparent = transparent;
- }
- bool GrowingPlantBlockType::isTransparent() const
- {
- return transparent;
- }
- void GrowingPlantBlockType::setPassable(bool passable)
- {
- this->passable = passable;
- }
- bool GrowingPlantBlockType::isPassable() const
- {
- return passable;
- }
- void GrowingPlantBlockType::setSpeedModifier(float speedModifier)
- {
- this->speedModifier = speedModifier;
- }
- float GrowingPlantBlockType::getSpeedModifier() const
- {
- return speedModifier;
- }
- void GrowingPlantBlockType::setInteractable(bool interactable)
- {
- this->interactable = interactable;
- }
- bool GrowingPlantBlockType::isInteractable() const
- {
- return interactable;
- }
- const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
- {
- return states;
- }
- void GrowingPlantBlockType::setBlockTypeNameAfterGrowth(
- Framework::Text blockTypeIdAfterGrowth)
- {}
- ItemType* GrowingPlantBlockType::createItemType() const
- {
- return 0;
- }
- GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
- : BlockTypeFactoryBase()
- {}
- GrowingPlantBlockType* GrowingPlantBlockTypeFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new GrowingPlantBlockType();
- }
- void GrowingPlantBlockTypeFactory::fromJson(
- GrowingPlantBlockType* zResult, Framework::JSON::JSONObject* zJson) const
- {
- zResult->setBlockTypeNameAfterGrowth(
- zJson->zValue("blockTypeAfterGrowth")->asString()->getString());
- zResult->setReadableName(
- zJson->zValue("readableName")->asString()->getString());
- zResult->setTicksNeeded(
- (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber());
- zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
- zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
- zResult->setSpeedModifier(
- (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
- zResult->setInteractable(
- zJson->zValue("interactable")->asBool()->getBool());
- for (Framework::JSON::JSONValue* state :
- *zJson->zValue("states")->asArray())
- {
- zResult->addGrowthState((float)state->asObject()
- ->zValue("percentage")
- ->asNumber()
- ->getNumber(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- state->asObject()->zValue("model")));
- }
- BlockTypeFactoryBase::fromJson(zResult, zJson);
- }
- void GrowingPlantBlockTypeFactory::toJson(
- GrowingPlantBlockType* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue("readableName",
- new Framework::JSON::JSONString(zObject->getReadableName()));
- zResult->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- zResult->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- zResult->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- zResult->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- zResult->addValue("blockTypeAfterGrowth",
- new Framework::JSON::JSONString(
- zObject->getBlockTypeNameAfterGrowth()));
- zResult->addValue("ticksNeeded",
- new Framework::JSON::JSONNumber((double)zObject->getTicksNeeded()));
- Framework::JSON::JSONArray* states = new Framework::JSON::JSONArray();
- for (GrowthState* state : zObject->getStates())
- {
- Framework::JSON::JSONObject* stateObj
- = new Framework::JSON::JSONObject();
- stateObj->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(state->zModel()));
- stateObj->addValue("percentage",
- new Framework::JSON::JSONNumber(state->getPercentage()));
- states->addValue(stateObj);
- }
- zResult->addValue("states", states);
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- zResult->addValue("groupNames", groupNames);
- }
- JSONObjectValidationBuilder* GrowingPlantBlockTypeFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return BlockTypeFactoryBase::addToValidator(
- builder->withRequiredString("readableName")
- ->finishString()
- ->withRequiredNumber("ticksNeeded")
- ->finishNumber()
- ->withRequiredString("blockTypeAfterGrowth")
- ->finishString()
- ->withRequiredArray("states")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("percentage")
- ->whichIsGreaterOrEqual(0.0)
- ->whichIsLessOrEqual(1.0)
- ->finishNumber()
- ->withRequiredAttribute("model",
- Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->finishObject()
- ->finishArray()
- ->withRequiredBool("transparent")
- ->withDefault(true)
- ->finishBool()
- ->withRequiredBool("passable")
- ->withDefault(true)
- ->finishBool()
- ->withRequiredNumber("speedModifier")
- ->withDefault(0.5)
- ->finishNumber()
- ->withRequiredBool("interactable")
- ->withDefault(true)
- ->finishBool());
- }
- Framework::Text GrowingPlantBlockTypeFactory::getTypeToken() const
- {
- return "growingPlant";
- }
|