| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include "BasicBlocks.h"
- #include "Game.h"
- #include "ItemEntity.h"
- #include "ItemSlot.h"
- #include "ItemStack.h"
- #include "ModelInfo.h"
- #include "TreeSeblingBlock.h"
- BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
- : BasicBlock(typeId, pos, dimensionId, false)
- {}
- BasicBlock::BasicBlock(
- int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
- : Block(typeId, pos, dimensionId, hasInventory)
- {}
- void BasicBlock::addComponent(BlockComponent* component)
- {
- components.add(component);
- }
- Framework::XML::Element* BasicBlock::getTargetUIML() const
- {
- Framework::XML::Element* element
- = Block::getTargetUIML(); // Get the base element from Block class
- int index = 0;
- Framework::XML::Element* last
- = element->getChildCount() > 0
- ? element->zChild(element->getChildCount() - 1)
- : 0;
- for (BlockComponent* component : components)
- {
- Framework::XML::Element* compElement = component->getTooltipUIML();
- if (compElement)
- {
- if (last)
- {
- if (!last->hasAttribute("id"))
- {
- last->setAttribute(
- "id", Framework::Text("_component_") + (index - 1));
- }
- compElement->setAttribute(
- "alignTop", last->getAttributeValue("id"));
- compElement->setAttribute("marginTop", "5");
- }
- element->addChild(compElement);
- last = compElement;
- }
- index++;
- }
- return element;
- }
- bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- bool ative = false;
- for (BlockComponent* component : components)
- {
- ative |= component->tick(numTicks);
- }
- return ative;
- }
- void BasicBlock::onPostTick() {}
- void BasicBlock::getLightEmisionColor(unsigned char* result) const
- {
- Block::getLightEmisionColor(result);
- for (BlockComponent* component : components)
- {
- int color = component->getLightColor();
- result[0] = (unsigned char)MIN(result[0] + ((color >> 16) & 0xFF), 255);
- result[1] = (unsigned char)MIN(result[1] + ((color >> 8) & 0xFF), 255);
- result[2] = (unsigned char)MIN(result[2] + (color & 0xFF), 255);
- }
- }
- TickSourceType BasicBlock::isTickSource() const
- {
- return components.getEintragAnzahl() > 0 ? TickSourceType::EACH_TICK
- : TickSourceType::NONE;
- }
- void BasicBlock::onApiCall(char messageType,
- Framework::StreamReader* zRequest,
- NetworkMessage* zResponse,
- Entity* zSource)
- {
- switch (messageType)
- {
- case 2: // component request
- {
- int index;
- zRequest->lese((char*)&index, sizeof(int));
- if (index >= 0 && index < components.getEintragAnzahl())
- {
- components.z(index)->api(zRequest, zResponse, zSource);
- }
- break;
- }
- }
- }
- BasicBlockType::BasicBlockType()
- : BlockType(),
- itemTypeName(),
- transparent(0),
- passable(0),
- speedModifier(1.f),
- interactable(1)
- {}
- bool BasicBlockType::initialize(Game* zGame)
- {
- if (itemTypeName.getLength())
- {
- itemTypeId = zGame->getItemTypeId(itemTypeName);
- }
- else
- {
- itemTypeId = 0;
- }
- return itemTypeId >= 0 && BlockType::initialize(zGame);
- }
- void BasicBlockType::loadSuperBlock(
- Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
- {
- BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
- for (BlockComponent* component : block->components)
- {
- component->loadComponent(zReader);
- }
- BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
- }
- void BasicBlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
- for (BlockComponent* component : block->components)
- {
- component->saveComponent(zWriter);
- }
- BlockType::saveSuperBlock(zBlock, zWriter);
- }
- void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
- {
- BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
- block->transparent = transparent;
- block->passable = passable;
- block->hp = (float)getInitialMaxHP();
- block->maxHP = (float)getInitialMaxHP();
- block->hardness = getHardness();
- block->speedModifier = speedModifier;
- block->interactable = interactable;
- for (ItemSlot* slot : itemSlots)
- {
- block->addSlot(new ItemSlot(*slot));
- }
- for (Framework::JSON::JSONValue* component : components)
- {
- BlockComponent* blockComponent
- = Game::INSTANCE->zTypeRegistry()->fromJson<BlockComponent>(
- component);
- blockComponent->initialize(block);
- block->addComponent(blockComponent);
- }
- BlockType::createSuperBlock(zBlock, zItem);
- }
- Block* BasicBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- return new BasicBlock(
- getId(), position, dimensionId, itemSlots.getEintragAnzahl() > 0);
- }
- Item* BasicBlockType::createItem() const
- {
- if (getItemTypeName().istGleich(""))
- {
- return 0;
- }
- return Game::INSTANCE->zItemType(itemTypeId)->createItem();
- }
- Framework::Text BasicBlockType::getItemTypeName() const
- {
- return itemTypeName;
- }
- ItemType* BasicBlockType::createItemType() const
- {
- if (getItemTypeName().istGleich(""))
- {
- return 0;
- }
- return new BasicBlockItemType(getItemTypeName(),
- new ModelInfo(zModel()->getModelPath(),
- zModel()->getTexturePaths(),
- zModel()->isTransparent(),
- zModel()->getSize() / 2.f),
- transparent,
- passable,
- getHardness(),
- speedModifier,
- getName(),
- 0,
- 50,
- getGroupNames());
- }
- void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
- {
- this->itemTypeName = itemTypeName;
- }
- int BasicBlockType::getItemTypeId() const
- {
- return itemTypeId;
- }
- void BasicBlockType::setTransparent(bool transparent)
- {
- this->transparent = transparent;
- }
- bool BasicBlockType::isTransparent() const
- {
- return transparent;
- }
- void BasicBlockType::setPassable(bool passable)
- {
- this->passable = passable;
- }
- bool BasicBlockType::isPassable() const
- {
- return passable;
- }
- void BasicBlockType::setSpeedModifier(float speedModifier)
- {
- this->speedModifier = speedModifier;
- }
- float BasicBlockType::getSpeedModifier() const
- {
- return speedModifier;
- }
- void BasicBlockType::setInteractable(bool interactable)
- {
- this->interactable = interactable;
- }
- bool BasicBlockType::isInteractable() const
- {
- return interactable;
- }
- const Framework::RCArray<ItemSlot>& BasicBlockType::getInventorySlots() const
- {
- return itemSlots;
- }
- void BasicBlockType::addInventorySlot(ItemSlot* slot)
- {
- itemSlots.add(slot);
- }
- const Framework::RCArray<Framework::JSON::JSONValue>&
- BasicBlockType::getComponents() const
- {
- return components;
- }
- void BasicBlockType::addComponent(Framework::JSON::JSONValue* component)
- {
- components.add(component);
- }
|