using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.Linq;
using AdamSync;
///
/// 无人机
///
public class UnmannedAerialVehicle : MonoBehaviour
{
///
/// 所属无人机蜂群
///
public UnmannedAerialVehicleManage unmannedAerialVehicleManage;
///
/// 序列编号
///
public string serialNumber = "";
#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; //
///
/// 是否正在攻击目标
///
public 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.equipmentCommon.isPlayer && 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)
{
colliders1.ForEach(x => Debug.Log(x.transform.name));
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;
SendMsg(target);
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("没有攻击到目标");
}
});
});
}
///
/// 发起攻击
///
public void AttAck(Vector3 target)
{
transform.DOLookAt(target, 0.1f).OnComplete(() =>
{
float distance = Vector3.Distance(transform.position, target);
transform.DOMove(target, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
{
});
});
}
private void OnTriggerEnter(Collider other)
{
if (unmannedAerialVehicleManage.equipmentCommon.isPlayer && other.tag == "AttackTarget")
{
AddBao(other.transform);
// 销毁objectToDestroy对象
BeAssaulted("攻击到目标");
}
}
//[]
//public void AddBeAssaulted()
//{
// BeAssaulted("无线电干扰");
//}
///
/// 被攻击
///
///
public void BeAssaulted(string type)
{
switch (type)
{
case "激光打击":
AddBao(true);
//Debug.Log(transform.name+"被激光打击销毁了");
break;
case "无线电干扰":
Vector3 _pos = transform.position - new Vector3(0, 30, 0);
transform.LookAt(_pos);
transform.DOMove(_pos, 1).OnComplete(() =>
{
AddBao(true);
});
//Debug.Log(transform.name + "无人机被无线电干扰销毁了");
break;
case "攻击到目标":
AddBao(true);
//Debug.Log(transform.name + "无人机自杀式攻击销毁了");
break;
case "没有攻击到目标":
AddBao(true);
//Debug.Log(transform.name + "无人机自杀式没有攻击到目标撞击地面销毁了");
break;
default:
break;
}
}
///
/// 销毁单体无人机
///
public void AddBao(bool isPassMessage)
{
if (isPassMessage)
{
string nowData = GetSyncDis();
Debug.Log(nowData);
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
}
GameObject Bao = Instantiate(explodePrefab, transform);
Bao.transform.localPosition = Vector3.zero;
Bao.transform.SetParent(null);
Bao.SetActive(true);
Destroy(gameObject);
// 获取组件
//Component component = gameObject.GetComponent();
//// 移除组件
//if (component != null)
//{
// Destroy(component);
// transform.localScale = Vector3.zero;
//}
}
///
/// 销毁蓝方设备
///
///
void AddBao(Transform _transform)
{
EquipmentCommon _equipmentCommon = _transform.GetComponent();
string nowData = string.Format("{0},{1}", "SetToBeDestroyed", _equipmentCommon.deviceID);
Debug.Log(nowData);
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
GameObject Bao = Instantiate(explodePrefab, _transform);
Bao.transform.localPosition = Vector3.zero;
Bao.transform.SetParent(null);
Bao.SetActive(true);
Destroy(_transform.gameObject);
}
private void OnBecameInvisible()
{
Debug.Log("测试");
}
void OnDestroy()
{
transform.DOKill();
}
public void SendMsg(Transform attackTarget)
{
var nowData = GetSyncData(attackTarget);
Debug.Log(nowData);
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
}
///
/// 单个无人机攻击目标传递
///
///
protected string GetSyncData(Transform attackTarget)
{
return string.Format("{0},{1},{2},{3},{4},{5}", "SingleDronePosition", unmannedAerialVehicleManage.equipmentCommon.deviceID, serialNumber,
attackTarget.position.x, attackTarget.position.y, attackTarget.position.z);
}
///
/// 单个无人机被销毁
///
///
protected string GetSyncDis()
{
return string.Format("{0},{1},{2}", "DroneWasDestroyed", unmannedAerialVehicleManage.equipmentCommon.deviceID, serialNumber);
}
}