287 lines
9.1 KiB
C#
287 lines
9.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using AdamThinkDevicesData;
|
|
using AdamSync;
|
|
using DG.Tweening;
|
|
using Newtonsoft.Json;
|
|
using static InterfaceManager;
|
|
|
|
/// <summary>
|
|
/// 地面无线电干扰控制
|
|
/// </summary>
|
|
public class TerrestrialRadioInterferenceManger : MonoBehaviour
|
|
{
|
|
|
|
public EquipmentCommon equipmentCommon;
|
|
|
|
#region 地面无线电干扰数据
|
|
/// <summary>
|
|
/// 干扰频率
|
|
/// </summary>
|
|
public string InterferingFrequency;
|
|
/// <summary>
|
|
/// 干扰模式
|
|
/// </summary>
|
|
public string InterferenceMode;
|
|
/// <summary>
|
|
/// 发射功率
|
|
/// </summary>
|
|
public string TransmittedPower;
|
|
/// <summary>
|
|
/// 干扰角度
|
|
/// </summary>
|
|
public string InterferenceAngle;
|
|
/// <summary>
|
|
/// 干扰距离
|
|
/// </summary>
|
|
public string InterferenceDistance;
|
|
|
|
#endregion
|
|
|
|
public float detectionRadius = 5f; // 检测范围半径
|
|
|
|
#region 启动暂停
|
|
private bool _isStartRehearsing = false;
|
|
/// <summary>
|
|
/// 是否正在预演
|
|
/// </summary>
|
|
public bool isStartRehearsing
|
|
{
|
|
get { return _isStartRehearsing; }
|
|
set
|
|
{
|
|
if (_isStartRehearsing != value)
|
|
{
|
|
_isStartRehearsing = value;
|
|
OnActivationChanged?.Invoke(_isStartRehearsing);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 布尔值变化时触发的事件
|
|
/// </summary>
|
|
public event System.Action<bool> OnActivationChanged;
|
|
/// <summary>
|
|
/// 协程对象
|
|
/// </summary>
|
|
private Coroutine timerCoroutine;
|
|
/// <summary>
|
|
/// 定时器运行状态
|
|
/// </summary>
|
|
private bool isTimerRunning = false;
|
|
/// <summary>
|
|
/// 间隔时间
|
|
/// </summary>
|
|
public float interval = 5.0f;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 爆炸预制体
|
|
/// </summary>
|
|
public GameObject explodePrefab;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
equipmentCommon = GetComponent<EquipmentCommon>();
|
|
// 订阅布尔值变化事件
|
|
OnActivationChanged += OnActivationChangedHandler;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (unmannedAerialVehicles.Count > 0 && isDo)
|
|
{
|
|
isDo = false;
|
|
UnmannedAerialVehicle unmannedAerialVehicle = unmannedAerialVehicles.Dequeue();
|
|
Vector3 _pos = unmannedAerialVehicle.transform.position - new Vector3(0, 50, 0);
|
|
unmannedAerialVehicle.transform.LookAt(_pos);
|
|
unmannedAerialVehicle.transform.DOMove(_pos, 1).OnComplete(() =>
|
|
{
|
|
AddBao(unmannedAerialVehicle);
|
|
});
|
|
}
|
|
}
|
|
#region 启动暂停
|
|
/// <summary>
|
|
/// 导条变化调用
|
|
/// </summary>
|
|
/// <param name="newValue"></param>
|
|
void OnActivationChangedHandler(bool newValue)
|
|
{
|
|
if (newValue)
|
|
{
|
|
StartTimer();
|
|
}
|
|
else
|
|
{
|
|
StopTimer();
|
|
}
|
|
}
|
|
|
|
|
|
IEnumerator Timer()
|
|
{
|
|
while (true)
|
|
{
|
|
//Debug.Log("Timer fired at: " + Time.time);
|
|
yield return new WaitForSeconds(interval); // 等待一段时间后继续执行
|
|
RadioDisturbance();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启
|
|
/// </summary>
|
|
public void StartTimer()
|
|
{
|
|
if (equipmentCommon.isPlayer && timerCoroutine == null)
|
|
{
|
|
timerCoroutine = StartCoroutine(Timer());
|
|
isTimerRunning = true;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 停止
|
|
/// </summary>
|
|
public void StopTimer()
|
|
{
|
|
if (equipmentCommon.isPlayer && timerCoroutine != null)
|
|
{
|
|
StopCoroutine(timerCoroutine);
|
|
timerCoroutine = null;
|
|
isTimerRunning = false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 数据写入
|
|
/// </summary>
|
|
/// <param name="weaponitemone"></param>
|
|
public void FillInTheData(List<List_paraItem> weaponitemone)
|
|
{
|
|
//if (equipmentCommon)
|
|
//{
|
|
//string msg = $"send2room {equipmentCommon.equipmentType}+{transform.position.ToString().Replace(" ", "").Replace("(", "").Replace(")", "")}+{transform.eulerAngles.ToString().Replace(" ", "").Replace("(", "").Replace(")", "")}";
|
|
//Debug.Log(msg);
|
|
//_ = SyncCreateRoom.SendMessageAsync(msg);
|
|
//}
|
|
for (int i = 0; i < weaponitemone.Count; i++)
|
|
{
|
|
switch (weaponitemone[i].para_name)
|
|
{
|
|
case "干扰频率:":
|
|
InterferingFrequency = weaponitemone[i].para_value;
|
|
interval= float.Parse(InterferingFrequency);
|
|
break;
|
|
case "干扰模式:":
|
|
InterferenceMode = weaponitemone[i].para_value;
|
|
Debug.LogError(InterferenceMode);
|
|
break;
|
|
case "发射功率:":
|
|
TransmittedPower = weaponitemone[i].para_value;
|
|
break;
|
|
case "干扰角度:":
|
|
InterferenceAngle = weaponitemone[i].para_value;
|
|
break;
|
|
case "干扰距离:":
|
|
InterferenceDistance = weaponitemone[i].para_value;
|
|
detectionRadius = float.Parse(InterferenceDistance);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启无线电干扰
|
|
/// </summary>
|
|
public void RadioDisturbance()
|
|
{
|
|
Collider[] colliders = Physics.OverlapSphere(transform.position, detectionRadius); // 检索范围内的所有碰撞体
|
|
|
|
for (int i = 0; i < colliders.Length; i++)
|
|
{
|
|
if (colliders[i].transform.tag == "WRJ")
|
|
{
|
|
//Debug.Log("检测到无人机: " + col.name);
|
|
UnmannedAerialVehicle unmannedAerialVehicle = colliders[i].GetComponent<UnmannedAerialVehicle>();
|
|
if (unmannedAerialVehicle)
|
|
{
|
|
//Debug.Log(col.name+"数据链通信频点...:" + unmannedAerialVehicle.dataLinkCommunicationFrequency);
|
|
if (unmannedAerialVehicle.dataLinkCommunicationFrequency == "" || InterferingFrequency == "") continue;//无数据不执行
|
|
if(unmannedAerialVehicle.dataLinkCommunicationFrequency== InterferingFrequency)
|
|
{
|
|
Debug.Log("干扰...:"+ colliders[i].name + "成功。");
|
|
//unmannedAerialVehicle.BeAssaulted("无线电干扰");
|
|
unmannedAerialVehicles.Enqueue(unmannedAerialVehicle);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Queue<UnmannedAerialVehicle> unmannedAerialVehicles = new Queue<UnmannedAerialVehicle>();
|
|
|
|
private bool isDo = true;
|
|
/// <summary>
|
|
/// 销毁单体无人机
|
|
/// </summary>
|
|
public void AddBao(UnmannedAerialVehicle unmannedAerialVehicle)
|
|
{
|
|
string nowData = GetSyncDis(unmannedAerialVehicle);
|
|
Debug.Log(nowData);
|
|
DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
string currentTime = System.DateTime.Now.ToString();
|
|
string _log = currentTime + " " + equipmentCommon.equipmentType + "(" + equipmentCommon.deviceID + ")"+
|
|
"攻击了销毁了"+ unmannedAerialVehicle.unmannedAerialVehicleManage.equipmentCommon.equipmentType + "(" + unmannedAerialVehicle.unmannedAerialVehicleManage.equipmentCommon.deviceID + ")"
|
|
+ "编号" + unmannedAerialVehicle.serialNumber + "子无人机";
|
|
UploadLog(_log);
|
|
GameObject Bao = Instantiate(explodePrefab, unmannedAerialVehicle.transform);
|
|
Bao.transform.localPosition = Vector3.zero;
|
|
Bao.transform.SetParent(null);
|
|
Bao.SetActive(true);
|
|
Destroy(unmannedAerialVehicle.gameObject);
|
|
isDo = true;
|
|
}
|
|
|
|
/// <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;
|
|
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);
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单个无人机被销毁
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected string GetSyncDis(UnmannedAerialVehicle unmannedAerialVehicle)
|
|
{
|
|
return string.Format("{0},{1},{2}", "DroneWasDestroyed", unmannedAerialVehicle.unmannedAerialVehicleManage.equipmentCommon.deviceID, unmannedAerialVehicle.serialNumber);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
OnActivationChanged -= OnActivationChangedHandler;
|
|
}
|
|
}
|