From 5d6d42aa5441f99117beddec6f25bd163018f161 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Thu, 20 Jun 2024 00:02:32 +0800 Subject: [PATCH] =?UTF-8?q?Adds=20the=20selected=20slot=20change=20event?= =?UTF-8?q?=20to=20the=20item=20container.=20=E5=8A=A0=E5=85=A5=E7=89=A9?= =?UTF-8?q?=E5=93=81=E5=AE=B9=E5=99=A8=E9=80=89=E4=B8=AD=E7=9A=84=E6=A7=BD?= =?UTF-8?q?=E4=BD=8D=E6=94=B9=E5=8F=98=E4=BA=8B=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 3 +- scripts/character/CharacterTemplate.cs | 25 +++- scripts/character/Player.cs | 55 +++++++-- scripts/inventory/HotBar.cs | 27 ++-- scripts/inventory/IItemContainer.cs | 11 +- scripts/inventory/UniversalItemContainer.cs | 115 ++++-------------- scripts/map/PlayerSpawn.cs | 14 ++- .../map/events/SelectedItemSlotChangeEvent.cs | 22 ++++ 8 files changed, 143 insertions(+), 129 deletions(-) create mode 100644 scripts/map/events/SelectedItemSlotChangeEvent.cs diff --git a/locals/Log.csv b/locals/Log.csv index 60dffeb..f0ca0bd 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -36,4 +36,5 @@ log_found_files,找到{0}个文件,Found {0} files,{0}ファイルが見つか log_found_item_types,从文件中找到{0}个物品类型,Found {0} item types in files,ファイルから{0}個のアイテム・タイプが見つかった log_register_item,注册物品类型{0}结果为{1},Registered item type {0}; results in {1},登録されたアイテム・タイプ {0} の結果は {1} です。 log_error_when_open_item_regs_dir,尝试打开物品信息目录时发生错误,Error when opening itemRegs dir,アイテム情報カタログを開こうとしてエラーが発生しました。 -log_wrong_custom_arg,不匹配的参数:类型为{0}而值为{1},Mismatched parameter: type {0} and value {1},パラメータの不一致:型{0}と値{1}。 \ No newline at end of file +log_wrong_custom_arg,不匹配的参数:类型为{0}而值为{1},Mismatched parameter: type {0} and value {1},パラメータの不一致:型{0}と値{1}。 +log_item_container_is_null,物品容器为空,Item container is null,アイテム・コンテナが空です \ No newline at end of file diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index 90c5c6e..30c19a2 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -7,7 +7,6 @@ using ColdMint.scripts.debug; using ColdMint.scripts.health; using ColdMint.scripts.inventory; using ColdMint.scripts.item; -using ColdMint.scripts.item.itemStacks; using ColdMint.scripts.utils; using ColdMint.scripts.item.weapon; using ColdMint.scripts.loot; @@ -40,9 +39,20 @@ public partial class CharacterTemplate : CharacterBody2D protected string? CharacterName; + protected IItemContainer? ProtectedItemContainer; + //Item containers are used to store items. //物品容器用于存储物品。 - public IItemContainer? ItemContainer { get; set; } + public IItemContainer? ItemContainer + { + get => ProtectedItemContainer; + set + { + ProtectedItemContainer = value; + WhenBindItemContainer(ProtectedItemContainer); + } + } + //Items currently held //当前持有的物品 @@ -58,6 +68,16 @@ public partial class CharacterTemplate : CharacterBody2D } } + /// + /// When binding an item container to a character + /// 当为角色绑定物品容器时 + /// + /// + protected virtual void WhenBindItemContainer(IItemContainer? itemContainer) + { + } + + /// /// When the items the character holds are updated /// 当角色持有的物品更新时 @@ -618,7 +638,6 @@ public partial class CharacterTemplate : CharacterBody2D { var itemSlotNode = ItemContainer?.GetItemSlotNode(index); if (itemSlotNode is null) return; - if (number < 0) { while (!itemSlotNode.IsEmpty()) diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index f618a05..edcf35a 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -1,9 +1,9 @@ using System; using System.Text; using System.Threading.Tasks; - using ColdMint.scripts.damage; using ColdMint.scripts.deathInfo; +using ColdMint.scripts.inventory; using ColdMint.scripts.item; using ColdMint.scripts.map.events; using ColdMint.scripts.utils; @@ -62,6 +62,43 @@ public partial class Player : CharacterTemplate } } + protected override void WhenBindItemContainer(IItemContainer? itemContainer) + { + if (itemContainer == null) + { + return; + } + + //Subscribe to events when the item container is bound to the player. + //在物品容器与玩家绑定时订阅事件。 + itemContainer.SelectedItemSlotChangeEvent += SelectedItemSlotChangeEvent; + } + + public override void _ExitTree() + { + base._ExitTree(); + if (ProtectedItemContainer != null) + { + //Unsubscribe to events when this object is destroyed. + //此节点被销毁时,取消订阅事件。 + ProtectedItemContainer.SelectedItemSlotChangeEvent -= SelectedItemSlotChangeEvent; + } + } + + private void SelectedItemSlotChangeEvent(SelectedItemSlotChangeEvent selectedItemSlotChangeEvent) + { + var item = selectedItemSlotChangeEvent.NewItemSlotNode?.GetItemStack()?.GetItem(); + GameSceneNodeHolder.HideBackpackUiContainerIfVisible(); + if (item is Node2D node2D) + { + CurrentItem = node2D; + } + else + { + CurrentItem = null; + } + } + /// /// Update operation prompt /// 更新操作提示 @@ -103,7 +140,7 @@ public partial class Player : CharacterTemplate operationTipBuilder.Append(Config.OperationTipActionColor); operationTipBuilder.Append(']'); operationTipBuilder.Append( - TranslationServerUtils.Translate(InputMap.ActionGetEvents("ui_down")[0].AsText())); + TranslationServerUtils.Translate(InputMap.ActionGetEvents("ui_down")[0].AsText())); operationTipBuilder.Append("[/color]"); operationTipBuilder.Append(TranslationServerUtils.Translate("action_jump_down")); } @@ -117,7 +154,7 @@ public partial class Player : CharacterTemplate operationTipBuilder.Append(Config.OperationTipActionColor); operationTipBuilder.Append(']'); operationTipBuilder.Append( - TranslationServerUtils.Translate(InputMap.ActionGetEvents("pick_up")[0].AsText())); + TranslationServerUtils.Translate(InputMap.ActionGetEvents("pick_up")[0].AsText())); operationTipBuilder.Append("[/color]"); operationTipBuilder.Append(TranslationServerUtils.Translate("action_pick_up")); operationTipLabel.Text = operationTipBuilder.ToString(); @@ -140,7 +177,7 @@ public partial class Player : CharacterTemplate operationTipBuilder.Append(Config.OperationTipActionColor); operationTipBuilder.Append(']'); operationTipBuilder.Append( - TranslationServerUtils.Translate(InputMap.ActionGetEvents("use_item")[0].AsText())); + TranslationServerUtils.Translate(InputMap.ActionGetEvents("use_item")[0].AsText())); operationTipBuilder.Append("[/color]"); operationTipBuilder.Append(TranslationServerUtils.Translate("action_use_item")); operationTipBuilder.Append(TranslationServerUtils.Translate(item.Name)); @@ -244,8 +281,8 @@ public partial class Player : CharacterTemplate } _parabola.Points = CurrentItem == null - ? _emptyVector2Array - : ParabolicUtils.ComputeParabolic(ItemMarker2D.Position, GetThrowVelocity(), Gravity, 0.1f); + ? _emptyVector2Array + : ParabolicUtils.ComputeParabolic(ItemMarker2D.Position, GetThrowVelocity(), Gravity, 0.1f); } @@ -403,9 +440,9 @@ public partial class Player : CharacterTemplate { var rotationDegreesNode2D = node2D.RotationDegrees; var rotationDegreesNode2DAbs = Math.Abs(rotationDegreesNode2D); - _floatLabel.Position = rotationDegreesNode2DAbs > 90 - ? new Vector2(0, PromptTextDistance) - : new Vector2(0, -PromptTextDistance); + _floatLabel.Position = rotationDegreesNode2DAbs > 90 + ? new Vector2(0, PromptTextDistance) + : new Vector2(0, -PromptTextDistance); _floatLabel.RotationDegrees = 0 - rotationDegreesNode2D; var label = _floatLabel.GetNode