using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Unity.Mathematics; using UnityEngine; public class PlayerAttackAnimatorFactory : MonoBehaviour { public static PlayerAttackAnimatorFactory Instance; private Player Player; private bool AllowAttacks; protected FloatingTextSpawner TextPopUp; public Animator MeleeAnimator; public List MeleeAnimationNames; public List MeleeAttackState; private Dictionary MeleeAnimationNameDict; public void Awake() { Instance = this; Player = GetComponentInParent(); TextPopUp = new FloatingTextSpawner(this.transform, 0.5f); BuildMeleeAnimationClips(); } private void BuildMeleeAnimationClips() { MeleeAnimationNameDict = new Dictionary(); for (int i = 0; i < MeleeAttackState.Count; i++) MeleeAnimationNameDict.Add(MeleeAttackState[i], MeleeAnimationNames[i]); } public Action OnHit; private void OnTriggerEnter2D(Collider2D other) { if (!AllowAttacks) return; if (!other.CompareTag("EnemyHitBox")) return; Debug.Log($"Colliding with {other.gameObject.name}"); TextPopUp.SpawnFloatingText($"Hit", Color.red, 3); OnHit?.Invoke(other.gameObject); ApplyKnockBack(other); } private void ApplyKnockBack(Collider2D other) { Rigidbody2D parentRb = other.transform.root.GetComponent(); if (parentRb == null) return; Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 direction = mouseWorldPos - Player.transform.parent.gameObject.transform.position; parentRb.AddForce(direction * 10f, ForceMode2D.Impulse); //KnockbackUtility.ApplyKnockback(this, rb2D, hitDirection, 10f, 0.3f); } public void AllowAttack(int allow) { AllowAttacks = (allow == 1); } public void RotateTowardsPlayerDirection() { //float angle = Mathf.Atan2(Player.PrevDirection.y, Player.PrevDirection.x) * Mathf.Rad2Deg; //angle = (angle + 360f) % 360f; //Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Vector2 direction = mouseWorldPos - Player.transform.position; float angle = Mathf.Atan2(Player.PrevDirection.y, Player.PrevDirection.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0, 0, angle); } public void MeleeResetKineticCharge() { //Melee.CrossFade("Blank", 0, 1); } public void MeleeBasic(MeleeFighterClass.AttackState state) { Debug.Log(state.ToString()); if (!MeleeAnimationNameDict.ContainsKey(state)) return; RotateTowardsPlayerDirection(); MeleeAnimator.CrossFade(MeleeAnimationNameDict[state], 0, 0); } public void MeleeKineticSurge() { return; MeleeAnimator.CrossFade("KineticSurgeRelease", 0, 0); } }