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

134 lines
4.7 KiB
C#
Raw Normal View History

2023-08-23 01:59:40 +08:00
using System;
2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
2023-10-02 23:24:56 +08:00
using BITFALL.Player.Equip;
2023-06-08 14:09:50 +08:00
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.InputSystem;
using BITKit;
using BITKit.UX;
using BITKit.Entities;
2023-08-27 02:58:19 +08:00
using BITKit.Entities.Player;
2023-09-01 14:33:54 +08:00
using BITKit.Steamwork;
2023-10-02 23:24:56 +08:00
using UnityEditor;
2023-08-27 02:58:19 +08:00
2023-06-08 14:09:50 +08:00
namespace BITFALL.UX
{
2023-08-27 02:58:19 +08:00
public class UXHud : UIToolKitPanel, ISelectableCallback
2023-06-08 14:09:50 +08:00
{
2023-06-17 16:30:53 +08:00
[Header(Constant.Header.Providers)]
2023-09-01 14:33:54 +08:00
[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;
2023-10-02 23:24:56 +08:00
[SerializeField] private UXImage crosshairParentImage;
2023-09-01 14:33:54 +08:00
[SerializeField] private UXLabel playerNameLabel;
[SerializeField] private UXImage playerAvatarImage;
[SerializeField] private UXBar healthBar;
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-27 02:58:19 +08:00
private IHealth _health;
2023-09-01 14:33:54 +08:00
protected IEntityMovement _entityMovement;
2023-10-02 23:24:56 +08:00
private IEquipService _equipService;
2023-08-23 01:59:40 +08:00
protected override void Awake()
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
base.Awake();
nameLabel.SetActive(false);
2023-06-08 14:09:50 +08:00
inputActionGroup.RegisterCallback(returnAction, OnReturn);
2023-08-27 02:58:19 +08:00
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
2023-06-08 14:09:50 +08:00
2023-08-23 01:59:40 +08:00
netClient.OnConnected += OnConnected;
2023-08-27 02:58:19 +08:00
playerService.OnPlayerInitialized += OnPlayerInitializedLocalPlayer;
2023-09-01 14:33:54 +08:00
2023-06-08 14:09:50 +08:00
}
2023-08-12 01:43:24 +08:00
2023-08-23 01:59:40 +08:00
private void OnDestroy()
{
netClient.OnConnected -= OnConnected;
2023-08-27 02:58:19 +08:00
playerService.OnPlayerInitialized -= OnPlayerInitializedLocalPlayer;
2023-08-23 01:59:40 +08:00
}
protected override void OnEntryOrExit(bool isEntry)
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
2023-08-23 01:59:40 +08:00
inputActionGroup.allowInput.SetElements(this, isEntry);
2023-06-08 14:09:50 +08:00
}
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);
}
2023-08-23 01:59:40 +08:00
private void OnConnected()
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
UXService.Entry<UXHud>();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private async void OnPlayerInitializedLocalPlayer(IEntity entity)
2023-06-08 14:09:50 +08:00
{
entity.RegisterCallback<ISelectableCallback>(this);
2023-08-27 02:58:19 +08:00
_health = entity.Get<IHealth>();
2023-09-01 14:33:54 +08:00
_entityMovement = entity.Get<IEntityMovement>();
2023-08-27 02:58:19 +08:00
_health.OnSetAlive += OnSetAlive;
2023-09-01 14:33:54 +08:00
_health.OnSetHealthPoint += OnSetHP;
if (!steamService.IsInitialized) return;
playerNameLabel.Set(steamService.Name);
var avatar = await steamService.GetAvatarAsync(cancellationToken);
playerAvatarImage.SetTexture(avatar);
2023-10-02 23:24:56 +08:00
_equipService = entity.Get<IEquipService>();
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
private void OnInventory(InputAction.CallbackContext context)
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
if(playerService.LocalPlayer is null) return;
if(_health.IsAlive is false) return;
if(context.JustPressed())
UXService.Entry<UXInventory>();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void LateUpdate()
{
if (playerService.LocalPlayer is null) return;
crosshairImage.SetPosition(_entityMovement.ViewCenter + _entityMovement.ViewRotation * Vector3.forward);
2023-10-02 23:24:56 +08:00
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);
2023-09-01 14:33:54 +08:00
}
2023-08-27 02:58:19 +08:00
private static void OnReturn(InputAction.CallbackContext context)
{
if (context.JustPressed())
UXService.Entry<Menu>();
}
private void OnSetAlive(bool alive)
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
if (alive is false)
{
Entry();
}
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void OnSetHP(int hp)
{
healthBar.Set(hp);
}
2023-06-08 14:09:50 +08:00
}
}