using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 单个无人机蜂群控制
///
public class UnmannedAerialVehicleManage : MonoBehaviour
{
public static List unmannedAerialVehicleManages = new List();
///
/// 是否正在预演
///
public bool isStartRehearsing = false;
///
/// 状态
///
public Pattern pattern = Pattern.待机;
///
/// 测试用
///
public string msg;
///
/// 测试接受数据
///
private List weaponitems;
private Weaponitemone weaponitemones;
///
/// 无人机预制体
///
public GameObject UAVPrefab;
///
/// 编队无人机数量
///
public int totalObjects = 30; // 总共的物体数量
///
/// 所有子物体无人机
///
public List unmannedAerialVehicles = new List();
///
/// 线预制体
///
public GameObject LinePrefab;
///
/// 航线
///
public GameObject airRoute;
#region 无人机数据
///
/// 续航时间
///
public string batteryLife;
///
/// 抗风等级
///
public string classificationWindResistance;
///
/// 最大飞行速度
///
public string maximumFlyingSpeed;
///
/// RCS
///
public string RCS;
///
/// 卫星定位频点
///
public string satellitePositioningFrequency;
///
/// 数据链通信频点
///
public string dataLinkCommunicationFrequency;
///
/// 电子侦察能力
///
public string electronicReconnaissanceCapability;
///
/// 光学侦察能力
///
public string opticalReconnaissanceCapability;
#endregion
// Start is called before the first frame update
void Start()
{
unmannedAerialVehicleManages.Add(this);
//Weaponitem weaponitem;
//weaponitem = Newtonsoft.Json.JsonConvert.DeserializeObject(msg);
//weaponitems.Add(weaponitem);
weaponitemones = Newtonsoft.Json.JsonConvert.DeserializeObject(msg);
FillInTheData(weaponitemones);//测试写入
Formation(1);//默认阵型
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000))
{
if (hitInfo.collider.tag == "WRJ")
{
UnmannedAerialVehicleUI.Instance.unmannedAerialVehicleManage = transform.GetComponent();
}
}
}
}
///
/// 模式切换
///
public void modeSwitch(int patternCut)
{
switch (patternCut) {
case 0://待机
pattern = Pattern.待机;
break;
case 1:
pattern = Pattern.警戒;
break;
case 2:
pattern = Pattern.攻击;
ObjectPlanner objectPlanner = airRoute.GetComponent();
if (objectPlanner)
{
objectPlanner.StartMoveObjectAlongPath();
}
break;
default:
break;
}
}
#region 数据写入
///
/// 数据写入
///
///
public void FillInTheData(Weaponitemone weaponitemone)
{
for(int i=0;i< weaponitemone.data.Count; i++)
{
switch (weaponitemone.data[i].para_name) {
case "续航时间:":
batteryLife = weaponitemone.data[i].para_value;
break;
case "抗风等级:":
classificationWindResistance = weaponitemone.data[i].para_value;
break;
case "最大飞行速度:":
maximumFlyingSpeed = weaponitemone.data[i].para_value;
break;
case "RCS:":
RCS = weaponitemone.data[i].para_value;
break;
case "卫星定位频点:":
satellitePositioningFrequency = weaponitemone.data[i].para_value;
break;
case "数据链通信频点:":
dataLinkCommunicationFrequency = weaponitemone.data[i].para_value;
break;
case "电子侦察能力:":
electronicReconnaissanceCapability = weaponitemone.data[i].para_value;
break;
case "光学侦察能力:":
opticalReconnaissanceCapability = weaponitemone.data[i].para_value;
break;
default:
break;
}
if(i== (weaponitemone.data.Count - 1))
{
StartCoroutine(WeaponitemoneDataAddition());
}
}
}
///
/// 单个无人机数据写入
///
///
IEnumerator WeaponitemoneDataAddition()
{
yield return new WaitForSeconds(0.1f);
for(int i=0;i< unmannedAerialVehicles.Count; i++)
{
unmannedAerialVehicles[i].batteryLife = batteryLife;
unmannedAerialVehicles[i].classificationWindResistance = classificationWindResistance;
unmannedAerialVehicles[i].maximumFlyingSpeed = maximumFlyingSpeed;
unmannedAerialVehicles[i].RCS = RCS;
unmannedAerialVehicles[i].satellitePositioningFrequency = satellitePositioningFrequency;
unmannedAerialVehicles[i].dataLinkCommunicationFrequency = dataLinkCommunicationFrequency;
unmannedAerialVehicles[i].electronicReconnaissanceCapability = electronicReconnaissanceCapability;
unmannedAerialVehicles[i].opticalReconnaissanceCapability = opticalReconnaissanceCapability;
unmannedAerialVehicles[i].unmannedAerialVehicleManage = this;
}
}
#endregion
#region 阵型编队
///
/// 阵型选择
///
///
public void Formation(int number)
{
switch (number) {
case 1:
GenerateFormation();
break;
case 2:
MatrixFormation(5);
break;
case 3:
MatrixFormation(30);
break;
case 4:
MatrixFormation(1);
break;
default:
break;
}
}
///
/// 无人机雁式阵型
///
private void GenerateFormation()
{
int count = 1; // 每一排的物体数量
int currentCount = 0;
Vector3 startPos = new Vector3();
for (int i = 0; i < Mathf.Ceil(Mathf.Sqrt(totalObjects)); i++)
{
//Debug.Log("剩余。。。:" + (totalObjects - currentCount));
startPos = new Vector3(-i * 2, 0, -i * 2);
for (int j = 0; j < count; j++)//每排物体个数
{
if (currentCount < totalObjects)
{
Vector3 _vector3 = startPos + new Vector3(j * 2, 0, 0);
unmannedAerialVehicles[currentCount].transform.localPosition = _vector3;
currentCount++;
}
else
{
BoxCollider box = transform.GetComponent();
if (box)
{
box.center = new Vector3(0, 0, -i);
box.size = new Vector3(2 * count, 1, 2 * (i + 1));
}
}
}
count += 2; // 下一排增加两个物体
}
}
///
/// 矩阵阵型
///
private void MatrixFormation(int rows)
{
float offsetX = 2.0f; // 子物体之间的水平间距
float offsetY = 2.0f; // 子物体之间的垂直间距
float offsetZ = 2.0f; // 子物体之间的垂直间距
int currentCount = 0;
int cols = CanDivideEvenly(totalObjects, rows) ? totalObjects / rows : totalObjects / rows + 1;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (currentCount < totalObjects)
{
Vector3 position = new Vector3(col * offsetX, 0, -row * offsetZ);
unmannedAerialVehicles[currentCount].transform.localPosition = position;
currentCount++;
}
}
}
BoxCollider box = transform.GetComponent();
if (box)
{
Debug.Log("cols..:" + cols);
Debug.Log("rows..:" + rows);
box.center = new Vector3(cols-1, 0, -(rows-1));
box.size = new Vector3(cols*2, 1, 2 * rows);
}
}
bool CanDivideEvenly(int dividend, int divisor)
{
return dividend % divisor == 0;
}
#endregion
///
/// 关闭航线设置
///
public void TurnOffCourseSettings()
{
if (airRoute)
{
ObjectPlanner objectPlanner = airRoute.GetComponent();
if (objectPlanner)
{
objectPlanner.isPathCanBePlanned = false;
}
}
}
///
/// 开启航线设置
///
public void RouteSettings()
{
if (airRoute)
{
ObjectPlanner objectPlanner = airRoute.GetComponent();
if (objectPlanner)
{
objectPlanner.isPathCanBePlanned = true;
objectPlanner.lineRenderer.SetVertexCount(0);
Array.Clear(objectPlanner.positions, 0, objectPlanner.positions.Length);
}
}
else
{
GameObject _object = Instantiate(LinePrefab);
airRoute = _object;
airRoute.transform.position = Vector3.zero;
ObjectPlanner objectPlanner = airRoute.GetComponent();
if (objectPlanner)
{
objectPlanner.isPathCanBePlanned = true;
objectPlanner.SetSelectedObject(transform.gameObject);
}
}
}
private void OnMouseEnter()
{
UnmannedAerialVehicleUI.Instance.unmannedAerialVehicleManage = transform.GetComponent();
}
}
public enum Pattern
{
待机,
警戒,
攻击
}