2024-06-19 16:02:32 +00:00
|
|
|
using System;
|
2024-06-12 15:42:35 +00:00
|
|
|
using ColdMint.scripts.map.events;
|
2024-05-07 09:38:50 +00:00
|
|
|
using ColdMint.scripts.utils;
|
2024-04-28 13:55:19 +00:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.inventory;
|
|
|
|
|
2024-05-04 14:59:46 +00:00
|
|
|
/// <summary>
|
|
|
|
/// <para>HotBar</para>
|
|
|
|
/// <para>快捷物品栏</para>
|
|
|
|
/// </summary>
|
2024-06-12 13:33:29 +00:00
|
|
|
public partial class HotBar : HBoxContainer
|
2024-04-28 13:55:19 +00:00
|
|
|
{
|
2024-06-12 15:42:35 +00:00
|
|
|
private IItemContainer? _itemContainer;
|
2024-06-21 14:55:31 +00:00
|
|
|
|
2024-06-19 16:02:32 +00:00
|
|
|
Action<SelectedItemSlotChangeEvent>? SelectedItemSlotChangeEvent { get; set; }
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
base._Ready();
|
2024-06-12 15:42:35 +00:00
|
|
|
_itemContainer = new UniversalItemContainer();
|
2024-06-21 14:55:31 +00:00
|
|
|
_itemContainer.SupportSelect = true;
|
2024-06-19 16:02:32 +00:00
|
|
|
_itemContainer.SelectedItemSlotChangeEvent += SelectedItemSlotChangeEvent;
|
2024-05-07 09:38:50 +00:00
|
|
|
NodeUtils.DeleteAllChild(this);
|
2024-05-03 14:35:54 +00:00
|
|
|
for (var i = 0; i < Config.HotBarSize; i++)
|
2024-04-28 13:55:19 +00:00
|
|
|
{
|
2024-06-14 04:44:31 +00:00
|
|
|
_itemContainer.AddItemSlot(this);
|
2024-06-12 15:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-03 14:35:54 +00:00
|
|
|
public override void _Process(double delta)
|
|
|
|
{
|
|
|
|
base._Process(delta);
|
|
|
|
if (Input.IsActionJustPressed("hotbar_next"))
|
|
|
|
{
|
|
|
|
//Mouse wheel down
|
|
|
|
//鼠标滚轮向下
|
2024-06-12 15:42:35 +00:00
|
|
|
_itemContainer?.SelectTheNextItemSlot();
|
2024-05-03 14:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_previous"))
|
|
|
|
{
|
|
|
|
//Mouse wheel up
|
|
|
|
//鼠标滚轮向上
|
2024-06-12 15:42:35 +00:00
|
|
|
_itemContainer?.SelectThePreviousItemSlot();
|
2024-05-03 14:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_1"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_2"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_3"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_4"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_5"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_6"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_7"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_8"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("hotbar_9"))
|
|
|
|
{
|
|
|
|
SelectItemSlotByHotBarShortcutKey(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <para>Select the HotBar project using the shortcut keys</para>
|
|
|
|
/// <para>通过快捷键选择HotBar项目</para>
|
|
|
|
/// </summary>
|
|
|
|
/// <para>The Pc version of the shortcut key index is 0-9</para>
|
|
|
|
/// <para>Pc版本的快捷键索引为0-9</para>
|
|
|
|
/// <param name="shortcutKeyIndex"></param>
|
|
|
|
private void SelectItemSlotByHotBarShortcutKey(int shortcutKeyIndex)
|
|
|
|
{
|
2024-06-12 15:42:35 +00:00
|
|
|
if (_itemContainer == null)
|
2024-06-06 13:05:51 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-06-19 16:02:32 +00:00
|
|
|
|
2024-06-12 15:42:35 +00:00
|
|
|
_itemContainer.SelectItemSlot(shortcutKeyIndex);
|
2024-05-06 10:59:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 13:33:29 +00:00
|
|
|
public IItemContainer? GetItemContainer()
|
2024-06-06 14:43:07 +00:00
|
|
|
{
|
2024-06-12 15:42:35 +00:00
|
|
|
return _itemContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _ExitTree()
|
|
|
|
{
|
|
|
|
base._ExitTree();
|
2024-06-19 16:02:32 +00:00
|
|
|
if (_itemContainer != null)
|
|
|
|
{
|
|
|
|
_itemContainer.SelectedItemSlotChangeEvent -= SelectedItemSlotChangeEvent;
|
|
|
|
}
|
2024-04-28 13:55:19 +00:00
|
|
|
}
|
|
|
|
}
|