diff --git a/Role/Attack.cs b/Role/Attack.cs index d248cc1..dfadf72 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -5,14 +5,20 @@ using System.Threading.Tasks; using UnityEngine; using Debug = UnityEngine.Debug; - 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("瀛愬脊棰勫埗浣")] public GameObject bulletPrefab; + [Header("瀛愬脊鏁版嵁")] public BulletData bulletData; + [Header("瑙掕壊瀵硅薄")] public Role role; + [Header("鏀诲嚮鑼冨洿")] public float attackScope; + [Header("鏀诲嚮绫诲瀷")] public DamageType damageTyp = DamageType.noAttributeDamage; + + [Header("鏀诲嚮鍐峰嵈鏃堕棿锛堢锛")] public float attackCooldown = 1f; // 鏀诲嚮鍐峰嵈鏃堕棿 + private float lastAttackTime = 0f; // 涓婃鏀诲嚮鐨勬椂闂 + + [HideInInspector] + public List bulltes = new List(); + public float AttackScope { [DebuggerStepThrough] @@ -22,74 +28,87 @@ public class Attack : MonoBehaviour { attackScope = value; GetComponent().radius = attackScope; - //Debug.Log("攻击半径"+GetComponent().radius); + //Debug.Log("鏀诲嚮鍗婂緞"+GetComponent().radius); } } + public async void Start() { AttackScope = attackScope; - //AttackScope = 5; bulletData = new BulletData(); bulletData.BulletScattering = 1; CircleCollider2D circleCollider2D = GetComponent(); - while (true) { - //Debug.Log(circleCollider2D.radius); + while (true) + { if (circleCollider2D) { - - Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope); - //Debug.Log(circleCollider2D.radius); - foreach (Collider2D collider in colliders) + // 妫鏌ユ槸鍚﹀彲浠ヨ繘琛屾敾鍑伙紙鍩轰簬鍐峰嵈鏃堕棿锛 + if (Time.time - lastAttackTime >= attackCooldown) { - - Role targetRole = collider.GetComponent(); - if (targetRole && targetRole.camp != role.camp) + Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope); + + foreach (Collider2D collider in colliders) { - Debug.Log("检测到碰撞器:" + collider.name); - attack(targetRole); + Role targetRole = collider.GetComponent(); + if (targetRole && targetRole.camp != role.camp) + { + Debug.Log("妫娴嬪埌纰版挒鍣: " + collider.name); + attack(targetRole); + lastAttackTime = Time.time; // 鏇存柊涓婃鏀诲嚮鏃堕棿 + break; // 鍙敾鍑讳竴涓洰鏍 + } } } - await Task.Delay((int)(1000)); + // Debug.Log("=====--"); + await Task.Delay(100); // 寤惰繜锛岄伩鍏嶈繃浜庨绻佸湴妫娴 } else { return; } - } } + public void attack(Role targetRole) { - - - if (bulletPrefab==null) + if (bulletPrefab == null) { - Debug.LogError("子弹预制体为空"); + Debug.LogError("瀛愬脊棰勫埗浣撲负绌"); return; } - if (bulletPrefab.GetComponent().myBulletType == BulletType.Spraying) - { - GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root); - return; - } - - Vector2 direction = (targetRole.transform.position - transform.position).normalized; - List bulltes= new List(); - for (int i = 0; i < bulletData.BulletScattering; i++) - { - //改变子弹方向,更具角色自身的攻击范围来计算, + 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.GetComponent().attackObj = this; + // BulletGamobj.transform.up = direction; BulletGamobj.transform.position = transform.position; 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); + } + + } + } + + private void OnDisable() + { + bulltes.Clear(); } - } diff --git a/Role/Bullet.cs b/Role/Bullet.cs index 35b7801..52e8f93 100644 --- a/Role/Bullet.cs +++ b/Role/Bullet.cs @@ -80,6 +80,8 @@ public class Bullet : MonoBehaviour timer += Time.deltaTime; if (timer > BulletDeadTimer) { + attackObj.bulltes.Remove(this.gameObject); + Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count); Destroy(this.gameObject); } break; @@ -101,10 +103,33 @@ public class Bullet : MonoBehaviour { if(Crole.camp!= role.camp) { - Debug.Log("进行攻击计算"); + Debug.Log(this.role.gameObject.name+"进行攻击计算"); Crole.bloodLoss(new object[] { Crole, role.Attack + bulletData.attack, attackObj.damageTyp, role }); - Destroy(this.gameObject); + attackObj.bulltes.Remove(this.gameObject); + + if (myBulletType!=BulletType.Spraying) + { + attackObj.bulltes.Remove(this.gameObject); + Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count); + Destroy(this.gameObject); + } } } } + + private void OnTriggerExit2D(Collider2D collision) + { + Debug.Log("进入检测范围"); + Role Crole = collision.gameObject.GetComponent(); + if (Crole) + { + if (Crole.camp != role.camp) + { + attackObj.bulltes.Remove(this.gameObject); + Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count); + Destroy(this.gameObject); + } + } + } + }