using System.Collections; using System.Collections.Generic; using UnityEngine; using AdamThinkDevicesData; using AdamSync; using System.Linq; /// /// 雷达控制 /// public class RadarManger : MonoBehaviour { public EquipmentCommon equipmentCommon; #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 /// /// 飞机不显示在小地图上 /// public GameObject minimap; /// /// 雷达UI预制体 /// public GameObject RadarUiPrefab; /// /// 雷达UI /// public GameObject RadarUi; /// /// 雷达动画 /// public Animator aniRandar; /// /// 雷达UI动画播放 /// public Animator aniRandarUI; #region 雷达数据 /// /// 转台转速 /// public string TurntableSpeed; /// /// 探测距离 /// public string DetectionRange; /// /// 近盲区 /// public string NearBlindArea; /// /// 批量标处理能力 /// public string BatchStandardProcessingCapability; /// /// 探测成功率 /// public string DetectionSuccessRate; /// /// 最小探测速度 /// public string MinimumDetectionVelocity; /// /// 距离分辨率 /// public string RangeResolution; /// /// 方位分辨率 /// public string AzimuthResolution; /// /// 方位波束宽度 /// public string AzimuthBeamwidth; /// /// 俯仰波束宽度 /// public string PitchBeamwidth; #endregion /// /// 检测范围半径 /// public float detectionRadius = 5f; // /// /// 批量标处理能力 /// public int NumberOfProbes = 0; /// /// 渲染小地图摄像机 /// public Camera Mincamera; void Start() { equipmentCommon = GetComponent(); aniRandar = GetComponent(); Mincamera = GameObject.Find("Minimap Camera").GetComponent(); if (RadarUi == null) { CreateRadarUI(); } // 订阅布尔值变化事件 OnActivationChanged += OnActivationChangedHandler; //InvokeRepeating("RetrievalUAV", 1, 5);//测试用 } // Update is called once per frame void Update() { } #region 启动暂停 /// /// 导条变化调用 /// /// void OnActivationChangedHandler(bool newValue) { if (newValue) { Debug.Log("开始检索"); StartTimer(); } else { Debug.Log("停止检索"); StopTimer(); } } IEnumerator Timer() { while (true) { //Debug.Log("Timer fired at: " + Time.time); yield return new WaitForSeconds(interval); // 等待一段时间后继续执行 RetrievalUAV(); } } /// /// 开启 /// public void StartTimer() { if (equipmentCommon.isPlayer && timerCoroutine == null) { timerCoroutine = StartCoroutine(Timer()); isTimerRunning = true; } } /// /// 停止 /// public void StopTimer() { if (equipmentCommon.isPlayer && timerCoroutine != null) { StopCoroutine(timerCoroutine); timerCoroutine = null; isTimerRunning = false; } } #endregion /// /// 生成雷达UI /// void CreateRadarUI() { Transform canvas = GameObject.Find("Canvas").transform; if (canvas) { GameObject _object = Instantiate(RadarUiPrefab, canvas); RadarUi = _object; RadarUi.transform.localScale = Vector3.zero; aniRandarUI = RadarUi.GetComponent(); RadarRotationSpeed(TurntableSpeed); } } /// /// 数据写入 /// /// 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 "转台转速:": TurntableSpeed = weaponitemone[i].para_value; RadarRotationSpeed(TurntableSpeed); break; case "探测距离:": DetectionRange = weaponitemone[i].para_value; detectionRadius = float.Parse(DetectionRange) * 100; //Mincamera.orthographicSize = detectionRadius; break; case "近盲区:": NearBlindArea = weaponitemone[i].para_value; break; case "批量标处理能力:": BatchStandardProcessingCapability = weaponitemone[i].para_value; NumberOfProbes = int.Parse(BatchStandardProcessingCapability); break; case "探测成功率:": DetectionSuccessRate = weaponitemone[i].para_value; break; case "最小探测速度:": MinimumDetectionVelocity = weaponitemone[i].para_value; break; case "距离分辨率:": RangeResolution = weaponitemone[i].para_value; break; case "方位分辨率:": AzimuthResolution = weaponitemone[i].para_value; break; case "方位波束宽度:": AzimuthBeamwidth = weaponitemone[i].para_value; break; case "俯仰波束宽度:": PitchBeamwidth = weaponitemone[i].para_value; break; default: break; } } } /// /// 雷达转动速度 /// public void RadarRotationSpeed(string speed) { float newSpeed = float.Parse(speed); if (newSpeed <= 0) return; if (aniRandar) { aniRandar.speed = (1 / newSpeed); // 改变动画的播放速度为新速度值 } if (aniRandarUI) { aniRandarUI.speed = (1 / newSpeed); // 改变动画的播放速度为新速度值 } } /// /// 检索 /// public void RetrievalUAV() { List colliders = Physics.OverlapSphere(transform.position, detectionRadius).ToList(); // 检索范围内的所有碰撞体 int number = 0; var colliders2 = colliders.FindAll(x => x.tag == "WRJ"); if (colliders2.Count > 0) { Mincamera.orthographicSize = detectionRadius; for (int i = 0; i < colliders2.Count; i++) { if (i <= NumberOfProbes) { if (Random.value > (1 - float.Parse(DetectionSuccessRate) / 1000)) { UnmannedAerialVehicle unmannedAerialVehicle = colliders2[i].GetComponent(); if (unmannedAerialVehicle) { LaserFireControlPlatformManger laserFireControlPlatformManger = LaserFireControlPlatformManger.laserFireControlPlatformMangers.Find(x => (x != null && x.isLasing == false)); if (laserFireControlPlatformManger) { laserFireControlPlatformManger.isLasing = true; Debug.Log(laserFireControlPlatformManger.transform.name + "攻击无人机: " + unmannedAerialVehicle.transform.name); laserFireControlPlatformManger.targetPoint = unmannedAerialVehicle.transform; laserFireControlPlatformManger.Lasing(); } number++; } } } else { minimap.GetComponent().cullingMask = ~(1 << 15); } } } } private void OnMouseEnter() { Debug.LogError("鼠标进入"); RadarUi.transform.localScale = Vector3.one; Mincamera.transform.position = new Vector3(transform.position.x,350, transform.position.z); } private void OnMouseExit() { Debug.LogError("鼠标离开"); RadarUi.transform.localScale = Vector3.zero; } private void OnDestroy() { Destroy(RadarUi.gameObject); OnActivationChanged -= OnActivationChangedHandler; } }