123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- #include "GrowingPlant.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)
- {
- 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 (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);
- 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);
- }
- }
- 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 (GrowthState* s : states)
- {
- if (s->getPercentage() > state->getPercentage())
- {
- states.add(state, index);
- return this;
- }
- index++;
- }
- states.add(state);
- return this;
- }
- GrowingPlantBlockType::GrowingPlantBlockType(ModelInfo* model,
- Framework::Text name,
- Framework::Text blockTypeNameAfterGrowth,
- const char* readableName,
- int ticksNeeded,
- int mapColor,
- float hardness,
- Framework::RCArray<Framework::Text> groupNames)
- : BlockType(
- 0, model, true, 10, false, name, true, mapColor, groupNames, hardness),
- transparent(1),
- passable(1),
- speedModifier(0.3f),
- interactable(1),
- states(),
- blockTypeNameAfterGrowth(blockTypeNameAfterGrowth),
- readableName(readableName),
- ticksNeeded(ticksNeeded)
- {}
- 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;
- }
- const char* GrowingPlantBlockType::getReadableName() const
- {
- return readableName;
- }
- int GrowingPlantBlockType::getTicksNeeded() const
- {
- return ticksNeeded;
- }
- const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
- {
- return states;
- }
- ItemType* GrowingPlantBlockType::createItemType() const
- {
- return 0;
- }
- GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
- : SubTypeFactory()
- {}
- GrowingPlantBlockType* GrowingPlantBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- GrowingPlantBlockType* result = new GrowingPlantBlockType(
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- zJson->zValue("blockTypeAfterGrowth")->asString()->getString(),
- zJson->zValue("readableName")->asString()->getString(),
- (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- (float)zJson->zValue("hardness")->asNumber()->getNumber(),
- groupNames);
- for (Framework::JSON::JSONValue* state :
- *zJson->zValue("states")->asArray())
- {
- result->addGrowthState((float)state->asObject()
- ->zValue("percentage")
- ->asNumber()
- ->getNumber(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- state->asObject()->zValue("model")));
- }
- return result;
- }
- Framework::JSON::JSONObject* GrowingPlantBlockTypeFactory::toJson(
- GrowingPlantBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("readableName",
- new Framework::JSON::JSONString(zObject->getReadableName()));
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- result->addValue("blockTypeAfterGrowth",
- new Framework::JSON::JSONString(
- zObject->getBlockTypeNameAfterGrowth()));
- result->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);
- }
- result->addValue("states", states);
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- GrowingPlantBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredNumber("hardness")
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredString("mapColor")
- ->finishString()
- ->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()
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text GrowingPlantBlockTypeFactory::getTypeToken() const
- {
- return "growingPlant";
- }
|