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

81 lines
2.5 KiB
C#

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
{
[Header(Constant.Header.Providers)]
[SerializeField, SerializeReference, SubclassSelector]
private INetClient netClient;
[SerializeField, SerializeReference, SubclassSelector]
private INetProvider netProvider;
[Header(Constant.Header.Input)]
public InputActionReference inventoryAction;
public InputActionReference returnAction;
public InputActionGroup inputActionGroup = new();
public UXLabel nameLabel;
public override void OnStart()
{
base.OnStart();
inputActionGroup.RegisterCallback(returnAction, OnReturn);
inputActionGroup.RegisterCallback(inventoryAction, OnInventry);
netClient.OnConnected += OnConnected;
}
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()
{
IEntity.OnSpawnLocalPlayer += OnSpawnLocalPlayer;
}
void OnDisable()
{
IEntity.OnSpawnLocalPlayer -= OnSpawnLocalPlayer;
}
void OnConnected()
{
UXFramework.Enter<HUD>();
}
void Start()
{
nameLabel.SetActive(false);
}
void OnSpawnLocalPlayer(IEntity entity)
{
entity.RegisterCallback<ISelectableCallback>(this);
}
void OnInventry(InputAction.CallbackContext context)
{
if (context.performed)
UXFramework.Enter<Inventory>();
}
void OnReturn(InputAction.CallbackContext context)
{
UXFramework.Enter<Menu>();
}
}
}