UnityCommon/Role/Attack.cs

86 lines
2.8 KiB
C#
Raw Normal View History

2024-12-05 16:25:46 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class Attack : MonoBehaviour
{
[Header("<22>ӵ<EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>")] public GameObject bulletPrefab;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>")] public BulletData bulletData;
[Header("<22><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>")] public Role role;
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ")] public float attackScope;
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public DamageType damageTyp = DamageType.noAttributeDamage;
public float AttackScope
{
[DebuggerStepThrough]
get => attackScope;
[DebuggerStepThrough]
set
{
attackScope = value;
GetComponent<CircleCollider2D>().radius = attackScope;
2024-12-11 16:51:55 +08:00
//Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD>뾶"+GetComponent<CircleCollider2D>().radius);
2024-12-05 16:25:46 +08:00
}
}
public async void Start()
{
2024-12-11 16:51:55 +08:00
AttackScope = attackScope;
//AttackScope = 5;
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-05 16:25:46 +08:00
while (true) {
2024-12-11 16:51:55 +08:00
//Debug.Log(circleCollider2D.radius);
2024-12-05 16:25:46 +08:00
if (circleCollider2D)
{
2024-12-11 16:51:55 +08:00
Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope);
//Debug.Log(circleCollider2D.radius);
2024-12-05 16:25:46 +08:00
foreach (Collider2D collider in colliders)
{
2024-12-11 16:51:55 +08:00
2024-12-05 16:25:46 +08:00
Role targetRole = collider.GetComponent<Role>();
if (targetRole && targetRole.camp != role.camp)
{
Debug.Log("<22><><EFBFBD><EFBFBD><E2B5BD>ײ<EFBFBD><D7B2>:" + collider.name);
attack(targetRole);
}
}
await Task.Delay((int)(1000));
}
else
{
return;
}
}
}
public void attack(Role targetRole)
{
if (bulletPrefab==null)
{
Debug.LogError("<22>ӵ<EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>");
return;
}
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
List<GameObject> bulltes= new List<GameObject>();
for (int i = 0; i < bulletData.BulletScattering; i++)
{
//<2F>ı<EFBFBD><C4B1>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>߽<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
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-11 16:51:55 +08:00
}
2024-12-05 16:25:46 +08:00
}
}