BITFALL/Assets/Artists/Scripts/UX/HUD.cs

84 lines
2.6 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.InputSystem;
using BITKit;
using BITKit.UX;
using BITKit.Entities;
namespace BITFALL.UX
{
public class HUD : UXPanel, ISelectableCallback
{
2023-06-17 16:30:53 +08:00
[Header(Constant.Header.Providers)]
[SerializeField, SerializeReference, SubclassSelector]
private INetClient netClient;
[SerializeField, SerializeReference, SubclassSelector]
private INetProvider netProvider;
2023-06-08 14:09:50 +08:00
[Header(Constant.Header.Input)]
public InputActionReference inventoryAction;
public InputActionReference returnAction;
public InputActionGroup inputActionGroup = new();
public UXLabel nameLabel;
2023-08-12 01:43:24 +08:00
2023-06-08 14:09:50 +08:00
public override void OnStart()
{
base.OnStart();
inputActionGroup.RegisterCallback(returnAction, OnReturn);
inputActionGroup.RegisterCallback(inventoryAction, OnInventry);
2023-08-12 01:43:24 +08:00
if (netClient is not null)
netClient.OnConnected += OnConnected;
2023-06-08 14:09:50 +08:00
}
2023-08-12 01:43:24 +08:00
2023-06-08 14:09:50 +08:00
public override void Set(bool active)
{
base.Set(active);
inputActionGroup.RegisterCallback(inventoryAction, OnInventry);
inputActionGroup.allowInput.SetElements(this, active);
}
public void OnActive(ISelectable selectable)
{
}
public void OnHover(ISelectable selectable)
{
if (!selectable.GetTransform().TryGetComponent<IDescription>(out var description)) return;
nameLabel.SetActive(true);
nameLabel.Set(description.Name);
}
public void OnInactive(ISelectable selectable)
{
nameLabel.SetActive(false);
}
void OnEnable()
{
2023-08-12 01:43:24 +08:00
IEntity.OnPlayerInitializedLocalPlayer += OnPlayerInitializedLocalPlayer;
2023-06-08 14:09:50 +08:00
}
void OnDisable()
{
2023-08-12 01:43:24 +08:00
IEntity.OnPlayerInitializedLocalPlayer -= OnPlayerInitializedLocalPlayer;
2023-06-08 14:09:50 +08:00
}
void OnConnected()
{
UXFramework.Enter<HUD>();
}
void Start()
{
nameLabel.SetActive(false);
}
2023-08-12 01:43:24 +08:00
void OnPlayerInitializedLocalPlayer(IEntity entity)
2023-06-08 14:09:50 +08:00
{
entity.RegisterCallback<ISelectableCallback>(this);
}
void OnInventry(InputAction.CallbackContext context)
{
if (context.performed)
UXFramework.Enter<Inventory>();
}
void OnReturn(InputAction.CallbackContext context)
{
UXFramework.Enter<Menu>();
}
}
}