UnityCommon/Role/Attack.cs

214 lines
7.0 KiB
C#
Raw Normal View History

2024-12-05 16:25:46 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
2024-12-14 23:20:36 +08:00
using System.Linq;
2024-12-05 16:25:46 +08:00
using System.Threading.Tasks;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class Attack : MonoBehaviour
{
2024-12-12 23:03:57 +08:00
[Header("子弹预制体")] public GameObject bulletPrefab;
[Header("子弹数据")] public BulletData bulletData;
[Header("角色对象")] public Role role;
[Header("攻击范围")] public float attackScope;
[Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage;
2024-12-18 14:16:52 +08:00
[Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间
2024-12-12 23:03:57 +08:00
private float lastAttackTime = 0f; // 上次攻击的时间
[HideInInspector]
public List<GameObject> bulltes = new List<GameObject>();
2024-12-17 17:15:32 +08:00
[Header("角色动画控制器")] public Animator animator;
[Header("火焰动画控制器")] public Animator fireAni;
2024-12-17 18:07:43 +08:00
[Header("子弹起始点")] public Transform BulletStartPos;
public Vector2 direction;
2024-12-05 16:25:46 +08:00
public float AttackScope
{
[DebuggerStepThrough]
get => attackScope;
[DebuggerStepThrough]
set
{
attackScope = value;
GetComponent<CircleCollider2D>().radius = attackScope;
2024-12-12 23:03:57 +08:00
//Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
2024-12-05 16:25:46 +08:00
}
}
2024-12-12 23:03:57 +08:00
2024-12-05 16:25:46 +08:00
public async void Start()
{
2024-12-11 16:51:55 +08:00
AttackScope = attackScope;
2024-12-05 16:25:46 +08:00
bulletData = new BulletData();
bulletData.BulletScattering = 1;
2024-12-11 16:51:55 +08:00
CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
2024-12-12 23:03:57 +08:00
while (true)
{
2024-12-05 16:25:46 +08:00
if (circleCollider2D)
{
2024-12-12 23:03:57 +08:00
// 检查是否可以进行攻击(基于冷却时间)
if (Time.time - lastAttackTime >= attackCooldown)
2024-12-05 16:25:46 +08:00
{
2024-12-12 23:03:57 +08:00
Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope);
foreach (Collider2D collider in colliders)
2024-12-05 16:25:46 +08:00
{
2024-12-12 23:03:57 +08:00
Role targetRole = collider.GetComponent<Role>();
if (targetRole && targetRole.camp != role.camp)
{
2024-12-14 23:20:36 +08:00
role.animationHighlight = 1;
2024-12-17 17:15:32 +08:00
if (animator!=null)
{
2024-12-17 18:07:43 +08:00
if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying)
{
2024-12-18 14:16:52 +08:00
direction = (targetRole.transform.position - BulletStartPos.position).normalized;
2024-12-17 18:07:43 +08:00
}
2024-12-17 17:15:32 +08:00
animator.SetInteger("State", 1);
lastAttackTime = Time.time; // 更新上次攻击时间
2024-12-17 18:07:43 +08:00
2024-12-17 17:15:32 +08:00
}
else
{
attack(targetRole);
lastAttackTime = Time.time; // 更新上次攻击时间
}
2024-12-12 23:03:57 +08:00
break; // 只攻击一个目标
}
2024-12-14 23:20:36 +08:00
else
{
2024-12-17 17:15:32 +08:00
if (animator != null)
{
animator.SetInteger("State", 0);
}
else
{
role.animationHighlight = 0;
}
2024-12-14 23:20:36 +08:00
}
2024-12-05 16:25:46 +08:00
}
2024-12-14 23:20:36 +08:00
2024-12-05 16:25:46 +08:00
}
2024-12-12 23:03:57 +08:00
await Task.Delay(100); // 延迟,避免过于频繁地检测
2024-12-05 16:25:46 +08:00
}
else
{
return;
}
}
}
2024-12-12 23:03:57 +08:00
2024-12-05 16:25:46 +08:00
public void attack(Role targetRole)
{
2024-12-14 23:20:36 +08:00
2024-12-12 23:03:57 +08:00
if (bulletPrefab == null)
2024-12-05 16:25:46 +08:00
{
2024-12-12 23:03:57 +08:00
Debug.LogError("子弹预制体为空");
2024-12-05 16:25:46 +08:00
return;
}
2024-12-12 17:06:08 +08:00
2024-12-14 23:20:36 +08:00
2024-12-05 16:25:46 +08:00
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
2024-12-12 23:03:57 +08:00
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
{
2024-12-05 16:25:46 +08:00
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
BulletGamobj.GetComponent<Bullet>().role = role;
2024-12-12 23:03:57 +08:00
BulletGamobj.GetComponent<Bullet>().attackObj = this;
// BulletGamobj.transform.up = direction;
2024-12-17 17:15:32 +08:00
BulletGamobj.transform.position =new Vector2(transform.position.x+0.15f, transform.position.y+0.2f);
2024-12-05 16:25:46 +08:00
bulltes.Add(BulletGamobj);
2024-12-12 23:03:57 +08:00
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);
}
}
}
2024-12-17 18:07:43 +08:00
public void Pointattack()
{
if (bulletPrefab == null)
{
Debug.LogError("子弹预制体为空");
return;
}
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;
2024-12-18 14:16:52 +08:00
BulletGamobj.transform.position = BulletStartPos.position;
2024-12-17 18:07:43 +08:00
bulltes.Add(BulletGamobj);
}
}
2024-12-12 23:03:57 +08:00
2024-12-17 17:15:32 +08:00
public void Fireattack()
{
if (bulletPrefab == null)
{
Debug.LogError("子弹预制体为空");
return;
}
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
{
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
fireAni = BulletGamobj.GetComponentInChildren<Animator>();
BulletGamobj.GetComponent<Bullet>().role = role;
BulletGamobj.GetComponent<Bullet>().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;
}
}
public void EndFire()
{
fireAni.SetInteger("State", 1);
Debug.Log("停止火焰");
}
2024-12-12 23:03:57 +08:00
private void OnDisable()
{
bulltes.Clear();
2024-12-05 16:25:46 +08:00
}
}