177 lines
5.3 KiB
C#
177 lines
5.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
[Serializable]
|
|
public class FormationInfo
|
|
{
|
|
public int groupIndex;
|
|
public List<UnmannedAerialVehicleManage> uavms = new List<UnmannedAerialVehicleManage>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编队控制脚本
|
|
/// </summary>
|
|
public class FormationManager : MonoSingleton<FormationManager>
|
|
{
|
|
public Button startFormationBtn;
|
|
public Button submitFormationBtn;
|
|
public Button cancelFormationBtn;
|
|
public bool isStartFormation;
|
|
public List<FormationInfo> formationInfos = new List<FormationInfo>();
|
|
public FormationInfo formationInfo;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
startFormationBtn.onClick.AddListener(OnStartFormation);
|
|
submitFormationBtn.onClick.AddListener(OnSubmit);
|
|
cancelFormationBtn.onClick.AddListener(OnCancel);
|
|
CheckBtns(0);
|
|
}
|
|
|
|
private void CheckBtns(int index)
|
|
{
|
|
startFormationBtn.gameObject.SetActive(index == 0);
|
|
submitFormationBtn.gameObject.SetActive(index == 1);
|
|
cancelFormationBtn.gameObject.SetActive(index == 1);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (isStartFormation)
|
|
{
|
|
if (Input.GetKey(KeyCode.LeftControl) && Input.GetMouseButtonDown(0))
|
|
{
|
|
if (EventSystem.current.IsPointerOverGameObject()) return;
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hitInfo;
|
|
if (Physics.Raycast(ray, out hitInfo, 1000))
|
|
{
|
|
if (hitInfo.collider.gameObject.tag == "WRJ")
|
|
{
|
|
UnmannedAerialVehicleManage uavmTemp = hitInfo.collider.gameObject.GetComponent<UnmannedAerialVehicleManage>();
|
|
if (uavmTemp != null && !uavmTemp.isGroup)
|
|
{
|
|
if (!formationInfo.uavms.Contains(uavmTemp))
|
|
{
|
|
uavmTemp.isGroup = true;
|
|
uavmTemp.groupId = formationInfo.groupIndex;
|
|
formationInfo.uavms.Add(uavmTemp);
|
|
}
|
|
}
|
|
Debug.Log("拾取");
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置组中得无人机所有航线
|
|
/// </summary>
|
|
public void SetUAVMGroupAirRoute(int uavmGroupID, Vector3 pos, DistanceMeasurement _airRoute)
|
|
{
|
|
List<UnmannedAerialVehicleManage> uavms = GroupUAVM(uavmGroupID);
|
|
for (int j = 0; j < uavms.Count; j++)
|
|
{
|
|
if (!uavms[j].airRoute)
|
|
{
|
|
uavms[j].airRoute = _airRoute.gameObject;
|
|
uavms[j].SetStartPos(_airRoute.PosPrefab);
|
|
}
|
|
uavms[j].positions.Enqueue(pos);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 清除组内所有无人机得航线
|
|
/// </summary>
|
|
/// <param name="uavmGroupID"></param>
|
|
public void RemoveUAMGroupAirRoute(int uavmGroupID)
|
|
{
|
|
List<UnmannedAerialVehicleManage> uavms = GroupUAVM(uavmGroupID);
|
|
for (int j = 0; j < uavms.Count; j++)
|
|
{
|
|
uavms[j].positions.Clear();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置组 航线设置
|
|
/// </summary>
|
|
public void SetGroupAttackByGroupID(int uavmGroupID, bool isOpen)
|
|
{
|
|
List<UnmannedAerialVehicleManage> uavms = GroupUAVM(uavmGroupID);
|
|
for (int j = 0; j < uavms.Count; j++)
|
|
{
|
|
int index = j;
|
|
if (isOpen)
|
|
{
|
|
uavms[index].RouteSettings();
|
|
if (uavms[index].airRoute)
|
|
uavms[index].airRoute.transform.localScale = Vector3.one;
|
|
}
|
|
else
|
|
{
|
|
uavms[index].TurnOffCourseSettings();
|
|
if (uavms[index].airRoute)
|
|
uavms[index].airRoute.transform.localScale = Vector3.zero;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void SetGroupUAVMState(int uavmGroupID,int patternCut)
|
|
{
|
|
List<UnmannedAerialVehicleManage> uavms = GroupUAVM(uavmGroupID);
|
|
for (int j = 0; j < uavms.Count; j++)
|
|
{
|
|
uavms[j].modeSwitch(patternCut);
|
|
}
|
|
}
|
|
|
|
private List<UnmannedAerialVehicleManage> GroupUAVM(int uavmGroupID)
|
|
{
|
|
List<UnmannedAerialVehicleManage> uavms = new List<UnmannedAerialVehicleManage>();
|
|
for (int i = 0; i < formationInfos.Count; i++)
|
|
{
|
|
int indexI = i;
|
|
if (formationInfos[indexI].groupIndex == uavmGroupID)
|
|
{
|
|
uavms = formationInfos[indexI].uavms;
|
|
}
|
|
}
|
|
return uavms;
|
|
}
|
|
|
|
|
|
|
|
public void OnStartFormation()
|
|
{
|
|
isStartFormation = true;
|
|
formationInfo = new FormationInfo();
|
|
formationInfo.groupIndex = formationInfos.Count;
|
|
CheckBtns(1);
|
|
}
|
|
|
|
public void OnSubmit()
|
|
{
|
|
isStartFormation = false;
|
|
if (formationInfo.uavms.Count > 0)
|
|
{
|
|
if (!formationInfos.Contains(formationInfo))
|
|
formationInfos.Add(formationInfo);
|
|
}
|
|
formationInfo = null;
|
|
CheckBtns(0);
|
|
}
|
|
|
|
public void OnCancel()
|
|
{
|
|
formationInfo = null;
|
|
CheckBtns(0);
|
|
}
|
|
}
|