diff --git a/Role/Attack.cs b/Role/Attack.cs index 117a0e6..c7d1ef8 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using UnityEngine; using Debug = UnityEngine.Debug; using UnityEditor.Animations; +using DG.Tweening; public class Attack : MonoBehaviour { @@ -17,7 +18,7 @@ public class Attack : MonoBehaviour [Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间 private float lastAttackTime = 0f; // 上次攻击的时间 - + [HideInInspector] public List bulltes = new List(); @@ -35,6 +36,11 @@ public class Attack : MonoBehaviour [Header("攻击碰撞体")] public CircleCollider2D attackCollider; [Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox; [Header("攻击范围图片")] public SpriteRenderer attackRangeSprite; + [Header("攻击持续时间")] public float AttackStayTime;// + + public bool isAttack = true; + public bool flag = false; + [HideInInspector] public float timer = 0; //public float AttackScope //{ // [DebuggerStepThrough] @@ -50,40 +56,37 @@ public class Attack : MonoBehaviour public async void Start() { - //AttackScope = attackScope; bulletData = new BulletData(); bulletData.BulletScattering = 1; - - animator.SetFloat("AttackSpeed",AttackSpeed); - - //CircleCollider2D circleCollider2D = GetComponent(); - + animator.SetFloat("AttackSpeed", AttackSpeed); + attackCooldown = transform.parent.GetComponent().AttackCD; - SetAttackRange();//设置攻击范围 + SetAttackRange(); // 设置攻击范围 - while (true) + while (isAttack) { - if (attackCollider|| attackColliderBox) - { - + if (attackCollider || attackColliderBox) + { // 检查是否可以进行攻击(基于冷却时间) if (Time.time - lastAttackTime >= attackCooldown) { + + if (attackCollider) { Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius); GetAllColliderInAttackRange(colliders); } - else if (attackColliderBox)// 使用 OverlapBoxAll 来检测矩形区域内的所有碰撞体 + else if (attackColliderBox) { // 获取碰撞体的尺寸 Vector2 boxSize = attackColliderBox.size; // 只检测右边一半,修改尺寸 - boxSize.x /= 2f; // 将宽度减半,只覆盖右边一半区域 + boxSize.x /= 2f; // 获取碰撞体的中心位置 Vector3 colliderCenter = attackColliderBox.transform.position; @@ -92,20 +95,15 @@ public class Attack : MonoBehaviour Vector3 rightCenter = new Vector3(colliderCenter.x + attackColliderBox.size.x / 4f, colliderCenter.y, colliderCenter.z); // 使用OverlapBoxAll检测右半部分区域 - Collider2D[] colliders = Physics2D.OverlapBoxAll( - rightCenter, // 右半部分的中心位置 - boxSize, // 新的尺寸,只覆盖右半部分 - 0f // 没有旋转 - ); - Debug.Log("进入范围"); + Collider2D[] colliders = Physics2D.OverlapBoxAll(rightCenter, boxSize, 0f); GetAllColliderInAttackRange(colliders); } - - - - + lastAttackTime = Time.time; // 更新上次攻击时间 } + + + await Task.Delay(100); // 延迟,避免过于频繁地检测 } else @@ -115,7 +113,24 @@ public class Attack : MonoBehaviour } } - public void GetAllColliderInAttackRange(Collider2D[] colliders)//遍历所有在攻击范围内的敌人 + + private void Update() + { + if (flag) + { + timer += Time.deltaTime; + if (timer>AttackStayTime) + { + animator.SetInteger("State", 0); + isAttack = true; + timer = 0; + flag = false; + } + + } + } + + public void GetAllColliderInAttackRange(Collider2D[] colliders) { foreach (Collider2D collider in colliders) { @@ -128,23 +143,26 @@ public class Attack : MonoBehaviour if (bulletPrefab.GetComponent().myBulletType != BulletType.Spraying) { direction = (targetRole.transform.position - BulletStartPos.position).normalized; + animator.SetTrigger("Attack"); } - //animator.SetInteger("State", 1); - animator.SetTrigger("Attack"); - lastAttackTime = Time.time; // 更新上次攻击时间 + else if (bulletPrefab.GetComponent().myBulletType == BulletType.Spraying) + { + animator.SetInteger("State", 1); - } - else - { - /*attack(targetRole); - lastAttackTime = Time.time; // 更新上次攻击时间*/ + flag = true; + + + + + } + + lastAttackTime = Time.time; // 更新上次攻击时间 } break; // 只攻击一个目标 } else { - if (animator != null) { //animator.SetInteger("State", 0); @@ -153,7 +171,6 @@ public class Attack : MonoBehaviour { role.animationHighlight = 0; } - } } } @@ -195,7 +212,7 @@ public class Attack : MonoBehaviour } }*/ - + public void Pointattack() { diff --git a/Role/Role.cs b/Role/Role.cs index 9b4a4b6..4a73339 100644 --- a/Role/Role.cs +++ b/Role/Role.cs @@ -75,6 +75,7 @@ public class Role : Fun public float AttackCD;//CD public float CritRate;// public float CriticalHitRateBonus;//ӳ + public float Hp { @@ -535,7 +536,10 @@ public class Role : Fun //Debug.LogError("˼ӳ"); } - + if (GetComponent()) + { + GetComponent().HarmNumber = Mathf.Round(hurt); + } return Mathf.Round(hurt); } diff --git a/Role/move/2D/SimplePathfindingDoTween.cs b/Role/move/2D/SimplePathfindingDoTween.cs index 462ad7a..16ea7ee 100644 --- a/Role/move/2D/SimplePathfindingDoTween.cs +++ b/Role/move/2D/SimplePathfindingDoTween.cs @@ -85,7 +85,7 @@ public class SimplePathfindingDoTween : Fun DOTween.To(() => currentSpeedFactor, x => pathTween.timeScale = x, newSpeedFactor, restoreTime) .OnKill(() => { - Debug.Log("ü+++++++++++++++++"); + // Debug.Log("ü+++++++++++++++++"); // ӳٽָԭٶ pathTween.timeScale = 1f; // ָԭٶ });