攻击
This commit is contained in:
parent
eecb005cf6
commit
f26a7c194d
@ -5,14 +5,20 @@ using System.Threading.Tasks;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Debug = UnityEngine.Debug;
|
using Debug = UnityEngine.Debug;
|
||||||
|
|
||||||
|
|
||||||
public class Attack : MonoBehaviour
|
public class Attack : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("子弹预制体")] public GameObject bulletPrefab;
|
[Header("子弹预制体")] public GameObject bulletPrefab;
|
||||||
[Header("子弹数据")] public BulletData bulletData;
|
[Header("子弹数据")] public BulletData bulletData;
|
||||||
[Header("角色对象")] public Role role;
|
[Header("角色对象")] public Role role;
|
||||||
[Header("攻击范围")] public float attackScope;
|
[Header("攻击范围")] public float attackScope;
|
||||||
[Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage;
|
[Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage;
|
||||||
|
|
||||||
|
[Header("攻击冷却时间(秒)")] public float attackCooldown = 1f; // 攻击冷却时间
|
||||||
|
private float lastAttackTime = 0f; // 上次攻击的时间
|
||||||
|
|
||||||
|
[HideInInspector]
|
||||||
|
public List<GameObject> bulltes = new List<GameObject>();
|
||||||
|
|
||||||
public float AttackScope
|
public float AttackScope
|
||||||
{
|
{
|
||||||
[DebuggerStepThrough]
|
[DebuggerStepThrough]
|
||||||
@ -22,74 +28,87 @@ public class Attack : MonoBehaviour
|
|||||||
{
|
{
|
||||||
attackScope = value;
|
attackScope = value;
|
||||||
GetComponent<CircleCollider2D>().radius = attackScope;
|
GetComponent<CircleCollider2D>().radius = attackScope;
|
||||||
//Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
|
//Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void Start()
|
public async void Start()
|
||||||
{
|
{
|
||||||
AttackScope = attackScope;
|
AttackScope = attackScope;
|
||||||
//AttackScope = 5;
|
|
||||||
bulletData = new BulletData();
|
bulletData = new BulletData();
|
||||||
bulletData.BulletScattering = 1;
|
bulletData.BulletScattering = 1;
|
||||||
|
|
||||||
CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
|
CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
|
||||||
|
|
||||||
while (true) {
|
while (true)
|
||||||
//Debug.Log(circleCollider2D.radius);
|
{
|
||||||
if (circleCollider2D)
|
if (circleCollider2D)
|
||||||
{
|
{
|
||||||
|
// 检查是否可以进行攻击(基于冷却时间)
|
||||||
Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope);
|
if (Time.time - lastAttackTime >= attackCooldown)
|
||||||
//Debug.Log(circleCollider2D.radius);
|
|
||||||
foreach (Collider2D collider in colliders)
|
|
||||||
{
|
{
|
||||||
|
Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope);
|
||||||
|
|
||||||
Role targetRole = collider.GetComponent<Role>();
|
foreach (Collider2D collider in colliders)
|
||||||
if (targetRole && targetRole.camp != role.camp)
|
|
||||||
{
|
{
|
||||||
Debug.Log("检测到碰撞器:" + collider.name);
|
Role targetRole = collider.GetComponent<Role>();
|
||||||
attack(targetRole);
|
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
|
else
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attack(Role targetRole)
|
public void attack(Role targetRole)
|
||||||
{
|
{
|
||||||
|
if (bulletPrefab == null)
|
||||||
|
|
||||||
if (bulletPrefab==null)
|
|
||||||
{
|
{
|
||||||
Debug.LogError("子弹预制体为空");
|
Debug.LogError("子弹预制体为空");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
|
||||||
|
|
||||||
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
|
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
|
||||||
{
|
{
|
||||||
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
|
|
||||||
|
|
||||||
List<GameObject> bulltes= new List<GameObject>();
|
|
||||||
for (int i = 0; i < bulletData.BulletScattering; i++)
|
|
||||||
{
|
|
||||||
//改变子弹方向,更具角色自身的攻击范围来计算,
|
|
||||||
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
||||||
BulletGamobj.GetComponent<Bullet>().role = role;
|
BulletGamobj.GetComponent<Bullet>().role = role;
|
||||||
BulletGamobj.GetComponent<Bullet>().attackObj = this ;
|
BulletGamobj.GetComponent<Bullet>().attackObj = this;
|
||||||
BulletGamobj.transform.up = direction;
|
// BulletGamobj.transform.up = direction;
|
||||||
BulletGamobj.transform.position = transform.position;
|
BulletGamobj.transform.position = transform.position;
|
||||||
bulltes.Add(BulletGamobj);
|
bulltes.Add(BulletGamobj);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i < bulletData.BulletScattering; i++)
|
||||||
|
{
|
||||||
|
// 改变子弹方向,根据角色自身的攻击范围来计算
|
||||||
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
||||||
|
BulletGamobj.GetComponent<Bullet>().role = role;
|
||||||
|
BulletGamobj.GetComponent<Bullet>().attackObj = this;
|
||||||
|
BulletGamobj.transform.up = direction;
|
||||||
|
BulletGamobj.transform.position = transform.position;
|
||||||
|
bulltes.Add(BulletGamobj);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnDisable()
|
||||||
|
{
|
||||||
|
bulltes.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,8 @@ public class Bullet : MonoBehaviour
|
|||||||
timer += Time.deltaTime;
|
timer += Time.deltaTime;
|
||||||
if (timer > BulletDeadTimer)
|
if (timer > BulletDeadTimer)
|
||||||
{
|
{
|
||||||
|
attackObj.bulltes.Remove(this.gameObject);
|
||||||
|
Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count);
|
||||||
Destroy(this.gameObject);
|
Destroy(this.gameObject);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -101,10 +103,33 @@ public class Bullet : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if(Crole.camp!= role.camp)
|
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 });
|
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<Role>();
|
||||||
|
if (Crole)
|
||||||
|
{
|
||||||
|
if (Crole.camp != role.camp)
|
||||||
|
{
|
||||||
|
attackObj.bulltes.Remove(this.gameObject);
|
||||||
|
Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count);
|
||||||
|
Destroy(this.gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user