84 lines
2.2 KiB
C#
84 lines
2.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.Entities.Player;
|
|
using BITKit.UX;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.UX
|
|
{
|
|
public class UXHitmarker : MonoBehaviour
|
|
{
|
|
[SerializeReference, SubclassSelector] private IPlayerService playerService;
|
|
[SerializeReference, SubclassSelector] private IDamageService damageService;
|
|
[SerializeField] private UXElement hitmarkerImage;
|
|
private CancellationTokenSource nextHitmarkerCancellationTokenSource=new();
|
|
|
|
private void Start()
|
|
{
|
|
playerService.OnPlayerInitialized += OnPlayerInitialized;
|
|
damageService.OnEntityDamaged += OnDamage;
|
|
damageService.OnEntityKilled += OnKilled;
|
|
|
|
destroyCancellationToken.Register(nextHitmarkerCancellationTokenSource.Cancel);
|
|
destroyCancellationToken.Register(() =>
|
|
{
|
|
playerService.OnPlayerInitialized -= OnPlayerInitialized;
|
|
damageService.OnEntityDamaged -= OnDamage;
|
|
damageService.OnEntityKilled -= OnKilled;
|
|
});
|
|
|
|
hitmarkerImage.GetVisualElement().SetOpacity(0);
|
|
|
|
}
|
|
|
|
|
|
private void OnPlayerInitialized(Entity obj)
|
|
{
|
|
obj.AddListener<DamageMessage>(OnDamage);
|
|
}
|
|
|
|
private void OnKilled(DamageMessage obj)
|
|
{
|
|
hitmarkerImage.GetVisualElement().style.unityBackgroundImageTintColor = Color.red;
|
|
Execute(obj,1);
|
|
}
|
|
|
|
private void OnDamage(DamageMessage obj)
|
|
{
|
|
hitmarkerImage.GetVisualElement().style.unityBackgroundImageTintColor = Color.white;
|
|
Execute(obj);
|
|
}
|
|
|
|
private async void Execute(DamageMessage obj,float duration=0.1f)
|
|
{
|
|
if ((Entity)obj.Target == playerService.LocalPlayer) return;
|
|
|
|
if ((Entity)obj.Initiator != playerService.LocalPlayer) return;
|
|
|
|
nextHitmarkerCancellationTokenSource?.Cancel();
|
|
|
|
nextHitmarkerCancellationTokenSource = new CancellationTokenSource();
|
|
|
|
hitmarkerImage.GetVisualElement().SetOpacity(1);
|
|
|
|
try
|
|
{
|
|
await Task.Delay(TimeSpan.FromSeconds(duration), nextHitmarkerCancellationTokenSource.Token);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return;
|
|
}
|
|
|
|
hitmarkerImage.GetVisualElement().SetOpacity(0);
|
|
}
|
|
}
|
|
|
|
}
|