using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.Linq;
///
/// 无人机
///
public class UnmannedAerialVehicle : MonoBehaviour
{
///
/// 所属无人机蜂群
///
public UnmannedAerialVehicleManage unmannedAerialVehicleManage;
#region 无人机数据
///
/// 续航时间
///
public string batteryLife;
///
/// 抗风等级
///
public string classificationWindResistance;
///
/// 最大飞行速度
///
public string maximumFlyingSpeed;
///
/// RCS
///
public string RCS;
///
/// 卫星定位频点
///
public string satellitePositioningFrequency;
///
/// 数据链通信频点
///
public string dataLinkCommunicationFrequency;
///
/// 电子侦察能力
///
public string electronicReconnaissanceCapability;
///
/// 光学侦察能力
///
public string opticalReconnaissanceCapability;
#endregion
///
/// 飞行速度
///
public float FireSpeed = 20.0f;
///
/// 检测范围半径
///
public float detectionRadius = 50; //
///
/// 是否正在攻击目标
///
private bool isEngagedTarget = false;
///
/// 爆炸预制体
///
public GameObject explodePrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (unmannedAerialVehicleManage && unmannedAerialVehicleManage.isStartRehearsing)
{
switch (unmannedAerialVehicleManage.pattern)
{
case Pattern.待机:
break;
case Pattern.警戒:
AttackATarget();
break;
case Pattern.攻击:
AttackATarget();
break;
}
}
}
///
/// 攻击目标
///
public void AttackATarget()
{
if (!isEngagedTarget)
{
List colliders = Physics.OverlapSphere(transform.position, detectionRadius).ToList(); // 检索范围内的所有碰撞体
List colliders1 = colliders.FindAll(x => x.transform.tag == "AttackTarget");
if (colliders1.Count>0)
{
int _number= Random.Range(0, colliders1.Count-1);
isEngagedTarget = true;
Debug.Log(colliders1[_number].transform.name);
AttAck(colliders1[_number].transform);
}
}
}
///
/// 发起攻击
///
private void AttAck(Transform target)
{
Debug.Log("开始攻击目标..:" + target.name);
Vector3 _v3 = target.position;
transform.DOLookAt(_v3, 0.1f).OnComplete(() => {
float distance = Vector3.Distance(transform.position, target.position);
transform.DOMove(target.position, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(()=> {
if (!target)
{
BeAssaulted("没有攻击到目标");
}
});
});
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "AttackTarget")
{
// 销毁objectToDestroy对象
BeAssaulted("攻击到目标");
GameObject Bao = Instantiate(explodePrefab, other.transform);
Bao.transform.localPosition = Vector3.zero;
Bao.transform.SetParent(null);
Bao.SetActive(true);
Destroy(other.gameObject);
}
}
///
/// 被攻击
///
///
public void BeAssaulted(string type)
{
switch (type) {
case "激光打击":
GameObject Bao1 = Instantiate(explodePrefab, transform);
Bao1.transform.localPosition = Vector3.zero;
Bao1.transform.SetParent(null);
Bao1.SetActive(true);
DistroyThis();
Debug.Log(transform.name+"被激光打击销毁了");
break;
case "无线电干扰":
Vector3 _pos = transform.position - new Vector3(0, 30, 0);
transform.LookAt(_pos);
transform.DOMove(_pos, 1).OnComplete(() => {
GameObject Bao2 = Instantiate(explodePrefab, transform);
Bao2.transform.localPosition = Vector3.zero;
Bao2.transform.SetParent(null);
Bao2.SetActive(true);
DistroyThis();
});
Debug.Log(transform.name + "无人机被无线电干扰销毁了");
break;
case "攻击到目标":
GameObject Bao3 = Instantiate(explodePrefab, transform);
Bao3.transform.localPosition = Vector3.zero;
Bao3.transform.SetParent(null);
Bao3.SetActive(true);
DistroyThis();
Debug.Log(transform.name + "无人机自杀式攻击销毁了");
break;
case "没有攻击到目标":
GameObject Bao4 = Instantiate(explodePrefab, transform);
Bao4.transform.localPosition = Vector3.zero;
Bao4.transform.SetParent(null);
Bao4.SetActive(true);
DistroyThis();
Debug.Log(transform.name + "无人机自杀式销毁了");
break;
default:
break;
}
}
void DistroyThis()
{
Destroy(gameObject);
}
void OnDestroy()
{
transform.DOKill();
//
}
}