Преглед изворни кода

add inventorySlots to basic block json config

Kolja Strohm пре 5 месеци
родитељ
комит
7fafa63b1c

+ 38 - 2
FactoryCraft/BasicBlocks.cpp

@@ -2,6 +2,7 @@
 
 #include "Game.h"
 #include "ItemEntity.h"
+#include "ItemSlot.h"
 #include "ItemStack.h"
 #include "ModelInfo.h"
 #include "TreeSeblingBlock.h"
@@ -54,6 +55,10 @@ void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
     block->hardness = getHardness();
     block->speedModifier = speedModifier;
     block->interactable = interactable;
+    for (ItemSlot* slot : itemSlots)
+    {
+        block->addSlot(new ItemSlot(*slot));
+    }
     BlockType::createSuperBlock(zBlock, zItem);
 }
 
@@ -148,6 +153,16 @@ bool BasicBlockType::isInteractable() const
     return interactable;
 }
 
+const Framework::RCArray<ItemSlot>& BasicBlockType::getInventorySlots() const
+{
+    return itemSlots;
+}
+
+void BasicBlockType::addInventorySlot(ItemSlot* slot)
+{
+    itemSlots.add(slot);
+}
+
 BasicBlockTypeFactory::BasicBlockTypeFactory()
     : BlockTypeFactoryBase()
 {}
@@ -177,6 +192,13 @@ BasicBlockType* BasicBlockTypeFactory::fromJson(
         (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<ItemSlot>(
+                value->asObject()));
+    }
     return result;
 }
 
@@ -202,13 +224,22 @@ Framework::JSON::JSONObject* BasicBlockTypeFactory::toJsonObject(
         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<ItemSlot>(slot));
+    }
+    result->addValue("inventorySlots", inventorySlots);
     return result;
 }
 
 JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
     JSONObjectValidationBuilder* builder) const
 {
-    return BlockTypeFactoryBase::addToValidator(builder
+    return BlockTypeFactoryBase::addToValidator(
+        builder
             ->withRequiredAttribute("itemType",
                 Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
                     ItemTypeNameFactory::TYPE_ID),
@@ -225,7 +256,12 @@ JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
             ->finishNumber()
             ->withRequiredBool("interactable")
             ->withDefault(true)
-            ->finishBool());
+            ->finishBool())
+        ->withRequiredArray("inventorySlots")
+        ->withDefault(new Framework::JSON::JSONArray())
+        ->addAcceptedTypeInArray(
+            Game::INSTANCE->zTypeRegistry()->getValidator<ItemSlot>())
+        ->finishArray();
 }
 
 const char* BasicBlockTypeFactory::getTypeToken() const

+ 3 - 0
FactoryCraft/BasicBlocks.h

@@ -33,6 +33,7 @@ private:
     bool passable;
     float speedModifier;
     bool interactable;
+    Framework::RCArray<ItemSlot> itemSlots;
 
 protected:
     virtual void createSuperBlock(Block* zBlock, Item* zItem) const override;
@@ -55,6 +56,8 @@ public:
     float getSpeedModifier() const;
     void setInteractable(bool interactable);
     bool isInteractable() const;
+    const Framework::RCArray<ItemSlot>& getInventorySlots() const;
+    void addInventorySlot(ItemSlot* slot);
 };
 
 class BasicBlockTypeFactory : public BlockTypeFactoryBase<BasicBlockType>

+ 1 - 8
FactoryCraft/Chest.cpp

@@ -14,14 +14,7 @@ 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::onDialogClosed(Framework::Text dialogId)
 {

+ 1 - 1
FactoryCraft/Inventory.h

@@ -8,12 +8,12 @@
 #include <Writer.h>
 
 #include "Area.h"
+#include "ItemSlot.h"
 
 class ItemFilter;
 class Inventory;
 class NetworkMessage;
 class Entity;
-class ItemSlot;
 class Item;
 class ItemStack;
 

+ 179 - 5
FactoryCraft/ItemSlot.cpp

@@ -71,12 +71,13 @@ void ItemSlot::addItems(ItemStack* zStack, Direction dir)
     }
 }
 
-void ItemSlot::update() {
+void ItemSlot::update()
+{
     if (items && items->getSize() == 0)
     {
-		items->release();
-		items = 0;
-	}
+        items->release();
+        items = 0;
+    }
 }
 
 int ItemSlot::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
@@ -140,4 +141,177 @@ const Framework::Text& ItemSlot::getName() const
 int ItemSlot::getId() const
 {
     return id;
-}
+}
+
+ItemSlotFactory::ItemSlotFactory()
+    : ObjectTypeFactory()
+{}
+
+JSONObjectValidationBuilder* ItemSlotFactory::addToValidator(
+    JSONObjectValidationBuilder* builder) const
+{
+    Framework::JSON::JSONArray* defaultSides = new Framework::JSON::JSONArray();
+    defaultSides->addValue(new Framework::JSON::JSONString("TOP"));
+    defaultSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
+    defaultSides->addValue(new Framework::JSON::JSONString("NORTH"));
+    defaultSides->addValue(new Framework::JSON::JSONString("EAST"));
+    defaultSides->addValue(new Framework::JSON::JSONString("SOUTH"));
+    defaultSides->addValue(new Framework::JSON::JSONString("WEST"));
+    return builder->withRequiredString("category")
+        ->withDefault("Inventory")
+        ->finishString()
+        ->withRequiredNumber("maxSize")
+        ->withDefault(50.0)
+        ->finishNumber()
+        ->withRequiredNumber("pullPriority")
+        ->whichIsGreaterThen(0.0)
+        ->withDefault(1.0)
+        ->finishNumber()
+        ->withRequiredNumber("pushPriority")
+        ->whichIsGreaterThen(0.0)
+        ->withDefault(1.0)
+        ->finishNumber()
+        ->withRequiredArray("allowedPullSides")
+        ->withDefault(
+            dynamic_cast<Framework::JSON::JSONArray*>(defaultSides->getThis()))
+        ->addAcceptedStringInArray()
+        ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
+        ->finishString()
+        ->finishArray()
+        ->withRequiredArray("allowedPushSides")
+        ->withDefault(defaultSides)
+        ->addAcceptedStringInArray()
+        ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
+        ->finishString()
+        ->finishArray()
+        ->withRequiredBool("allowHigherStackSize")
+        ->withDefault(false)
+        ->finishBool();
+}
+
+ItemSlot* ItemSlotFactory::fromJson(Framework::JSON::JSONObject* zJson) const
+{
+    Framework::Text category
+        = zJson->zValue("category")->asString()->getString();
+    int maxSize = (int)zJson->zValue("maxSize")->asNumber()->getNumber();
+    int pullPriority
+        = (int)zJson->zValue("pullPriority")->asNumber()->getNumber();
+    int pushPriority
+        = (int)zJson->zValue("pushPriority")->asNumber()->getNumber();
+    int allowedPullSides = 0;
+    int allowedPushSides = 0;
+    for (Framework::JSON::JSONValue* side :
+        *zJson->zValue("allowedPullSides")->asArray())
+    {
+        Framework::Text sideText = side->asString()->getString();
+        if (sideText.istGleich("TOP"))
+        {
+            allowedPullSides |= TOP;
+        }
+        else if (sideText.istGleich("BOTTOM"))
+        {
+            allowedPullSides |= BOTTOM;
+        }
+        else if (sideText.istGleich("NORTH"))
+        {
+            allowedPullSides |= NORTH;
+        }
+        else if (sideText.istGleich("EAST"))
+        {
+            allowedPullSides |= EAST;
+        }
+        else if (sideText.istGleich("SOUTH"))
+        {
+            allowedPullSides |= SOUTH;
+        }
+        else if (sideText.istGleich("WEST"))
+        {
+            allowedPullSides |= WEST;
+        }
+    }
+    for (Framework::JSON::JSONValue* side :
+        *zJson->zValue("allowedPushSides")->asArray())
+    {
+        Framework::Text sideText = side->asString()->getString();
+        if (sideText.istGleich("TOP"))
+        {
+            allowedPushSides |= TOP;
+        }
+        else if (sideText.istGleich("BOTTOM"))
+        {
+            allowedPushSides |= BOTTOM;
+        }
+        else if (sideText.istGleich("NORTH"))
+        {
+            allowedPushSides |= NORTH;
+        }
+        else if (sideText.istGleich("EAST"))
+        {
+            allowedPushSides |= EAST;
+        }
+        else if (sideText.istGleich("SOUTH"))
+        {
+            allowedPushSides |= SOUTH;
+        }
+        else if (sideText.istGleich("WEST"))
+        {
+            allowedPushSides |= WEST;
+        }
+    }
+    bool allowHigherStackSize
+        = zJson->zValue("allowHigherStackSize")->asBool()->getBool();
+    return new ItemSlot(category,
+        maxSize,
+        pullPriority,
+        pushPriority,
+        allowedPullSides,
+        allowedPushSides,
+        allowHigherStackSize);
+}
+
+Framework::JSON::JSONObject* ItemSlotFactory::toJsonObject(
+    ItemSlot* zObject) const
+{
+    Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
+    result->addValue(
+        "category", new Framework::JSON::JSONString(zObject->getName()));
+    result->addValue(
+        "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxSize));
+    result->addValue("pullPriority",
+        new Framework::JSON::JSONNumber((double)zObject->getPullPriority()));
+    result->addValue("pushPriority",
+        new Framework::JSON::JSONNumber((double)zObject->getPushPriority()));
+    Framework::JSON::JSONArray* allowedPullSides
+        = new Framework::JSON::JSONArray();
+    if (zObject->allowedPullSide & TOP)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("TOP"));
+    if (zObject->allowedPullSide & BOTTOM)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
+    if (zObject->allowedPullSide & NORTH)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("NORTH"));
+    if (zObject->allowedPullSide & EAST)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("EAST"));
+    if (zObject->allowedPullSide & SOUTH)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("SOUTH"));
+    if (zObject->allowedPullSide & WEST)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("WEST"));
+    result->addValue("allowedPullSides", allowedPullSides);
+    Framework::JSON::JSONArray* allowedPushSides
+        = new Framework::JSON::JSONArray();
+    if (zObject->allowedPushSides & TOP)
+        allowedPushSides->addValue(new Framework::JSON::JSONString("TOP"));
+    if (zObject->allowedPushSides & BOTTOM)
+        allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
+    if (zObject->allowedPushSides & NORTH)
+        allowedPushSides->addValue(new Framework::JSON::JSONString("NORTH"));
+    if (zObject->allowedPushSides & EAST)
+        allowedPushSides->addValue(new Framework::JSON::JSONString("EAST"));
+    if (zObject->allowedPushSides & SOUTH)
+        allowedPushSides->addValue(new Framework::JSON::JSONString("SOUTH"));
+    if (zObject->allowedPushSides & WEST)
+        allowedPushSides->addValue(new Framework::JSON::JSONString("WEST"));
+    result->addValue("allowedPushSides", allowedPushSides);
+    result->addValue("allowHigherStackSize",
+        new Framework::JSON::JSONBool(zObject->allowHigherStackSize));
+    return result;
+}

+ 15 - 2
FactoryCraft/ItemSlot.h

@@ -3,6 +3,7 @@
 #include <Text.h>
 
 #include "Area.h"
+#include "TypeRegistry.h"
 
 class Inventory;
 class ItemStack;
@@ -36,7 +37,7 @@ public:
         int maxSize,
         int pullPriority,
         int pushPriority,
-        int allowedPullSide,
+        int allowedPullSides,
         int allowedPushSides,
         bool allowHigherStackSize);
     ~ItemSlot();
@@ -55,4 +56,16 @@ public:
     int getNumberOfItems() const;
     const Framework::Text& getName() const;
     int getId() const;
-};
+
+    friend class ItemSlotFactory;
+};
+
+class ItemSlotFactory : public ObjectTypeFactory<ItemSlot>
+{
+public:
+    ItemSlotFactory();
+    JSONObjectValidationBuilder* addToValidator(
+        JSONObjectValidationBuilder* builder) const override;
+    ItemSlot* fromJson(Framework::JSON::JSONObject* zJson) const override;
+    Framework::JSON::JSONObject* toJsonObject(ItemSlot* zObject) const override;
+};

+ 4 - 0
FactoryCraft/TypeRegistry.cpp

@@ -23,6 +23,7 @@
 #include "GeneratorRule.h"
 #include "Grass.h"
 #include "GrowingPlant.h"
+#include "ItemSlot.h"
 #include "JsonExpression.h"
 #include "LightSources.h"
 #include "ModelInfo.h"
@@ -143,6 +144,9 @@ TypeRegistry::TypeRegistry()
     registerSubType(new DefaultInventoryItemDropFactory());
     registerSubType(new SpecificItemDropFactory());
     registerSubType(new BlockReplacementDropFactory());
+
+    // inventories
+    registerType(new ItemSlotFactory());
 }
 
 void TypeRegistry::writeSyntaxInfo(Framework::Text folderPath) const

+ 134 - 0
Windows Version/data/blocks/blockTypes.json

@@ -1087,6 +1087,128 @@
     "mapColor": "0xFFE2C292",
     "groupNames": [
       "Wood"
+    ],
+    "inventorySlots": [
+      {
+        "pullPriority": 1,
+        "pushPriority": 1
+      },
+      {
+        "pullPriority": 2,
+        "pushPriority": 2
+      },
+      {
+        "pullPriority": 3,
+        "pushPriority": 3
+      },
+      {
+        "pullPriority": 4,
+        "pushPriority": 4
+      },
+      {
+        "pullPriority": 5,
+        "pushPriority": 5
+      },
+      {
+        "pullPriority": 6,
+        "pushPriority": 6
+      },
+      {
+        "pullPriority": 7,
+        "pushPriority": 7
+      },
+      {
+        "pullPriority": 8,
+        "pushPriority": 8
+      },
+      {
+        "pullPriority": 9,
+        "pushPriority": 9
+      },
+      {
+        "pullPriority": 10,
+        "pushPriority": 10
+      },
+      {
+        "pullPriority": 11,
+        "pushPriority": 11
+      },
+      {
+        "pullPriority": 12,
+        "pushPriority": 12
+      },
+      {
+        "pullPriority": 13,
+        "pushPriority": 13
+      },
+      {
+        "pullPriority": 14,
+        "pushPriority": 14
+      },
+      {
+        "pullPriority": 15,
+        "pushPriority": 15
+      },
+      {
+        "pullPriority": 16,
+        "pushPriority": 16
+      },
+      {
+        "pullPriority": 17,
+        "pushPriority": 17
+      },
+      {
+        "pullPriority": 18,
+        "pushPriority": 18
+      },
+      {
+        "pullPriority": 19,
+        "pushPriority": 19
+      },
+      {
+        "pullPriority": 20,
+        "pushPriority": 20
+      },
+      {
+        "pullPriority": 21,
+        "pushPriority": 21
+      },
+      {
+        "pullPriority": 22,
+        "pushPriority": 22
+      },
+      {
+        "pullPriority": 23,
+        "pushPriority": 23
+      },
+      {
+        "pullPriority": 24,
+        "pushPriority": 24
+      },
+      {
+        "pullPriority": 25,
+        "pushPriority": 25
+      },
+      {
+        "pullPriority": 26,
+        "pushPriority": 26
+      },
+      {
+        "pullPriority": 27,
+        "pushPriority": 27
+      },
+      {
+        "pullPriority": 28,
+        "pushPriority": 28
+      },
+      {
+        "pullPriority": 29,
+        "pushPriority": 29
+      },
+      {
+        "pullPriority": 30,
+        "pushPriority": 30
+      }
     ]
   },
   {
@@ -1102,5 +1224,17 @@
     "mapColor": "0xFFE25822",
     "lightColor": "0x00E25822",
     "lightSource": true
+  },
+  {
+    "type": "basicBlock",
+    "name": "Vines",
+    "itemType": "Vines",
+    "model": {
+      "modelPath": "blocks.m3/vines",
+      "texturePaths": [
+        "blocks.ltdb/leaves.png"
+      ]
+    },
+    "mapColor": "0xFF33A033"
   }
 ]