2024-12-02 09:37:47 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2024-12-02 23:38:32 +08:00
|
|
|
|
public class Attack : Palye
|
2024-12-02 09:37:47 +08:00
|
|
|
|
{
|
|
|
|
|
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>")]
|
|
|
|
|
public GameObject bullet;
|
|
|
|
|
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
|
|
|
|
|
public float attackInterval = 1f;
|
|
|
|
|
[Header("<22><><EFBFBD><EFBFBD>Tag")]
|
|
|
|
|
public string enemyTag = "Enemy";
|
|
|
|
|
[Header("paotaInfo<66>Ľű<C4BD>")]
|
|
|
|
|
public paotaInfo towerInfo; // <20><><EFBFBD><EFBFBD> paotaInfo <20>ű<EFBFBD>
|
|
|
|
|
[Header("<22><>Ҫһ<D2AA><D2BB>canvas<61><73><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>")]
|
|
|
|
|
public Canvas canvas;
|
|
|
|
|
[Header("<22>ӵ<EFBFBD><D3B5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>ٶ<EFBFBD>")]
|
|
|
|
|
public float bulletSpeed;
|
|
|
|
|
private float attackTimer = 0f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
if (towerInfo == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("δ<>ҵ<EFBFBD> paotaInfo <20>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
if (!transform.parent.CompareTag("paotai"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attackTimer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
if (attackTimer >= attackInterval)
|
|
|
|
|
{
|
|
|
|
|
attackTimer = 0f;
|
|
|
|
|
CheckAndAttack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckAndAttack()
|
|
|
|
|
{
|
|
|
|
|
if (towerInfo == null) return;
|
|
|
|
|
|
|
|
|
|
// ʹ<><CAB9> CircleCollider2D <20>ķ<EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, towerInfo._CircleCollider2D.radius);
|
|
|
|
|
|
|
|
|
|
foreach (var hit in hits)
|
|
|
|
|
{
|
|
|
|
|
if (hit.CompareTag(enemyTag))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><E2B5BD><EFBFBD><EFBFBD>");
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><E2B5BD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|
|
|
|
Shoot(hit.transform);
|
|
|
|
|
return; // ֻ<><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ŀ<EFBFBD><C4BF>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Shoot(Transform target)
|
|
|
|
|
{
|
|
|
|
|
if (bullet != null)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ʵ<EFBFBD><CAB5>
|
|
|
|
|
GameObject newBullet = Instantiate(bullet, canvas.transform);
|
|
|
|
|
newBullet.transform.position = transform.position;
|
|
|
|
|
newBullet.GetComponent<bullet>().Init(target.gameObject, bulletSpeed);
|
2024-12-02 23:38:32 +08:00
|
|
|
|
newBullet.GetComponent<bullet>().user = this;
|
2024-12-02 09:37:47 +08:00
|
|
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>" + target.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDrawGizmos()
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7> paotaInfo <20>ѹ<EFBFBD><D1B9><EFBFBD>
|
|
|
|
|
if (towerInfo != null)
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, towerInfo._CircleCollider2D.radius);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|