using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using UnityEngine; using Debug = UnityEngine.Debug; using UnityEditor.Animations; public class Attack : MonoBehaviour { [Header("子弹预制体")] public GameObject bulletPrefab; [Header("子弹数据")] public BulletData bulletData; [Header("角色对象")] public Role role; [Header("攻击范围")] public float attackScope; [Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage; [Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间 private float lastAttackTime = 0f; // 上次攻击的时间 [HideInInspector] public List bulltes = new List(); [Header("角色动画控制器")] public Animator animator; [Header("火焰动画控制器")] public Animator fireAni; [Header("子弹起始点")] public Transform BulletStartPos; [Header("攻击速度")] public float AttackSpeed=1f; public Vector2 direction; [Header("子弹速度加成")]public float roleBulletSpeedAdd=0f; [Header("子弹数量")] public int BulletNumber = 1; public float AttackScope { [DebuggerStepThrough] get => attackScope; [DebuggerStepThrough] set { attackScope = value; GetComponent().radius = attackScope; //Debug.Log("攻击半径"+GetComponent().radius); } } public async void Start() { AttackScope = attackScope; bulletData = new BulletData(); bulletData.BulletScattering = 1; animator.SetFloat("AttackSpeed",AttackSpeed); CircleCollider2D circleCollider2D = GetComponent(); attackCooldown = transform.parent.GetComponent().AttackCD; while (true) { if (circleCollider2D) { // 检查是否可以进行攻击(基于冷却时间) if (Time.time - lastAttackTime >= attackCooldown) { Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope); foreach (Collider2D collider in colliders) { Role targetRole = collider.GetComponent(); if (targetRole && targetRole.camp != role.camp) { role.animationHighlight = 1; if (animator!=null) { if (bulletPrefab.GetComponent().myBulletType != BulletType.Spraying) { direction = (targetRole.transform.position - BulletStartPos.position).normalized; } //animator.SetInteger("State", 1); animator.SetTrigger("Attack"); lastAttackTime = Time.time; // 更新上次攻击时间 } else { /*attack(targetRole); lastAttackTime = Time.time; // 更新上次攻击时间*/ } break; // 只攻击一个目标 } else { if (animator != null) { //animator.SetInteger("State", 0); } else { role.animationHighlight = 0; } } } } await Task.Delay(100); // 延迟,避免过于频繁地检测 } else { return; } } } /*public void attack(Role targetRole) { if (bulletPrefab == null) { Debug.LogError("子弹预制体为空"); return; } Vector2 direction = (targetRole.transform.position - transform.position).normalized; if (bulletPrefab.GetComponent().myBulletType == BulletType.Spraying) { GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root); BulletGamobj.GetComponent().role = role; BulletGamobj.GetComponent().attackObj = this; // BulletGamobj.transform.up = direction; BulletGamobj.transform.position =new Vector2(transform.position.x+0.15f, transform.position.y+0.2f); bulltes.Add(BulletGamobj); return; } else { for (int i = 0; i < bulletData.BulletScattering; i++) { // 改变子弹方向,根据角色自身的攻击范围来计算 GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root); BulletGamobj.GetComponent().role = role; BulletGamobj.GetComponent().attackObj = this; BulletGamobj.transform.up = direction; BulletGamobj.transform.position = transform.position; bulltes.Add(BulletGamobj); } } }*/ public void Pointattack() { if (bulletPrefab == null) { Debug.LogError("子弹预制体为空"); return; } for (int i = 0; i < BulletNumber; i++) { // 改变子弹方向,根据角色自身的攻击范围来计算 GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root); BulletGamobj.GetComponent().role = role; BulletGamobj.GetComponent().attackObj = this; BulletGamobj.GetComponent().bulletData.BulletSpeed *= (1 + roleBulletSpeedAdd); BulletGamobj.transform.up = direction; BulletGamobj.transform.position = BulletStartPos.position; bulltes.Add(BulletGamobj); } } public void Fireattack() { if (bulletPrefab == null) { Debug.LogError("子弹预制体为空"); return; } //需要修改 if (bulletPrefab.GetComponent().myBulletType == BulletType.Spraying) { // 计算每个子弹之间的角度间隔 float angleStep = BulletNumber > 1 ? 30f / (BulletNumber - 1) : 0f; // 计算起始角度,使得子弹整体居中 float startAngle = -30f / 2f; for (int i = 0; i < BulletNumber; i++) { // 计算当前子弹的旋转角度 float currentAngle = startAngle + (angleStep * i); // 生成子弹对象并设置父物体 GameObject bulletGameObj = GameObject.Instantiate(bulletPrefab, transform.root); // 设置子弹的初始位置 bulletGameObj.transform.position = new Vector2(transform.position.x + 0.15f, transform.position.y + 0.2f); // 设置子弹的角色和攻击者 Bullet bulletComponent = bulletGameObj.GetComponent(); if (bulletComponent != null) { bulletComponent.role = role; bulletComponent.attackObj = this; } // 设置子弹的旋转角度 bulletGameObj.transform.rotation = Quaternion.Euler(0, 0, currentAngle); // 将生成的子弹添加到子弹列表中 bulltes.Add(bulletGameObj); } } } public void EndFire() { fireAni.SetInteger("State", 1); //Debug.Log("停止火焰"); } private void OnDisable() { bulltes.Clear(); } }