123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include "Chest.h"
- #include <TextFeld.h>
- #include "Game.h"
- Chest::Chest(int typeId, Framework::Vec3<int> pos, int dimensionId)
- : BasicBlock(typeId, pos, dimensionId, 1),
- open(0),
- userEntityId(0)
- {
- for (int i = 0; i < 30; i++)
- {
- ItemSlot* slot = new ItemSlot(
- "Inventory", 50, i, i, ANY_DIRECTION, ANY_DIRECTION, 0);
- addSlot(slot);
- }
- }
- void Chest::onDestroy()
- {
- for (ItemSlot* slot : *this)
- {
- if (!slot->isEmpty())
- {
- Game::INSTANCE->spawnItem(location + Vec3<float>(0.5f, 0.5f, 0.5f),
- getDimensionId(),
- slot->takeItemsOut(slot->getNumberOfItems(), NO_DIRECTION));
- }
- }
- BasicBlock::onDestroy();
- }
- void Chest::onDialogClosed(Framework::Text dialogId)
- {
- if (dialogId.istGleich(getDialogId()))
- {
- open = 0;
- userEntityId = 0;
- NetworkMessage* msg = new NetworkMessage();
- msg->animateBlockBone(getDimensionId(),
- Game::getChunkCenter(getPos().x, getPos().y),
- Chunk::index(Dimension::chunkCoordinates(getPos())),
- 1,
- 1.0,
- Vec3<float>(0.5f, 0.f, 0.45f),
- Vec3<float>(0.0f, 0.f, 0.f)); // close the chest over one second
- broadcastMessage(msg);
- }
- }
- Framework::Text Chest::getDialogId() const
- {
- Text dialogId = "chest_inventory_";
- dialogId.append() << getDimensionId() << "," << getPos().x << ","
- << getPos().y << "," << getPos().z;
- return dialogId;
- }
- bool Chest::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- if (open)
- {
- if (!Game::INSTANCE->zEntity(userEntityId))
- {
- onDialogClosed(getDialogId());
- }
- }
- return open;
- }
- bool Chest::interact(Item* zItem, Entity* zActor)
- {
- lock();
- if (open)
- {
- if (!Game::INSTANCE->zEntity(userEntityId)) open = 0;
- }
- if (!open)
- {
- userEntityId = zActor->getId();
- open = 1;
- Text uiml = "";
- uiml.append()
- << "<dialog id=\"" << getDialogId()
- << "\" title=\"Chest\" "
- "notifyOnClose=\""
- << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
- << getPos().z
- << "\" "
- "width=\"610\" "
- "height=\"480\">"
- << "<inventory id=\"chest_inventory\" margin-bottom=\"18\" "
- "align-bottom=\"player_label\" align-left=\"start\" "
- "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
- "numSlots=\"30\" slotNameFilter=\"\" target=\""
- << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
- << getPos().z << "\"/>"
- << "<text id=\"player_label\" width=\"100%\" height=\"auto\" "
- "style=\""
- << std::uppercase << std::hex
- << (TextFeld::Style::Text | TextFeld::Style::Center) << std::dec
- << std::nouppercase
- << "\" margin-bottom=\"9\" "
- "align-bottom=\"player_inventory\">Player "
- "Inventory</text>"
- << "<inventory id=\"player_inventory\" margin-bottom=\"18\" "
- "align-bottom=\"item_bar\" align-left=\"start\" "
- "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
- "numSlots=\"30\" slotNameFilter=\"Inventory\" target=\""
- << zActor->getId() << "\"/>"
- << "<inventory id=\"item_bar\" margin-bottom=\"9\" "
- "align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" "
- "width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" "
- "slotNameFilter=\"ItemBar\" target=\""
- << zActor->getId() << "\"/>"
- << "</dialog>";
- Game::INSTANCE->zUIController()->addDialog(new UIDialog(
- getDialogId(), zActor->getId(), new Framework::XML::Element(uiml)));
- NetworkMessage* msg = new NetworkMessage();
- msg->animateBlockBone(getDimensionId(),
- Game::getChunkCenter(getPos().x, getPos().y),
- Chunk::index(Dimension::chunkCoordinates(getPos())),
- 1,
- 1.0,
- Vec3<float>(0.5f, 0.f, 0.45f),
- Vec3<float>(
- 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
- broadcastMessage(msg);
- }
- unlock();
- return false; // item was not changed
- }
- void Chest::sendModelInfo(NetworkMessage* zMessage)
- {
- if (open)
- {
- zMessage->animateBlockBone(getDimensionId(),
- Game::getChunkCenter(getPos().x, getPos().y),
- Chunk::index(Dimension::chunkCoordinates(getPos())),
- 1,
- 0.0,
- Vec3<float>(0.5f, 0.f, 0.45f),
- Vec3<float>(
- 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
- }
- }
- ChestBlockType::ChestBlockType(Framework::Text itemTypeName,
- ModelInfo* model,
- Framework::Text name,
- int mapColor,
- bool modelSubscription,
- float hardness,
- Framework::RCArray<Framework::Text> groupNames)
- : BasicBlockType(itemTypeName,
- model,
- name,
- mapColor,
- modelSubscription,
- hardness,
- groupNames)
- {}
- Block* ChestBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- return new Chest(getItemTypeId(), position, dimensionId);
- }
- ItemType* ChestBlockType::createItemType() const
- {
- return new BasicBlockItemType(getItemTypeName(),
- new ModelInfo(zModel()->getModelPath(),
- zModel()->getTexturePaths(),
- zModel()->isTransparent(),
- zModel()->getSize() / 2.f),
- isTransparent(),
- isPassable(),
- getHardness(),
- getSpeedModifier(),
- getItemTypeName(),
- 0, 50);
- }
- ChestBlockTypeFactory::ChestBlockTypeFactory()
- : SubTypeFactory()
- {}
- ChestBlockType* ChestBlockTypeFactory::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()));
- }
- return new ChestBlockType(
- zJson->zValue("itemType")->asString()->getString(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- zJson->zValue("modelSubscription")->asBool()->getBool(),
- (float)zJson->zValue("hardness")->asNumber()->getNumber(),
- groupNames);
- }
- Framework::JSON::JSONObject* ChestBlockTypeFactory::toJson(
- ChestBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(zObject->getItemTypeName()));
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- result->addValue("modelSubscription",
- new Framework::JSON::JSONBool(zObject->doesNeedModelSubscription()));
- result->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- 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* ChestBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("itemType")
- ->finishString()
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredString("mapColor")
- ->finishString()
- ->withRequiredBool("modelSubscription")
- ->withDefault(false)
- ->finishBool()
- ->withRequiredNumber("hardness")
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text ChestBlockTypeFactory::getTypeToken() const
- {
- return "chest";
- }
|