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

134 lines
4.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BITFALL.Player.Equip;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.InputSystem;
using BITKit;
using BITKit.UX;
using BITKit.Entities;
using BITKit.Entities.Player;
using BITKit.Steamwork;
using UnityEditor;
namespace BITFALL.UX
{
public class UXHud : UIToolKitPanel, ISelectableCallback
{
[Header(Constant.Header.Providers)]
[SerializeReference, SubclassSelector] private INetClient netClient;
[SerializeReference, SubclassSelector] private INetProvider netProvider;
[SerializeReference, SubclassSelector] private IPlayerService playerService;
[SerializeReference, SubclassSelector] private ISteamService steamService;
[Header(Constant.Header.Components)]
[SerializeField] private UXImage crosshairImage;
[SerializeField] private UXImage crosshairParentImage;
[SerializeField] private UXLabel playerNameLabel;
[SerializeField] private UXImage playerAvatarImage;
[SerializeField] private UXBar healthBar;
[Header(Constant.Header.Input)]
public InputActionReference inventoryAction;
public InputActionReference returnAction;
public InputActionGroup inputActionGroup = new();
public UXLabel nameLabel;
private IHealth _health;
protected IEntityMovement _entityMovement;
private IEquipService _equipService;
protected override void Awake()
{
base.Awake();
nameLabel.SetActive(false);
inputActionGroup.RegisterCallback(returnAction, OnReturn);
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
netClient.OnConnected += OnConnected;
playerService.OnPlayerInitialized += OnPlayerInitializedLocalPlayer;
}
private void OnDestroy()
{
netClient.OnConnected -= OnConnected;
playerService.OnPlayerInitialized -= OnPlayerInitializedLocalPlayer;
}
protected override void OnEntryOrExit(bool isEntry)
{
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
inputActionGroup.allowInput.SetElements(this, isEntry);
}
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);
}
private void OnConnected()
{
UXService.Entry<UXHud>();
}
private async void OnPlayerInitializedLocalPlayer(IEntity entity)
{
entity.RegisterCallback<ISelectableCallback>(this);
_health = entity.Get<IHealth>();
_entityMovement = entity.Get<IEntityMovement>();
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
if (!steamService.IsInitialized) return;
playerNameLabel.Set(steamService.Name);
var avatar = await steamService.GetAvatarAsync(cancellationToken);
playerAvatarImage.SetTexture(avatar);
_equipService = entity.Get<IEquipService>();
}
private void OnInventory(InputAction.CallbackContext context)
{
if(playerService.LocalPlayer is null) return;
if(_health.IsAlive is false) return;
if(context.JustPressed())
UXService.Entry<UXInventory>();
}
private void LateUpdate()
{
if (playerService.LocalPlayer is null) return;
crosshairImage.SetPosition(_entityMovement.ViewCenter + _entityMovement.ViewRotation * Vector3.forward);
if(_equipService is null) return;
var currentCrosshairOpacity = crosshairParentImage.visualElement.style.opacity.value;
crosshairParentImage.visualElement.style.opacity = Mathf.Lerp(currentCrosshairOpacity, _equipService.Zoom.Allow ? 0 : 1, Time.deltaTime * 5);
}
private static void OnReturn(InputAction.CallbackContext context)
{
if (context.JustPressed())
UXService.Entry<Menu>();
}
private void OnSetAlive(bool alive)
{
if (alive is false)
{
Entry();
}
}
private void OnSetHP(int hp)
{
healthBar.Set(hp);
}
}
}