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 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(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(out var selfAnimator); //instance.TryGetComponent(out var newAnimator); TryGetComponent(out var selfAgent); instance.TryGetComponent(out var newAgent); if (instance.TryGetComponent(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()) // { // 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; } } }