479 lines
16 KiB
C#
479 lines
16 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using AdamThinkDevicesData;
|
|
using AdamSync;
|
|
using Newtonsoft.Json;
|
|
using static InterfaceManager;
|
|
using DG.Tweening.Core.Easing;
|
|
using UnityEngine.EventSystems;
|
|
using System;
|
|
using Random = UnityEngine.Random;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 激光火控平台
|
|
/// </summary>
|
|
public class LaserFireControlPlatformManger : MonoBehaviour
|
|
{
|
|
public EquipmentCommon equipmentCommon;
|
|
/// <summary>
|
|
/// 所有激光火控平台
|
|
/// </summary>
|
|
public static List<LaserFireControlPlatformManger> laserFireControlPlatformMangers = new List<LaserFireControlPlatformManger>();
|
|
#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 bool islaser = false;
|
|
/// <summary>
|
|
/// 协程对象
|
|
/// </summary>
|
|
private Coroutine timerCoroutine;
|
|
/// <summary>
|
|
/// 定时器运行状态
|
|
/// </summary>
|
|
private bool isTimerRunning = false;
|
|
/// <summary>
|
|
/// 间隔时间
|
|
/// </summary>
|
|
public float interval = 5.0f;
|
|
#endregion
|
|
|
|
|
|
#region 激光火控平台数据
|
|
/// <summary>
|
|
/// 储能间隔时间
|
|
/// </summary>
|
|
public string StorageIntervalTime;
|
|
/// <summary>
|
|
/// 毁伤目标累积作用时间
|
|
/// </summary>
|
|
public string CumulativeActionTimeOfDamageTarget;
|
|
/// <summary>
|
|
/// 干扰距离
|
|
/// </summary>
|
|
public string InterferenceDistance;
|
|
/// <summary>
|
|
/// 干扰角度
|
|
/// </summary>
|
|
public string InterferenceAngle;
|
|
/// <summary>
|
|
/// 接收干扰距离
|
|
/// </summary>
|
|
public float Receivingdistance;
|
|
/// <summary>
|
|
/// 计时器
|
|
/// </summary>
|
|
public float timepiece = 5;
|
|
#endregion
|
|
|
|
|
|
|
|
#region 激光
|
|
/// <summary>
|
|
/// 是否这在攻击无人机
|
|
/// </summary>
|
|
public bool isLasing = false;//判断看向无人机
|
|
public bool isLasings = false;//判断时候需要攻击
|
|
public GameObject LaserModer;
|
|
public GameObject LaserPoint;
|
|
public Transform targetPoint; // 锁定目标点的Transform组件
|
|
private Transform Receivingposin;//激光武器打击无人机位置
|
|
public GameObject InnerLaserlineRendererPrefab; // 用于绘制射线的LineRenderer组件
|
|
public GameObject OuterLaserlineRendererPrefab; // 用于绘制射线的LineRenderer组件
|
|
public LineRenderer InnerLaserlineRenderer; // 用于绘制射线的LineRenderer组件
|
|
public LineRenderer OuterLaserlineRenderer; // 用于绘制射线的LineRenderer组件
|
|
//private Vector3 laserStartPoint = Vector3.zero;
|
|
//private Vector3 laserEndPoint = Vector3.zero;
|
|
public float FireSpeed = 20.0f;//激光速度
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 爆炸预制体
|
|
/// </summary>
|
|
public GameObject explodePrefab;
|
|
/// <summary>
|
|
/// 十字准星架视角
|
|
/// </summary>
|
|
public Camera camera;
|
|
|
|
public RawImage raw;
|
|
|
|
/// <summary>
|
|
/// 十字准星架视角UI显示
|
|
/// </summary>
|
|
public GameObject rawImageUIObj;
|
|
/// <summary>
|
|
/// 接收十字准星生成的界面
|
|
/// </summary>
|
|
public GameObject crosshair;
|
|
/// <summary>
|
|
/// 显示十字准星开关
|
|
/// </summary>
|
|
public bool Front = true;
|
|
/// <summary>
|
|
/// 点击空白处碰撞器
|
|
/// </summary>
|
|
public GameObject Detection;
|
|
/// <summary>
|
|
/// 摄像机视角开关
|
|
/// </summary>
|
|
public bool isplaser = true;
|
|
/// <summary>
|
|
/// 激光火炮打击时间
|
|
/// </summary>
|
|
public float lasertime;
|
|
/// <summary>
|
|
/// 激光火炮打击时间
|
|
/// </summary>
|
|
public float storageIntervalTime;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private bool onlyOne = true;
|
|
/// <summary>
|
|
/// 判断物体是否在相机里面
|
|
/// </summary>
|
|
public bool ispcamera;
|
|
|
|
void Start()
|
|
{
|
|
equipmentCommon = GetComponent<EquipmentCommon>();
|
|
laserFireControlPlatformMangers.Add(this);
|
|
//生成激光
|
|
GameObject _InnerLaserlineRenderer = Instantiate(InnerLaserlineRendererPrefab);
|
|
_InnerLaserlineRenderer.transform.position = Vector3.zero;
|
|
//_InnerLaserlineRenderer.transform.SetParent(transform);
|
|
InnerLaserlineRenderer = _InnerLaserlineRenderer.GetComponent<LineRenderer>();
|
|
if (InnerLaserlineRenderer)
|
|
{
|
|
InnerLaserlineRenderer.positionCount = 2; // 设置线段的端点数量为2
|
|
InnerLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
InnerLaserlineRenderer.SetPosition(1, LaserPoint.transform.position); // 设置线段终点为目标点
|
|
|
|
}
|
|
GameObject _OuterLaserlineRenderer = Instantiate(OuterLaserlineRendererPrefab);
|
|
_OuterLaserlineRenderer.transform.position = Vector3.zero;
|
|
OuterLaserlineRenderer = _OuterLaserlineRenderer.GetComponent<LineRenderer>();
|
|
if (OuterLaserlineRenderer)
|
|
{
|
|
OuterLaserlineRenderer.positionCount = 2;// 设置线段的端点数量为2
|
|
OuterLaserlineRenderer.SetPosition(0, LaserPoint.transform.position);// 设置线段起点为物体位置
|
|
OuterLaserlineRenderer.SetPosition(1, LaserPoint.transform.position);// 设置线段终点为目标点
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
lasertime -= Time.deltaTime;
|
|
if (onlyOne && equipmentCommon.deviceID.Length > 10)
|
|
{
|
|
onlyOne = false;
|
|
DroneViewDisplay.Instance.CreateUI(equipmentCommon.deviceID, camera, raw);
|
|
}
|
|
}
|
|
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (Receivingdistance > 0 && isplaser == true)
|
|
{
|
|
camera.farClipPlane = Receivingdistance;
|
|
isplaser = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#region 创建场景激光发射
|
|
|
|
/// <summary>
|
|
/// 数据写入
|
|
/// </summary>
|
|
/// <param name="weaponitemone"></param>
|
|
public void FillInTheData(List<List_paraItem> weaponitemone)
|
|
{
|
|
for (int i = 0; i < weaponitemone.Count; i++)
|
|
{
|
|
switch (weaponitemone[i].para_name)
|
|
{
|
|
case "储能间隔时间:":
|
|
StorageIntervalTime = weaponitemone[i].para_value;
|
|
lasertime = float.Parse(StorageIntervalTime);
|
|
storageIntervalTime = float.Parse(StorageIntervalTime);
|
|
break;
|
|
case "毁伤目标累积作用时间:":
|
|
CumulativeActionTimeOfDamageTarget = weaponitemone[i].para_value;
|
|
break;
|
|
case "干扰距离:":
|
|
InterferenceDistance = weaponitemone[i].para_value;
|
|
|
|
Receivingdistance = float.Parse(InterferenceDistance) * 1000;
|
|
break;
|
|
case "瞬时视场:":
|
|
InterferenceAngle = weaponitemone[i].para_value;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 看向无人机
|
|
/// </summary>
|
|
public void Lasing()
|
|
{
|
|
if (targetPoint != null)
|
|
{
|
|
LaserModer.transform.DOLookAt(targetPoint.position, 0.5f).OnComplete(() =>
|
|
{
|
|
Launchanattack();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("没有目标");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 激光显示
|
|
/// </summary>
|
|
public void Launchanattack()
|
|
{
|
|
if (targetPoint != null && Receivingdistance > 0)
|
|
{
|
|
Vector3 pos = camera.WorldToViewportPoint(targetPoint.position);
|
|
Debug.Log("攻击目标距离" + Vector3.Distance(targetPoint.position, transform.position));
|
|
ispcamera = (pos.x > 0 && pos.x < 1 && pos.y > 0 && pos.y < 1 && pos.z > 0);
|
|
if (Vector3.Distance(targetPoint.position, transform.position) < Receivingdistance && ispcamera)
|
|
{
|
|
Debug.Log("出现在摄像机视角里面了");
|
|
Vector3 cector = targetPoint.position - transform.position;
|
|
float angle = Vector3.Angle(cector, transform.forward);
|
|
if (float.Parse(InterferenceAngle) > angle)
|
|
{
|
|
Debug.Log("角度合适进行打击");
|
|
var nowData = GetSyncData();
|
|
MyNetMQClient.instance.Send(nowData);
|
|
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
|
//DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
//MQTTManager.instance.SendData(MQTTManager.instance.Lasing, nowData);
|
|
CastRayAndRender();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("超出了角度不进行攻击");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
targetPoint = null;
|
|
isLasing = false;
|
|
Debug.LogError("飞机不在摄像机范围内");
|
|
}
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 激光显示
|
|
/// </summary>
|
|
public void CastRayAndRender()
|
|
{
|
|
|
|
if (InnerLaserlineRenderer)
|
|
{
|
|
InnerLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
InnerLaserlineRenderer.SetPosition(1, targetPoint.position); // 设置线段终点为目标点
|
|
}
|
|
if (OuterLaserlineRenderer)
|
|
{
|
|
OuterLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
OuterLaserlineRenderer.SetPosition(1, targetPoint.position); // 设置线段终点为目标点
|
|
}
|
|
StrikeDrone(targetPoint);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打击无人机
|
|
/// </summary>
|
|
private void StrikeDrone(Transform hit)
|
|
{
|
|
UnmannedAerialVehicle unmannedAerialVehicle = hit.GetComponent<UnmannedAerialVehicle>();
|
|
if (unmannedAerialVehicle)
|
|
{
|
|
//unmannedAerialVehicle.BeAssaulted("激光打击");
|
|
AddBao(unmannedAerialVehicle);
|
|
StartCoroutine(LaserExtinction());
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 销毁单体无人机
|
|
/// </summary>
|
|
public void AddBao(UnmannedAerialVehicle unmannedAerialVehicle)
|
|
{
|
|
string nowData = GetSyncDis(unmannedAerialVehicle);
|
|
Debug.Log(nowData);
|
|
MyNetMQClient.instance.Send(nowData);
|
|
//DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
|
//MQTTManager.instance.SendData(MQTTManager.instance.DroneWasDestroyed, 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);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 隐藏激光
|
|
/// </summary>
|
|
public IEnumerator LaserExtinction()
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
if (InnerLaserlineRenderer)
|
|
{
|
|
InnerLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
InnerLaserlineRenderer.SetPosition(1, LaserPoint.transform.position); // 设置线段终点为目标点
|
|
}
|
|
if (OuterLaserlineRenderer)
|
|
{
|
|
OuterLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
OuterLaserlineRenderer.SetPosition(1, LaserPoint.transform.position); // 设置线段终点为目标点
|
|
}
|
|
isLasing = false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
/// 激光发射点位
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected string GetSyncData()
|
|
{
|
|
Debug.Log("目标点位..:" + targetPoint.position);
|
|
return string.Format("{0},{1},{2},{3},{4}", "Lasing", equipmentCommon.deviceID, targetPoint.position.x, targetPoint.position.y, targetPoint.position.z);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 非自生成激光发射台发射激光发射激光
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public void NonSelfGeneratedEmissionLaser(string[] data)
|
|
{
|
|
//激光位置下发传递
|
|
Vector3 vector3 = new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4]));
|
|
LaserModer.transform.DOLookAt(vector3, 0.1f).OnComplete(() =>
|
|
{
|
|
if (InnerLaserlineRenderer)
|
|
{
|
|
InnerLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
InnerLaserlineRenderer.SetPosition(1, vector3); // 设置线段终点为目标点
|
|
}
|
|
if (OuterLaserlineRenderer)
|
|
{
|
|
OuterLaserlineRenderer.SetPosition(0, LaserPoint.transform.position); // 设置线段起点为物体位置
|
|
OuterLaserlineRenderer.SetPosition(1, vector3); // 设置线段终点为目标点
|
|
}
|
|
StartCoroutine(LaserExtinction());
|
|
});
|
|
}
|
|
void OnMouseDown()
|
|
{
|
|
|
|
//Transform canvas = GameObject.Find("Canvas").transform;
|
|
//if (canvas)
|
|
//{
|
|
// Instantiate(RawImage, canvas);
|
|
//}
|
|
if (GlobalFlag.blueOrRed == 1)
|
|
rawImageUIObj.SetActive(true);
|
|
}
|
|
/// <summary>
|
|
/// 检查鼠标是否离开物体
|
|
/// </summary>
|
|
public void OnMouseExit()
|
|
{
|
|
Front = false;
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
Destroy(crosshair.gameObject);
|
|
if (InnerLaserlineRenderer != null)
|
|
Destroy(InnerLaserlineRenderer.gameObject);
|
|
if (OuterLaserlineRenderer != null)
|
|
Destroy(OuterLaserlineRenderer.gameObject);
|
|
}
|
|
}
|