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

172 lines
5.9 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-10-20 19:31:12 +08:00
using BITFALL.Player.Movement;
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-10-20 19:31:12 +08:00
using BITKit.SceneManagement;
using BITKit.Selection;
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-10-20 19:31:12 +08:00
public class UXHud : UIToolKitPanel
2023-06-08 14:09:50 +08:00
{
2023-06-17 16:30:53 +08:00
[Header(Constant.Header.Providers)]
2023-10-20 19:31:12 +08:00
[SerializeReference, SubclassSelector] private ISceneService sceneService;
2023-09-01 14:33:54 +08:00
[SerializeReference, SubclassSelector] private IPlayerService playerService;
[SerializeReference, SubclassSelector] private ISteamService steamService;
[Header(Constant.Header.Components)]
2023-10-20 19:31:12 +08:00
[SerializeField] private UXLabel seleableLabel;
2023-09-01 14:33:54 +08:00
[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;
2023-10-20 19:31:12 +08:00
[SerializeField] private UXElement playerInfo;
2023-09-01 14:33:54 +08:00
[SerializeField] private UXBar healthBar;
2023-10-20 19:31:12 +08:00
[SerializeField] private UXBar lerpHealthBar;
[SerializeField] private UXBar staminaBar;
2023-09-01 14:33:54 +08:00
2023-06-08 14:09:50 +08:00
[Header(Constant.Header.Input)]
public InputActionReference inventoryAction;
public InputActionReference returnAction;
public InputActionGroup inputActionGroup = new();
2023-10-20 19:31:12 +08:00
[Inject]
2023-08-27 02:58:19 +08:00
private IHealth _health;
2023-10-20 19:31:12 +08:00
[Inject]
private IEntityMovement _entityMovement;
[Inject]
private IPlayerMovement _playerMovement;
[Inject]
2023-10-02 23:24:56 +08:00
private IEquipService _equipService;
2023-10-20 19:31:12 +08:00
[Inject]
private ISelector _selector;
private float _currentHealthLerp;
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();
2023-10-20 19:31:12 +08:00
seleableLabel.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-10-20 19:31:12 +08:00
sceneService.OnSceneLoaded += x=>Entry();
2023-09-01 14:33:54 +08:00
2023-10-20 19:31:12 +08:00
playerService.OnPlayerInitialized += OnPlayerInitializedLocalPlayer;
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()
{
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)
{
2023-10-20 19:31:12 +08:00
seleableLabel.SetActive(false);
2023-06-08 14:09:50 +08:00
}
2023-10-20 19:31:12 +08:00
public void OnSelect(ISelectable selectable)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
seleableLabel.SetActive(true);
seleableLabel.Set(selectable.Transform.TryGetComponent<IDescription>(out var description)
? description.Name
: "互动");
2023-06-08 14:09:50 +08:00
}
public void OnInactive(ISelectable selectable)
{
2023-10-20 19:31:12 +08:00
seleableLabel.SetActive(false);
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
{
2023-10-20 19:31:12 +08:00
entity.Inject(this);
2023-08-27 02:58:19 +08:00
_health.OnSetAlive += OnSetAlive;
2023-09-01 14:33:54 +08:00
_health.OnSetHealthPoint += OnSetHP;
2023-10-20 19:31:12 +08:00
_selector.OnActive += OnActive;
_selector.OnInactive += OnInactive;
_selector.OnSelected += OnSelect;
if (steamService.IsInitialized)
{
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-10-20 19:31:12 +08:00
OnSetHP(_health.HealthPoint);
OnSetAlive(_health.IsAlive);
OnInactive(null);
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
2023-10-20 19:31:12 +08:00
private void Update()
{
if (_health is not null)
{
_currentHealthLerp = Mathf.MoveTowards(_currentHealthLerp, _health.HealthPoint * 0.01f, Time.deltaTime * 5).Fix();
_currentHealthLerp = Mathf.Clamp(_currentHealthLerp, 0, 1);
lerpHealthBar.SetDirect(_currentHealthLerp,_health.HealthPoint.ToString());
staminaBar.Set(_playerMovement.Stamina * 0.01f);
staminaBar.visualElement.parent.SetActive(_playerMovement.Stamina is not 100);
}
}
2023-09-01 14:33:54 +08:00
private void LateUpdate()
{
if (playerService.LocalPlayer is null) return;
2023-10-20 19:31:12 +08:00
2023-09-01 14:33:54 +08:00
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())
2023-10-20 19:31:12 +08:00
UXService.Entry<UXMenu>();
2023-08-27 02:58:19 +08:00
}
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-10-20 19:31:12 +08:00
crosshairImage.SetActive(alive);
crosshairParentImage.SetActive(alive);
playerInfo.SetActive(alive);
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void OnSetHP(int hp)
{
2023-10-20 19:31:12 +08:00
healthBar.Set(Mathf.Clamp(hp,0,100) * 0.01f);
2023-09-01 14:33:54 +08:00
}
2023-06-08 14:09:50 +08:00
}
}