diff --git a/prefab/packsacks/packsack.tscn b/prefab/packsacks/packsack.tscn index 2b6750f..f588a82 100644 --- a/prefab/packsacks/packsack.tscn +++ b/prefab/packsacks/packsack.tscn @@ -13,7 +13,10 @@ size = Vector2(21, 29) collision_layer = 8 collision_mask = 38 script = ExtResource("1_slakl") +NumberSlots = 30 Id = "packsack" +UniqueName = null +UniqueDescription = null _minContactInjury = null _maxContactInjury = null diff --git a/prefab/ui/ItemSlot.tscn b/prefab/ui/ItemSlot.tscn index bb518a1..ab7fbe3 100644 --- a/prefab/ui/ItemSlot.tscn +++ b/prefab/ui/ItemSlot.tscn @@ -7,6 +7,7 @@ offset_right = 38.0 offset_bottom = 38.0 size_flags_vertical = 3 +mouse_filter = 0 theme_override_constants/margin_left = 3 theme_override_constants/margin_top = 3 theme_override_constants/margin_right = 3 @@ -15,6 +16,7 @@ script = ExtResource("1_fbwot") [node name="BackgroundTexture" type="TextureRect" parent="."] layout_mode = 2 +mouse_filter = 2 texture = ExtResource("1_y2wyt") [node name="IconTextureRect" type="TextureRect" parent="BackgroundTexture"] @@ -28,10 +30,12 @@ offset_right = -3.0 offset_bottom = -3.0 grow_horizontal = 2 grow_vertical = 2 +mouse_filter = 2 expand_mode = 1 [node name="Control" type="Control" parent="."] layout_mode = 2 +mouse_filter = 2 [node name="QuantityLabel" type="Label" parent="Control"] layout_mode = 1 diff --git a/prefab/ui/packsackUI.tscn b/prefab/ui/packsackUI.tscn index 07b1811..b2392ac 100644 --- a/prefab/ui/packsackUI.tscn +++ b/prefab/ui/packsackUI.tscn @@ -9,46 +9,37 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +mouse_filter = 2 script = ExtResource("1_wm4bu") -[node name="ColorRect" type="ColorRect" parent="."] -layout_mode = 1 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 -color = Color(0, 0, 0, 0.294118) - [node name="TitleLabel" type="Label" parent="."] layout_mode = 1 anchors_preset = 10 anchor_right = 1.0 -offset_bottom = 30.0 +offset_bottom = 25.0 grow_horizontal = 2 text = "ui_name" horizontal_alignment = 1 -[node name="GridContainer" type="GridContainer" parent="."] -layout_mode = 1 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -offset_left = 36.0 -offset_top = 57.0 -offset_right = -34.0 -offset_bottom = -23.0 -grow_horizontal = 2 -grow_vertical = 2 - [node name="ExitButton" type="Button" parent="."] layout_mode = 1 anchors_preset = 1 anchor_left = 1.0 anchor_right = 1.0 -offset_left = -75.0 -offset_top = 8.0 -offset_right = -8.0 -offset_bottom = 41.0 +offset_left = -91.0 +offset_top = 11.0 +offset_right = -24.0 +offset_bottom = 44.0 grow_horizontal = 0 text = "ui_close" + +[node name="HFlowContainer" type="HFlowContainer" parent="."] +layout_mode = 1 +anchors_preset = 10 +anchor_right = 1.0 +offset_left = 26.0 +offset_top = 70.0 +offset_right = -29.0 +offset_bottom = 389.0 +grow_horizontal = 2 +mouse_filter = 2 diff --git a/scenes/game.tscn b/scenes/game.tscn index d68aafb..77b2408 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -91,6 +91,7 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +mouse_filter = 2 [node name="GameOverMenu" parent="CanvasLayer" instance=ExtResource("6_yjmrv")] visible = false diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index 355f0d5..95bd643 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -219,7 +219,7 @@ public interface IItemContainer : IEnumerable /// 添加物品槽 /// /// - void AddItemSlot(Node rootNode); + ItemSlotNode? AddItemSlot(Node rootNode); /// /// SelectTheNextItemSlot diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index 9907637..87ae9a8 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -1,7 +1,7 @@ +using ColdMint.scripts.debug; using ColdMint.scripts.item; using ColdMint.scripts.item.itemStacks; using ColdMint.scripts.utils; - using Godot; namespace ColdMint.scripts.inventory; @@ -21,6 +21,34 @@ public partial class ItemSlotNode : MarginContainer private Texture2D? _backgroundTexture; private Texture2D? _backgroundTextureWhenSelect; + public override Variant _GetDragData(Vector2 atPosition) + { + if (_iconTextureRect == null) + { + return base._GetDragData(atPosition); + } + + var textureRect = new TextureRect(); + textureRect.ExpandMode = _iconTextureRect.ExpandMode; + textureRect.Size = _iconTextureRect.Size; + textureRect.Texture = _iconTextureRect.Texture; + SetDragPreview(textureRect); + return Variant.From(this); + } + + public override bool _CanDropData(Vector2 atPosition, Variant data) + { + if (_iconTextureRect == null) + { + return false; + } + + //TODO:在这里判断是否可以放置物品。物品槽必须是空的。 + // var itemSlotNode = data.As(); + // itemSlotNode._itemStack + return true; + } + public bool IsSelect { get => _isSelect; @@ -272,9 +300,9 @@ public partial class ItemSlotNode : MarginContainer if (debugText != null) { _control.TooltipText = string.Format(debugText, _itemStack.GetItem()?.Id, - TranslationServerUtils.Translate(_itemStack.Name), - _itemStack.Quantity, _itemStack.MaxQuantity, _itemStack.GetType().Name, - TranslationServerUtils.Translate(_itemStack.Description)); + TranslationServerUtils.Translate(_itemStack.Name), + _itemStack.Quantity, _itemStack.MaxQuantity, _itemStack.GetType().Name, + TranslationServerUtils.Translate(_itemStack.Description)); } } else diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 87b1d64..fefc211 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -2,14 +2,11 @@ using System.Collections; using System.Collections.Generic; using System.Linq; - using ColdMint.scripts.character; using ColdMint.scripts.item; using ColdMint.scripts.item.itemStacks; using ColdMint.scripts.utils; - using Godot; - using JetBrains.Annotations; namespace ColdMint.scripts.inventory; @@ -62,7 +59,7 @@ public class UniversalItemContainer : IItemContainer var slots = MatchAll(slot => slot.CanAddItem(testItem)); return Math.Min(itemStack.Quantity, - slots.Select(slot => slot.CanAddItemStack(itemStack)).Sum()); + slots.Select(slot => slot.CanAddItemStack(itemStack)).Sum()); } public bool AddItemStack(IItemStack itemStack) @@ -72,7 +69,7 @@ public class UniversalItemContainer : IItemContainer { if (itemSlotNode.AddItemStack(itemStack)) return true; - + itemSlotNode = Match(itemStack); } @@ -222,21 +219,22 @@ public class UniversalItemContainer : IItemContainer /// Add items tank /// 添加物品槽 /// - public void AddItemSlot(Node rootNode) + public ItemSlotNode? AddItemSlot(Node rootNode) { if (_itemSlotNodes == null || _itemSlotPackedScene == null) { - return; + return null; } var itemSlotNode = NodeUtils.InstantiatePackedScene(_itemSlotPackedScene, rootNode); if (itemSlotNode == null) { - return; + return null; } itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex; _itemSlotNodes.Add(itemSlotNode); + return itemSlotNode; } public void SelectTheNextItemSlot() diff --git a/scripts/item/Packsack.cs b/scripts/item/Packsack.cs index 6c334e9..626592c 100644 --- a/scripts/item/Packsack.cs +++ b/scripts/item/Packsack.cs @@ -15,6 +15,8 @@ public partial class Packsack : PickAbleTemplate { private PackedScene? _packedScene; private PacksackUi? _packsackUi; + [Export] + public int NumberSlots { get; set; } public override bool CanPutInPack => false; @@ -45,7 +47,7 @@ public partial class Packsack : PickAbleTemplate _packsackUi.ItemContainer = ItemContainer; } } - + GameSceneNodeHolder.BackpackUiContainer?.Show(); _packsackUi?.Show(); } @@ -55,6 +57,12 @@ public partial class Packsack : PickAbleTemplate { base._Ready(); ItemContainer = new UniversalItemContainer(); + //When the backpack is created, the item slot is generated. + //当背包被创建时,物品槽就被生成出来了。 + for (var i = 0; i < NumberSlots; i++) + { + ItemContainer.AddItemSlot(this)?.Hide(); + } _packedScene = GD.Load("res://prefab/ui/packsackUI.tscn"); } } \ No newline at end of file diff --git a/scripts/loader/uiLoader/PacksackUi.cs b/scripts/loader/uiLoader/PacksackUi.cs index 37fd574..e96feb9 100644 --- a/scripts/loader/uiLoader/PacksackUi.cs +++ b/scripts/loader/uiLoader/PacksackUi.cs @@ -14,7 +14,7 @@ public partial class PacksackUi : UiLoaderTemplate private PackedScene? _packedScene; - private GridContainer? _gridContainer; + private HFlowContainer? _hFlowContainer; private Label? _titleLabel; @@ -57,15 +57,16 @@ public partial class PacksackUi : UiLoaderTemplate /// private void PlaceItemSlot(IItemContainer? itemContainer) { - if (_gridContainer == null || itemContainer == null) + if (_hFlowContainer == null || itemContainer == null) { return; } - NodeUtils.DeleteAllChild(_gridContainer); + NodeUtils.DeleteAllChild(_hFlowContainer); foreach (var itemSlotNode in itemContainer) { - itemSlotNode.Reparent(_gridContainer); + itemSlotNode.Reparent(_hFlowContainer); + itemSlotNode.Show(); } } @@ -91,10 +92,9 @@ public partial class PacksackUi : UiLoaderTemplate public override void InitializeUi() { - _gridContainer = GetNode("GridContainer"); + _hFlowContainer = GetNode("HFlowContainer"); _titleLabel = GetNode