#pragma once #include "Block.h" #include "BlockComponent.h" #include "BlockType.h" #include "Item.h" #include "TypeRegistry.h" class BlockType; class ItemType; class BasicBlockType; class BasicBlock : public Block { private: Framework::RCArray components; public: BasicBlock(int typeId, Framework::Vec3 pos, int dimensionId); BasicBlock(int typeId, Framework::Vec3 pos, int dimensionId, bool hasInventory); void addComponent(BlockComponent* component); virtual bool onTick( TickQueue* zQueue, int numTicks, bool& blocked) override; virtual void onPostTick() override; friend BasicBlockType; }; class BasicBlockType : public BlockType { private: Framework::Text itemTypeName; int itemTypeId; bool transparent; bool passable; float speedModifier; bool interactable; Framework::RCArray itemSlots; Framework::RCArray components; public: BasicBlockType(); virtual bool initialize(Game* zGame) override; protected: virtual void loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const override; virtual void saveSuperBlock( Block* zBlock, Framework::StreamWriter* zWriter) const override; virtual void createSuperBlock(Block* zBlock, Item* zItem) const override; public: virtual Block* createBlock( Framework::Vec3 position, int dimensionId) const override; virtual Item* createItem() const override; Framework::Text getItemTypeName() const; virtual ItemType* createItemType() const override; void setItemTypeName(Framework::Text itemTypeName); int getItemTypeId() const; void setTransparent(bool transparent); bool isTransparent() const; void setPassable(bool passable); bool isPassable() const; void setSpeedModifier(float speedModifier); float getSpeedModifier() const; void setInteractable(bool interactable); bool isInteractable() const; const Framework::RCArray& getInventorySlots() const; void addInventorySlot(ItemSlot* slot); const Framework::RCArray& getComponents() const; void addComponent(Framework::JSON::JSONValue* component); }; template::value>> class BasicBlockTypeFactory : public BlockTypeFactoryBase { public: BasicBlockTypeFactory() : BlockTypeFactoryBase() {} virtual T* createValue(Framework::JSON::JSONObject* zJson) const override { return (T*)new BasicBlockType(); } virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override { T* result = BlockTypeFactoryBase::fromJson(zJson); if (zJson->zValue("itemType")->getType() == Framework::AbstractType::STRING) { result->setItemTypeName( zJson->zValue("itemType")->asString()->getString()); } else { result->setItemTypeName(""); } result->setTransparent( zJson->zValue("transparent")->asBool()->getBool()); result->setPassable(zJson->zValue("passable")->asBool()->getBool()); result->setSpeedModifier( (float)zJson->zValue("speedModifier")->asNumber()->getNumber()); result->setInteractable( (float)zJson->zValue("interactable")->asBool()->getBool()); for (Framework::JSON::JSONValue* value : *zJson->zValue("inventorySlots")->asArray()) { result->addInventorySlot( Game::INSTANCE->zTypeRegistry()->fromJson( value->asObject())); } for (Framework::JSON::JSONValue* component : *zJson->zValue("components")->asArray()) { result->addComponent(dynamic_cast( component->getThis())); } return result; } virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override { Framework::JSON::JSONObject* result = BlockTypeFactoryBase::toJsonObject(zObject); if (zObject->getItemTypeName().istGleich("")) { result->addValue("itemType", new Framework::JSON::JSONValue()); } else { result->addValue("itemType", new Framework::JSON::JSONString(zObject->getItemTypeName())); } result->addValue("transparent", new Framework::JSON::JSONBool(zObject->isTransparent())); result->addValue( "passable", new Framework::JSON::JSONBool(zObject->isPassable())); result->addValue("speedModifier", new Framework::JSON::JSONNumber(zObject->getSpeedModifier())); result->addValue("interactable", new Framework::JSON::JSONBool(zObject->isInteractable())); Framework::JSON::JSONArray* inventorySlots = new Framework::JSON::JSONArray(); for (ItemSlot* slot : zObject->getInventorySlots()) { inventorySlots->addValue( Game::INSTANCE->zTypeRegistry()->toJson(slot)); } result->addValue("inventorySlots", inventorySlots); Framework::JSON::JSONArray* components = new Framework::JSON::JSONArray(); for (Framework::JSON::JSONValue* component : zObject->getComponents()) { components->addValue(dynamic_cast( component->getThis())); } result->addValue("components", components); return result; } virtual JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override { return BlockTypeFactoryBase::addToValidator( builder ->withRequiredAttribute("itemType", Game::INSTANCE->zTypeRegistry() ->getValidator( ItemTypeNameFactory::TYPE_ID), true, false) ->withRequiredBool("transparent") ->withDefault(false) ->finishBool() ->withRequiredBool("passable") ->withDefault(false) ->finishBool() ->withRequiredNumber("speedModifier") ->withDefault(1.0) ->finishNumber() ->withRequiredBool("interactable") ->withDefault(true) ->finishBool()) ->withRequiredArray("inventorySlots") ->withDefault(new Framework::JSON::JSONArray()) ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->withRequiredArray("components") ->withDefault(new Framework::JSON::JSONArray()) ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); } virtual const char* getTypeToken() const override { return "basicBlock"; } };