111 lines
3.1 KiB
C#
111 lines
3.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Animancer;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITFALL.Entities.Transition
|
|
{
|
|
public sealed class EntityDamageTransition : EntityBehavior
|
|
{
|
|
[SerializeField] private List<EntityHitbox> damageParts;
|
|
[SerializeField, ReadOnly] private int currentThreshold;
|
|
[SerializeField] private Entity newEntity;
|
|
[SerializeField] private int initialThreshold;
|
|
[SerializeReference, SubclassSelector] private IReference transitionAnimation;
|
|
[Inject] private IHealth _health;
|
|
[Inject(true)] private IEntityOverride _override;
|
|
private bool _isAnimation;
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
currentThreshold = initialThreshold;
|
|
_health.OnDamageRelease += OnDamageRelease;
|
|
_health.OnSetAlive += OnSetAlive;
|
|
_health.OnDamageFactory += OnDamageFactory;
|
|
UnityEntity.AddListener<string>(Constant.Animation.OnPlayEnd, OnPlayEnd);
|
|
}
|
|
|
|
private int OnDamageFactory(DamageMessage arg1, int arg2)
|
|
{
|
|
return _isAnimation && _override.IsOvering ? 0 : arg2;
|
|
}
|
|
|
|
private void OnPlayEnd(string obj)
|
|
{
|
|
if(obj!=transitionAnimation.Value) return;
|
|
|
|
if(!newEntity) return;
|
|
var instance = Instantiate(newEntity, Transform.position, Transform.rotation, Transform.parent);
|
|
//TryGetComponent<Animator>(out var selfAnimator);
|
|
//instance.TryGetComponent<Animator>(out var newAnimator);
|
|
TryGetComponent<NavMeshAgent>(out var selfAgent);
|
|
instance.TryGetComponent<NavMeshAgent>(out var newAgent);
|
|
|
|
if (instance.TryGetComponent<AnimancerComponent>(out var animancerComponent))
|
|
{
|
|
animancerComponent.Evaluate(Time.deltaTime);
|
|
}
|
|
|
|
switch (selfAgent,newAgent)
|
|
{
|
|
case (not null,not null):
|
|
newAgent.Warp(selfAgent.transform.position);
|
|
break;
|
|
}
|
|
// switch (selfAnimator,newAnimator)
|
|
// {
|
|
// case (not null,not null):
|
|
// var selfRoot = selfAnimator.avatarRoot;
|
|
// var newRoot = newAnimator.avatarRoot;
|
|
// newRoot.position = selfRoot.position;
|
|
// newRoot.rotation = selfRoot.rotation;
|
|
// foreach (var x in selfRoot.GetComponentsInChildren<Transform>())
|
|
// {
|
|
// if (x == selfRoot)
|
|
// {
|
|
// continue;
|
|
// }
|
|
// var target = newRoot.Find(x.name);
|
|
// if (!target)
|
|
// {
|
|
// continue;
|
|
// }
|
|
// target.localPosition = x.localPosition;
|
|
// target.localRotation = x.localRotation;
|
|
// }
|
|
// break;
|
|
// }
|
|
_isAnimation = false;
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
{
|
|
if (obj)
|
|
{
|
|
currentThreshold = initialThreshold;
|
|
}
|
|
}
|
|
|
|
private void OnDamageRelease(DamageMessage obj)
|
|
{
|
|
if (_isAnimation) return;
|
|
if (obj.Hit is EntityHitbox hitbox && damageParts.Contains(hitbox) is false) return;
|
|
if (_override is { IsOvering: true }) return;
|
|
currentThreshold -= obj.Damage;
|
|
if (currentThreshold >= 0) return;
|
|
UnityEntity.Invoke(Constant.Animation.Play, transitionAnimation.Value);
|
|
|
|
_isAnimation = true;
|
|
}
|
|
}
|
|
|
|
}
|