213 lines
6.0 KiB
C#
213 lines
6.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using AdamThinkDevicesData;
|
|
using AdamSync;
|
|
|
|
/// <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
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
equipmentCommon = GetComponent<EquipmentCommon>();
|
|
// 订阅布尔值变化事件
|
|
OnActivationChanged += OnActivationChangedHandler;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
#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;
|
|
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); // 检索范围内的所有碰撞体
|
|
|
|
foreach (Collider col in colliders)
|
|
{
|
|
if (col.transform.tag == "WRJ")
|
|
{
|
|
//Debug.Log("检测到无人机: " + col.name);
|
|
UnmannedAerialVehicle unmannedAerialVehicle = col.GetComponent<UnmannedAerialVehicle>();
|
|
if (unmannedAerialVehicle)
|
|
{
|
|
//Debug.Log(col.name+"数据链通信频点...:" + unmannedAerialVehicle.dataLinkCommunicationFrequency);
|
|
if (unmannedAerialVehicle.dataLinkCommunicationFrequency == "" || InterferingFrequency == "") return;//无数据不执行
|
|
if(unmannedAerialVehicle.dataLinkCommunicationFrequency== InterferingFrequency)
|
|
{
|
|
Debug.Log("干扰...:"+ col.name + "成功。");
|
|
unmannedAerialVehicle.BeAssaulted("无线电干扰");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void OnDestroy()
|
|
{
|
|
OnActivationChanged -= OnActivationChangedHandler;
|
|
}
|
|
}
|