NewN_UAVPlane/Assets/Zion/Scripts/ModelFunction/LaserFireControlPlatform/LaserFireControlPlatformMan...

636 lines
21 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;
/// <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 Coroutine coroutinetime;
/// <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;
/// <summary>
/// 十字准星架视角UI显示
/// </summary>
public GameObject RawImage;
/// <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 static float lasertime;
public float lasertimes;
/// <summary>
/// 判断物体是否在相机里面
/// </summary>
public bool ispcamera;
void Start()
{
equipmentCommon = GetComponent<EquipmentCommon>();
laserFireControlPlatformMangers.Add(this);
camera = transform.GetChild(1).GetComponent<Camera>();
//weaponitemones = Newtonsoft.Json.JsonConvert.DeserializeObject<Weaponitemone>(msg);
//FillInTheData(weaponitemones);//测试写入
// 订阅布尔值变化事件
OnActivationChanged += OnActivationChangedHandler;
//生成激光
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.transform.SetParent(transform);
OuterLaserlineRenderer = _OuterLaserlineRenderer.GetComponent<LineRenderer>();
if (OuterLaserlineRenderer)
{
OuterLaserlineRenderer.positionCount = 2;// 设置线段的端点数量为2
OuterLaserlineRenderer.SetPosition(0, LaserPoint.transform.position);// 设置线段起点为物体位置
OuterLaserlineRenderer.SetPosition(1, LaserPoint.transform.position);// 设置线段终点为目标点
}
}
void Update()
{
if (lasertime>0)
{
lasertime -= Time.deltaTime;
}
//if (UIBootstrap.Instance.GetRoleByIDPracticeId(GlobalFlag.practiceSeatId) != "0"&&Input.GetMouseButtonDown(0))
//{
// if (!EventSystem.current.IsPointerOverGameObject())
// {
// Destroy(crosshair.gameObject);
// Front = true;
// }
//}
if (UIBootstrap.Instance.GetRoleByIDPracticeId(GlobalFlag.practiceSeatId) != "0" && Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
if (hit.transform.gameObject.tag == "AttackTarget" && hit.transform.gameObject.name == "激光火控平台(Clone)")
{
Transform canvas = GameObject.Find("Canvas").transform;
if (canvas)
{
Instantiate(RawImage, canvas);
}
}
}
}
}
/// <summary>
/// 开启暂停激光发射武器
/// </summary>
void OnActivationChangedHandler(bool bos)
{
if (bos)
{
Debug.LogError("开启协程");
starttimer();
}
else
{
Debug.LogError("暂停协程");
stoptimer();
}
}
/// <summary>
/// 开启协程
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void starttimer()
{
if (equipmentCommon.isPlayer && coroutinetime == null)
{
coroutinetime = StartCoroutine(Timer());
islaser = true;
}
}
/// <summary>
/// 关闭协程
/// </summary>
private void stoptimer()
{
if (equipmentCommon.isPlayer && coroutinetime != null)
{
StopCoroutine(coroutinetime);
islaser = false;
}
}
/// <summary>
/// 协程
/// </summary>
IEnumerator Timer()
{
while (true)
{
yield return new WaitForSeconds(lasertime);
if (lasertime != 0)
{
//Laserattack();
}
}
}
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);
lasertimes = 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 Laserattack()
{
if (Receivingdistance > 0 && targetPoint != null)
{
Collider[] colliders = Physics.OverlapSphere(transform.position, Receivingdistance);//检查是否在打击的范围内
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject.tag == "WRJ")
{
UnmannedAerialVehicle unmannedAerialVehicle = null;
if (colliders[i].GetComponent<UnmannedAerialVehicle>())
{
unmannedAerialVehicle = colliders[i].GetComponent<UnmannedAerialVehicle>();
}
if (unmannedAerialVehicle != null)
{
Vector3 Angle = unmannedAerialVehicle.transform.position - transform.position;
float Angle1 = Vector3.Angle(Angle, transform.forward);
Debug.LogError("角度大小:" + Angle1);
if (float.Parse(InterferenceAngle) >= Angle1)
{
Debug.LogError("角度没有问题进来了" + InterferenceAngle);
if (Receivingposin == null)
{
//if (Random.value>0.5f)
//{
Receivingposin = unmannedAerialVehicle.transform;
Debug.LogError("接收无人机的位置" + Receivingposin.position);
var nowData = GetSyncData();
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
isLasings = true;
CastRayAndRender();
break;
//}
//else
//{
// Receivingposin = unmannedAerialVehicle.transform;
// var nowData1= GetSyncData1();
// _ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData1));
// isLasings = true;
// CastRayAndRender1();
// break;
//}
}
}
}
}
}
}
}
private string GetSyncData1()
{
return string.Format("{0},{1},{2},{3}" + equipmentCommon.deviceID, Receivingposin.position.x + 3, Receivingposin.position.y + 3, Receivingposin.position.z);
}
/// <summary>
/// 看向无人机
/// </summary>
public void Lasing()
{
if (targetPoint != null)
{
LaserModer.transform.DOLookAt(targetPoint.position,0.5f).OnComplete(() =>
{
});
//var nowData = GetSyncData();
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
}
else
{
Debug.LogError("没有目标");
}
}
/// <summary>
/// 十字准星架视角
/// </summary>
public void Crosshair()
{
if (targetPoint != null)
{
camera.transform.DOLookAt(targetPoint.position,0.5f).SetEase(Ease.Linear).OnComplete(() =>
{
Debug.LogError("看向进来了");
Launchanattack();
});
}
}
/// <summary>
/// 激光显示
/// </summary>
public void Launchanattack()
{
if (targetPoint != null)
{
Vector3 pos = camera.WorldToViewportPoint(targetPoint.position);
ispcamera = (pos.x > 0 && pos.x < 1 && pos.y > 0 && pos.y < 1 && pos.z > 0);
if (ispcamera)
{
Debug.LogError("出现在摄像机视角里面了");
Vector3 cector = targetPoint.position - transform.position;
float angle = Vector3.Angle(cector, transform.forward);
if (float.Parse(InterferenceAngle) > angle)
{
Debug.Log("角度合适进行打击");
var nowData = GetSyncData();
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
CastRayAndRender();
}
else
{
Debug.LogError("超出了角度不进行攻击");
}
}
else
{
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);
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);
}
/// <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); // 设置线段终点为目标点
}
if (lasertimes!=0)
{
lasertime = lasertimes;
}
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());
});
}
/// <summary>
/// 检查鼠标是否进入物体
/// </summary>
public void OnMouseEnter()
{
//if (UIBootstrap.Instance.GetRoleByIDPracticeId(GlobalFlag.practiceSeatId) != "0")
//{
// Transform Canvas = GameObject.Find("Canvas").transform;
// if (Canvas && Front)
// {
// crosshair = Instantiate(RawImage, Canvas);
// crosshair.transform.position = Camera.main.WorldToScreenPoint(transform.position);
// }
//}
}
/// <summary>
/// 检查鼠标是否离开物体
/// </summary>
public void OnMouseExit()
{
Front = false;
}
private void OnDestroy()
{
Destroy(crosshair.gameObject);
Destroy(InnerLaserlineRenderer.gameObject);
Destroy(InnerLaserlineRenderer.gameObject);
Destroy(OuterLaserlineRenderer.gameObject);
OnActivationChanged -= OnActivationChangedHandler;
}
}