355 lines
11 KiB
C#
355 lines
11 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using System.Linq;
|
|
using AdamSync;
|
|
using static InterfaceManager;
|
|
using AdamThinkDevicesData;
|
|
using Newtonsoft.Json;
|
|
|
|
/// <summary>
|
|
/// 无人机
|
|
/// </summary>
|
|
public class UnmannedAerialVehicle : MonoBehaviour
|
|
{
|
|
public WRJModel wrjModel = WRJModel.无人机;
|
|
/// <summary>
|
|
/// 所属无人机蜂群
|
|
/// </summary>
|
|
public UnmannedAerialVehicleManage unmannedAerialVehicleManage;
|
|
/// <summary>
|
|
/// 序列编号
|
|
/// </summary>
|
|
public string serialNumber = "";
|
|
|
|
#region 无人机数据
|
|
/// <summary>
|
|
/// 续航时间
|
|
/// </summary>
|
|
public string batteryLife;
|
|
/// <summary>
|
|
/// 抗风等级
|
|
/// </summary>
|
|
public string classificationWindResistance;
|
|
/// <summary>
|
|
/// 最大飞行速度
|
|
/// </summary>
|
|
public string maximumFlyingSpeed;
|
|
/// <summary>
|
|
/// RCS
|
|
/// </summary>
|
|
public string RCS;
|
|
/// <summary>
|
|
/// 卫星定位频点
|
|
/// </summary>
|
|
public string satellitePositioningFrequency;
|
|
/// <summary>
|
|
/// 数据链通信频点
|
|
/// </summary>
|
|
public string dataLinkCommunicationFrequency;
|
|
/// <summary>
|
|
/// 电子侦察能力
|
|
/// </summary>
|
|
public string electronicReconnaissanceCapability;
|
|
/// <summary>
|
|
/// 光学侦察能力
|
|
/// </summary>
|
|
public string opticalReconnaissanceCapability;
|
|
#endregion
|
|
/// <summary>
|
|
/// 飞行速度
|
|
/// </summary>
|
|
public float FireSpeed = 20.0f;
|
|
/// <summary>
|
|
/// 检测范围半径
|
|
/// </summary>
|
|
public float detectionRadius = 50; //
|
|
|
|
/// <summary>
|
|
/// 爆炸预制体
|
|
/// </summary>
|
|
public GameObject explodePrefab;
|
|
|
|
public TextMesh tips;
|
|
/// <summary>
|
|
/// 攻击目标
|
|
/// </summary>
|
|
public Transform attackTarget;
|
|
|
|
/// <summary>
|
|
/// 炮弹
|
|
/// </summary>
|
|
public GameObject bulletPrefab;
|
|
|
|
public Transform bulletPoint;
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (unmannedAerialVehicleManage.equipmentCommon.isPlayer && other.tag == "AttackTarget")
|
|
{
|
|
AddBao(other.transform);
|
|
// 销毁objectToDestroy对象
|
|
BeAssaulted("攻击到目标");
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 被攻击
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
public void BeAssaulted(string type)
|
|
{
|
|
AddBao(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 销毁单体无人机
|
|
/// </summary>
|
|
public void AddBao(bool isPassMessage)
|
|
{
|
|
|
|
if (isPassMessage)
|
|
{
|
|
string nowData = GetSyncDis();
|
|
MyNetMQClient.instance.Send(nowData);
|
|
//Debug.Log(nowData);
|
|
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
|
//DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
//MQTTManager.instance.SendData(MQTTManager.instance.DroneWasDestroyed, nowData);
|
|
string currentTime = System.DateTime.Now.ToString();
|
|
string _log = currentTime + " " + unmannedAerialVehicleManage.equipmentCommon.equipmentType + "(" + unmannedAerialVehicleManage.equipmentCommon.deviceID + ")"
|
|
+ "编号" + serialNumber + "子无人机" + "自杀式攻击自爆了";
|
|
UploadLog(_log);
|
|
}
|
|
if(explodePrefab&&transform)
|
|
{
|
|
GameObject Bao = Instantiate(explodePrefab, transform);
|
|
Bao.transform.localPosition = Vector3.zero;
|
|
Bao.transform.SetParent(null);
|
|
Bao.SetActive(true);
|
|
}
|
|
//DroneViewDisplay.Instance.DistroyUI(unmannedAerialVehicleManage.equipmentCommon.deviceID);
|
|
DeviceManager.Instance.RemoveDevice(gameObject);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 销毁蓝方设备
|
|
/// </summary>
|
|
/// <param name="_transform"></param>
|
|
void AddBao(Transform _transform)
|
|
{
|
|
//设备
|
|
EquipmentCommon _equipmentCommon = _transform.GetComponent<EquipmentCommon>();
|
|
if (_equipmentCommon)
|
|
{
|
|
string nowData = string.Format("{0},{1}", "SetToBeDestroyed", _equipmentCommon.deviceID);
|
|
MyNetMQClient.instance.Send(nowData);
|
|
//Debug.Log(nowData);
|
|
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
|
//DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
//MQTTManager.instance.SendData(MQTTManager.instance.SetToBeDestroyed, nowData);
|
|
string currentTime = System.DateTime.Now.ToString();
|
|
string _log = currentTime + " " + unmannedAerialVehicleManage.equipmentCommon.equipmentType + "(" + unmannedAerialVehicleManage.equipmentCommon.deviceID + ")"
|
|
+ "编号" + serialNumber + "子无人机" + "攻击摧毁了" + _equipmentCommon.equipmentType + "(" + _equipmentCommon.deviceID + ")";
|
|
UploadLog(_log);
|
|
|
|
WWWForm headers = new WWWForm();
|
|
headers.AddField("id", _equipmentCommon.deviceID);
|
|
StartCoroutine(PostString(Url_Deletepracticedevicedetail, headers, data =>
|
|
{
|
|
//Debug.Log(data);
|
|
}));
|
|
}
|
|
//重点打击目标
|
|
HighPriorityTarget highPriorityTarget = _transform.GetComponent<HighPriorityTarget>();
|
|
if (highPriorityTarget)
|
|
{
|
|
highPriorityTarget.BeAssaulted(atkPos, true);
|
|
}
|
|
|
|
GameObject Bao = Instantiate(explodePrefab, _transform);
|
|
Bao.transform.localPosition = Vector3.zero;
|
|
Bao.transform.SetParent(null);
|
|
Bao.SetActive(true);
|
|
if (_equipmentCommon)
|
|
{
|
|
DeviceManager.Instance.RemoveDevice(_transform.gameObject);
|
|
Destroy(_transform.gameObject);
|
|
}
|
|
}
|
|
/// <summary>
|
|
///上传日志
|
|
/// </summary>
|
|
/// <param name="deviceID"></param>
|
|
public void UploadLog(string _log)
|
|
{
|
|
|
|
List<UploadLogMain> uploadLogMains = new List<UploadLogMain>();
|
|
UploadLogMain uploadLogMain = new UploadLogMain();
|
|
uploadLogMain.PracticeId = GlobalFlag.practiceSubjectID;
|
|
uploadLogMain.ThinkId = GlobalFlag.currentThinkId;
|
|
string log =
|
|
uploadLogMain.log = _log;
|
|
uploadLogMains.Add(uploadLogMain);
|
|
string uploadLogMainJson = JsonConvert.SerializeObject(uploadLogMains);
|
|
WWWForm wWWForm = new WWWForm();
|
|
wWWForm.AddField("data", uploadLogMainJson);
|
|
//Debug.Log(uploadLogMainJson);
|
|
StartCoroutine(PostString(Url_Addpracticelog, wWWForm, data =>
|
|
{
|
|
Debug.Log(data);
|
|
}));
|
|
}
|
|
public LayerMask layerMask = new LayerMask();
|
|
public void CheckSatellitePositioningFrequency(
|
|
string interferenceMode)
|
|
{
|
|
|
|
if (interferenceMode == "驱离")
|
|
{
|
|
transform.DOKill();
|
|
transform.LookAt(new Vector3(-500, 160, 1600));
|
|
transform.DOMove(new Vector3(-500, 160, 1600), 60);
|
|
}
|
|
else if (interferenceMode == "迫降")
|
|
{
|
|
transform.DOKill();
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity, layerMask))
|
|
{
|
|
if (hit.distance > 1f)
|
|
{
|
|
hit.point = new Vector3(hit.point.x, hit.point.y + 3, hit.point.z);
|
|
transform.DOMove(hit.point, 6);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void OnBecameInvisible()
|
|
{
|
|
Debug.Log("测试");
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
attackTarget = null;
|
|
StopCoroutine(RangedAttack());
|
|
transform.DOKill();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起攻击
|
|
/// </summary>
|
|
public void AttAck(Transform target)
|
|
{
|
|
if (attackTarget == null)
|
|
{
|
|
transform.DOKill();
|
|
attackTarget = target;
|
|
//Debug.Log("开始攻击目标..:" + attackTarget.name);
|
|
Vector3 _v3 = attackTarget.position;
|
|
transform.DOLookAt(_v3, 0.1f).OnComplete(() =>
|
|
{
|
|
if (wrjModel == WRJModel.光学无人机)
|
|
{
|
|
Debug.Log("光学无人机攻击");
|
|
StopCoroutine(RangedAttack());
|
|
StartCoroutine(RangedAttack());
|
|
}
|
|
else
|
|
{
|
|
string nowData = GetSyncData(attackTarget);
|
|
Debug.Log(nowData);
|
|
MyNetMQClient.instance.Send(nowData);
|
|
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
|
//DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
//MQTTManager.instance.SendData(MQTTManager.instance.SingleDronePosition, nowData);
|
|
float distance = Vector3.Distance(transform.position, attackTarget.position);
|
|
transform.DOMove(attackTarget.position, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
|
|
{
|
|
//if (!attackTarget)
|
|
//{
|
|
// //BeAssaulted("没有攻击到目标");
|
|
//}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
Vector3 atkPos = new Vector3();
|
|
|
|
/// <summary>
|
|
/// 远程攻击
|
|
/// </summary>
|
|
public IEnumerator RangedAttack()
|
|
{
|
|
while (attackTarget)
|
|
{
|
|
Vector3 v3 = attackTarget.position;
|
|
yield return new WaitForSeconds(5f);
|
|
GameObject obj = Instantiate(bulletPrefab, bulletPoint.position, bulletPoint.rotation);
|
|
obj.SetActive(true);
|
|
|
|
obj.transform.DOMove(v3, 2).SetEase(Ease.Linear).OnComplete(() =>
|
|
{
|
|
Destroy(obj);
|
|
});
|
|
ShellBoom sb = obj.AddComponent<ShellBoom>();
|
|
sb.isPlayer = unmannedAerialVehicleManage.equipmentCommon.isPlayer;
|
|
sb.onShellAttack.AddListener(() =>
|
|
{
|
|
atkPos = sb.transform.position;
|
|
AddBao(attackTarget.transform);
|
|
Destroy(sb);
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起攻击
|
|
/// </summary>
|
|
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(() =>
|
|
{
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 单个无人机被销毁
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected string GetSyncDis()
|
|
{
|
|
return string.Format("{0},{1},{2}", "DroneWasDestroyed", unmannedAerialVehicleManage.equipmentCommon.deviceID, serialNumber);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 无人机攻击目标传递
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected string GetSyncData(Transform attackTarget)
|
|
{
|
|
return string.Format("{0},{1},{2},{3},{4}", "SingleDronePosition", unmannedAerialVehicleManage.equipmentCommon.deviceID,
|
|
attackTarget.position.x, attackTarget.position.y, attackTarget.position.z);
|
|
}
|
|
|
|
}
|