using ColdMint.scripts.character;
using ColdMint.scripts.loader.uiLoader;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.furniture;
///
/// GUIFurnitureTemplate
/// 带有图形用户页面的家居模板
///
public partial class GuiFurniture : Furniture
{
///
/// Operating range of furniture
/// 家具的操作范围
///
///
///For furniture with graphical user pages, the player must enter the action range and press the shortcut key to display the UI page.
///对于带有图形用户页面的家具来说,玩家必须进入操作范围内按下快捷键才能显示UI页面。
///
private Area2D? _operateArea2D;
///
/// Whether the player is within range of the operation
/// 玩家是否在操作范围内
///
private bool _playerInRange;
[Export]
public string? Path;
///
/// There's a mouse hover
/// 有鼠标悬停
///
private bool _hasMouseOver;
public override void _Ready()
{
base._Ready();
InputPickable = true;
_operateArea2D = GetNode("OperateArea2D");
_operateArea2D.BodyEntered += OnBodyEntered;
_operateArea2D.BodyExited += OnBodyExited;
_operateArea2D.SetCollisionMaskValue(Config.LayerNumber.Player, true);
if (Path != null)
{
GameSceneDepend.DynamicUiGroup?.RegisterControl(Path, () =>
{
var packedScene = GD.Load(Path);
return NodeUtils.InstantiatePackedScene(packedScene);
});
}
}
///
/// Use furniture
/// 使用家具
///
///
private void Use(Player player)
{
if (Path == null)
{
return;
}
GameSceneDepend.DynamicUiGroup?.ShowControl(Path);
}
public override void _MouseEnter()
{
_hasMouseOver = true;
}
public override void _MouseExit()
{
_hasMouseOver = false;
}
private void OnBodyEntered(Node node)
{
if (node is Player)
{
_playerInRange = true;
}
}
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
if (GameSceneDepend.Player == null || !_playerInRange || !_hasMouseOver)
{
return;
}
if (Input.IsActionJustPressed("use_item"))
{
Use(GameSceneDepend.Player);
}
}
private void OnBodyExited(Node2D node2D)
{
if (node2D is Player)
{
_playerInRange = false;
if (Path != null)
{
GameSceneDepend.DynamicUiGroup?.HideControl(Path);
}
}
}
public override void _ExitTree()
{
if (_operateArea2D != null)
{
_operateArea2D.BodyEntered -= OnBodyEntered;
_operateArea2D.BodyExited -= OnBodyExited;
}
}
}