using AX.InputSystem;
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 车辆多选寻路
///
public class MultiSelectNAV : ResourceLoadPanel
{
//public static MultiSelectNAV GetInstance;
public Toggle MultiSelectNAVButton;
public Toggle InstallNAVButton;
public RectTransform Content;
public GameObject MultiSelectNAVPanel;
public GameObject Item;
public List SelectCars;
public List TargetPoint;
public bool pathfindingFlag;
void Start()
{
//GetInstance = this;
MultiSelectNAVButton.onValueChanged.AddListener(ShowUI);
InstallNAVButton.onValueChanged.AddListener(InstallNAV);
}
private void InstallNAV(bool Ison)
{
if (Ison)
{
MessageDispatcher.AddListener("PATH_FINDING_COMMAND", PathFinding);
TargetPoint.Clear();
CancelSelectedCommand.Instance.Execute(0, null);
}
else
MessageDispatcher.RemoveListener("PATH_FINDING_COMMAND", PathFinding);
pathfindingFlag = Ison;
InputManager.MutiSelectFind = Ison;
if (!pathfindingFlag && TargetPoint.Count > 0)
{
var count = TargetPoint.Count;
var list = AgentController.LiSan(TargetPoint[count - 1], SelectCars.Count, new Vector3(9, 0, 9));
int indexer = 0;
if (SelectCars.Count>0&&list.Count>0)
{
//开始多选车辆的巡礼操作
SelectCars.ForEach((X) =>
{
TargetPoint[count - 1] = list[indexer];
X.GetComponent().AutoPathFinding(TargetPoint);
indexer++;
});
// TargetPoint.Clear();
}
}
}
public void AddSelectCar(GameObject Car)
{
if (SelectCars.Contains(Car))
return;
SelectCars.Add(Car);
var index = NAVSelectCarItem.SelectCarItem.FindIndex((X) => { return X.SelectCar == Car; });
if (index >= 0)
{
NAVSelectCarItem.SelectCarItem[index].Toggle.isOn = true;
return;
}
var item = Instantiate(Item, Content);
var scipts = item.GetComponent();
scipts.Text.text = Car.GetComponent().Affiliation + "-" + Car.GetComponent().Number;
scipts.SelectCar = Car;
}
public void RemoveSelectCar(GameObject Car)
{
if (SelectCars.Contains(Car))
{
Car.GetComponent().Stop();
SelectCars.Remove(Car);
}
}
private void PathFinding(IMessage obj)
{
if (!pathfindingFlag)
return;
var data = (PathFindingCmdArgs)obj.Data;
InputManager.pointerObj.transform.position = data.hitPoint;
TargetPoint.Add(data.hitPoint);
}
private void ShowUI(bool Ison)
{
pathfindingFlag = false;
SelectCars.Clear();
TargetPoint.Clear();
MultiSelectNAVPanel.SetActive(Ison);
InstallNAVButton.gameObject.SetActive(Ison);
InstallNAVButton.isOn = false;
if (Ison)
MessageDispatcher.AddListener("RADIO_SELECTED_COMMAND", RadioSelect);
else
MessageDispatcher.RemoveListener("RADIO_SELECTED_COMMAND", RadioSelect);
}
private void RadioSelect(IMessage obj)
{
var gameObjID = (long)obj.Sender;
var hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID);
if (hitObj.gameObject.tag.Equals(GlobalVariable.CarTag))
{
if (SelectCars.Contains(hitObj))
MessageDispatcher.SendMessage("RemoveSelectCar", hitObj);
else
AddSelectCar(hitObj);
}
}
public void Clear()
{
MultiSelectNAVButton.isOn = false;
}
}