using System.Collections; using System.Collections.Generic; using UnityEngine; using AdamThinkDevicesData; using AdamSync; /// /// 地面无线电干扰控制 /// public class TerrestrialRadioInterferenceManger : MonoBehaviour { public EquipmentCommon equipmentCommon; #region 地面无线电干扰数据 /// /// 干扰频率 /// public string InterferingFrequency; /// /// 干扰模式 /// public string InterferenceMode; /// /// 发射功率 /// public string TransmittedPower; /// /// 干扰角度 /// public string InterferenceAngle; /// /// 干扰距离 /// public string InterferenceDistance; #endregion public float detectionRadius = 5f; // 检测范围半径 #region 启动暂停 private bool _isStartRehearsing = false; /// /// 是否正在预演 /// public bool isStartRehearsing { get { return _isStartRehearsing; } set { if (_isStartRehearsing != value) { _isStartRehearsing = value; OnActivationChanged?.Invoke(_isStartRehearsing); } } } /// /// 布尔值变化时触发的事件 /// public event System.Action OnActivationChanged; /// /// 协程对象 /// private Coroutine timerCoroutine; /// /// 定时器运行状态 /// private bool isTimerRunning = false; /// /// 间隔时间 /// public float interval = 5.0f; #endregion // Start is called before the first frame update void Start() { equipmentCommon = GetComponent(); // 订阅布尔值变化事件 OnActivationChanged += OnActivationChangedHandler; } // Update is called once per frame void Update() { } #region 启动暂停 /// /// 导条变化调用 /// /// 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(); } } /// /// 开启 /// public void StartTimer() { if (timerCoroutine == null) { timerCoroutine = StartCoroutine(Timer()); isTimerRunning = true; } } /// /// 停止 /// public void StopTimer() { if (timerCoroutine != null) { StopCoroutine(timerCoroutine); timerCoroutine = null; isTimerRunning = false; } } #endregion /// /// 数据写入 /// /// public void FillInTheData(List 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; } } } /// /// 开启无线电干扰 /// 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(); if (unmannedAerialVehicle) { Debug.Log(col.name+"数据链通信频点...:" + unmannedAerialVehicle.dataLinkCommunicationFrequency); if (unmannedAerialVehicle.dataLinkCommunicationFrequency == "" || InterferingFrequency == "") return;//无数据不执行 if(unmannedAerialVehicle.dataLinkCommunicationFrequency== InterferingFrequency) { Debug.Log("干扰...:"+ col.name + "成功。"); unmannedAerialVehicle.BeAssaulted("无线电干扰"); } } } } } }