You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
399 lines
12 KiB
399 lines
12 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using System.IO; |
|
using Newtonsoft.Json; |
|
using UnityEngine.UI; |
|
using AX.MessageSystem; |
|
using System; |
|
|
|
[Serializable] |
|
public class SubEquipData |
|
{ |
|
public List<Equip> equips; |
|
public string gameObjectname; |
|
} |
|
public class EquipSelect : ResourceLoadPanel<EquipSelect> |
|
{ |
|
/// <summary> |
|
/// 所有装备 |
|
/// </summary> |
|
public List<Equip> Equips = new List<Equip>(); |
|
/// <summary> |
|
/// 装备组有哪些装备类型,key为装备组,valus为改组包含的装备类型 |
|
/// </summary> |
|
public Dictionary<string, List<string>> Groups = new Dictionary<string, List<string>>(); |
|
|
|
/// <summary> |
|
/// 本次提交所选择的装备 |
|
/// </summary> |
|
private List<Equip> selectEquips = new List<Equip>(); |
|
|
|
public string Cloths = "隔热防护服,灭火防护服,消防避火服" ; |
|
public bool HasCloth = false; |
|
public float DrawLineLength = 25f; |
|
public float LiftLightLength = 25f; |
|
///// <summary> |
|
///// 装备组有哪些类,类里有哪些装备 |
|
///// </summary> |
|
//public static Dictionary<string, Dictionary<string, List<Equip>>> equipsGroups = new Dictionary<string, Dictionary<string, List<Equip>>>(); |
|
|
|
private GameObject GroupToggleItem;//组按钮预设 |
|
private GameObject GroupPanelItem;//组主体预设 |
|
private Transform GroupContent; |
|
private Transform bag; |
|
private Transform equipsumselect; |
|
|
|
void Awake() |
|
{ |
|
GroupContent = transform.Find("Group/Viewport/Content"); |
|
GroupToggleItem = Resources.Load("UI/EquipUI/GroupToggle") as GameObject; |
|
GroupPanelItem= Resources.Load("UI/EquipUI/GroupPanel") as GameObject; |
|
bag = transform.parent.Find("Bag"); |
|
equipsumselect = transform.parent.Find("EquipNumSelectPanel"); |
|
InitData(); |
|
InitGroups(); |
|
gameObject.SetActive(false); |
|
} |
|
private void Start() |
|
{ |
|
DrawGroup(); |
|
MessageDispatcher.AddListener("EquipAddSubmit", AddSubmit); |
|
MessageDispatcher.AddListener("EquipRemoveSubmit", RemoveSubmit); |
|
MessageDispatcher.RemoveListener("SelectChange", selectchange); |
|
MessageDispatcher.AddListener("ReplayEvent", ReplayEventSelectEquip); |
|
} |
|
|
|
private void ReplayEventSelectEquip(IMessage obj) |
|
{ |
|
if (RecordEvent.IsReplay()) |
|
{ |
|
var eventData = (EventData)obj.Data; |
|
if (eventData.eventType == RecordEventType.SelectEquip) |
|
{ |
|
SubEquipData args = JsonUtility.FromJson<SubEquipData>(eventData.json); |
|
string equipsname = ""; |
|
for (int i = 0; i < args.equips.Count; i++) |
|
{ |
|
equipsname += args.equips[i].Name+" "; |
|
} |
|
equipsname += "已提交"; |
|
ResourceLoadWindow.Instance.LoadTextHintWindow(equipsname, 2f); |
|
|
|
} |
|
} |
|
|
|
} |
|
private void OnDestroy() |
|
{ |
|
MessageDispatcher.RemoveListener("EquipAddSubmit", AddSubmit); |
|
MessageDispatcher.RemoveListener("EquipRemoveSubmit", RemoveSubmit); |
|
MessageDispatcher.RemoveListener("SelectChange", selectchange); |
|
MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventSelectEquip); |
|
} |
|
|
|
private void selectchange(IMessage obj) |
|
{ |
|
Close(); |
|
} |
|
|
|
/// <summary> |
|
/// 读取json文件 |
|
/// </summary> |
|
/// <typeparam name="T"></typeparam> |
|
/// <param name="fileName"></param> |
|
/// <returns></returns> |
|
public T LoadJson<T>(string fileName) |
|
{ |
|
string json = null; |
|
string path = Path.Combine(Application.streamingAssetsPath, fileName); |
|
if (File.Exists(path)) |
|
{ |
|
json = File.ReadAllText(path); |
|
} |
|
T data = JsonConvert.DeserializeObject<T>(json); |
|
return data; |
|
} |
|
/// <summary> |
|
/// 从json中读取装备数据 |
|
/// </summary> |
|
private void InitData() |
|
{ |
|
Equips.Clear(); |
|
Equips = LoadJson<List<Equip>>("Equip.json"); |
|
//foreach (var item in tempData) |
|
//{ |
|
// Equips.Add(item.ID, item); |
|
//} |
|
} |
|
/// <summary> |
|
/// 从json中读取组数据 |
|
/// </summary> |
|
private void InitGroups() |
|
{ |
|
List<EquipGroup> tempData = LoadJson<List<EquipGroup>>("EquipGroup.json"); |
|
foreach (var item in tempData) |
|
{ |
|
Groups.Add(item.Name, item.Types); |
|
} |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 画Group按钮和panel |
|
/// </summary> |
|
private void DrawGroup() |
|
{ |
|
|
|
Dictionary<string, List<string>>.KeyCollection keycoll = Groups.Keys; |
|
List<string> keylist = new List<string>(); |
|
foreach (var item in keycoll) |
|
{ |
|
keylist.Add(item); |
|
} |
|
for (int i = 0; i < keylist.Count; i++) |
|
{ |
|
GameObject grouptoggle = Instantiate(GroupToggleItem, GroupContent); |
|
grouptoggle.name = keylist[i]; |
|
grouptoggle.transform.Find("Label").GetComponent<Text>().text = keylist[i]; |
|
grouptoggle.GetComponent<Toggle>().group = GroupContent.GetComponent<ToggleGroup>(); |
|
GameObject groupMain = Instantiate(GroupPanelItem, transform); |
|
groupMain.name = keylist[i]; |
|
grouptoggle.GetComponent<GroupBindPanel>().ControlPanel = groupMain; |
|
if (i==0) |
|
{ |
|
grouptoggle.GetComponent<Toggle>().isOn = true; |
|
} |
|
} |
|
} |
|
/// <summary> |
|
/// 从所有装备中找到某件装备的初始信息 |
|
/// </summary> |
|
/// <param name="equipName"></param> |
|
/// <returns></returns> |
|
public Equip GetInitInfoByName(string equipName) |
|
{ |
|
Equip eq = null; |
|
for (int i = 0; i < Equips.Count; i++) |
|
{ |
|
if (Equips[i].Name==equipName) |
|
{ |
|
eq = Equips[i]; |
|
break; |
|
} |
|
} |
|
return eq; |
|
} |
|
/// <summary> |
|
/// 根据名称获取一个装备拷贝,未进行深复制.除了装备数量及是否选中 其余为定值 |
|
/// </summary> |
|
/// <param name="equipname"></param> |
|
/// <returns></returns> |
|
public Equip GetInitEquipByName(string equipname) |
|
{ |
|
Equip eq = new Equip(); |
|
Equip baseeq = GetInitInfoByName(equipname); |
|
eq.ID = baseeq.ID; |
|
eq.Name = baseeq.Name; |
|
eq.IsSelected = false; |
|
eq.SelectGroup = baseeq.SelectGroup; |
|
eq.PicName = baseeq.PicName; |
|
eq.Group = baseeq.Group; |
|
eq.Type = baseeq.Type; |
|
eq.Number =0; |
|
eq.IsSelectMore = baseeq.IsSelectMore; |
|
|
|
return eq; |
|
} |
|
|
|
public List<Equip> GetSelectEquips() |
|
{ |
|
return selectEquips; |
|
} |
|
|
|
public Equip GetEquipFromSelectByName(string name) |
|
{ |
|
Equip eq = null; |
|
for (int i = 0; i <selectEquips.Count; i++) |
|
{ |
|
if (selectEquips[i].Name==name) |
|
{ |
|
eq = selectEquips[i]; |
|
break; |
|
} |
|
} |
|
return eq; |
|
} |
|
|
|
private void RemoveSubmit(IMessage obj) |
|
{ |
|
Equip remove = (Equip)obj.Data; |
|
for (int i = 0; i < selectEquips.Count; i++) |
|
{ |
|
if (selectEquips[i].Name == remove.Name) |
|
{ |
|
selectEquips.Remove(selectEquips[i]); |
|
} |
|
} |
|
} |
|
|
|
private void AddSubmit(IMessage obj) |
|
{ |
|
Equip add = (Equip)obj.Data; |
|
if (!add.IsSelectMore) |
|
{ |
|
bool has = false; |
|
for (int i = 0; i < selectEquips.Count; i++) |
|
{ |
|
if (selectEquips[i].Name==add.Name) |
|
{ |
|
has= true; |
|
} |
|
} |
|
if (!has) |
|
{ |
|
selectEquips.Add(add); |
|
} |
|
} |
|
else |
|
{ |
|
bool has = false; |
|
for (int i = 0; i < selectEquips.Count; i++) |
|
{ |
|
if (selectEquips[i].Name==add.Name) |
|
{ |
|
has = true; |
|
selectEquips[i].Number = add.Number; |
|
if (add.Number==0) |
|
{ |
|
selectEquips.Remove(selectEquips[i]); |
|
} |
|
} |
|
} |
|
if (!has) |
|
{ |
|
selectEquips.Add(add); |
|
} |
|
} |
|
} |
|
public void CancelSubmit() |
|
{ |
|
selectEquips.Clear(); |
|
ResetItemToggle(); |
|
} |
|
|
|
public void SureSubmit() |
|
{ |
|
if (!CheckCloth()) |
|
{ |
|
return; |
|
} |
|
|
|
//提交数据给当前背包 |
|
FiremanEquipRecordData data = new FiremanEquipRecordData |
|
{ |
|
equips = selectEquips, |
|
objectName = SelectedObjs.selectedCharacters[0].name |
|
}; |
|
MessageDispatcher.SendMessage("EquipSureSubmit", data); |
|
// AddRecordSelectEquip(data); |
|
StartCoroutine(WaitClose(0.5f)); |
|
AddRecordEquipChange(data); |
|
|
|
} |
|
public void AddRecordEquipChange(FiremanEquipRecordData args) |
|
{ |
|
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) |
|
{ |
|
var eventData = new EventData(); |
|
eventData.time = RecordManager.Instance.RecordTimer; |
|
eventData.cloneObjType = CloneObjType.None; |
|
eventData.eventType = RecordEventType.SelectEquip; |
|
string json = JsonUtility.ToJson(args); |
|
eventData.json = json; |
|
RecordManager.Instance.jsonData.eventDataList.Add(eventData); |
|
} |
|
} |
|
public void Close() |
|
{ |
|
CancelSubmit(); |
|
if (EquipNumSelectPanel.GetInstance.gameObject.activeInHierarchy) |
|
{ |
|
EquipNumSelectPanel.GetInstance.gameObject.SetActive(false); |
|
} |
|
gameObject.SetActive(false); |
|
// BagPanel.GetInstance.gameObject.SetActive(false); |
|
} |
|
private void ResetItemToggle() |
|
{ |
|
EquipItemSelect[] eqs = GetComponentsInChildren<EquipItemSelect>(true); |
|
for (int i = 0; i < eqs.Length; i++) |
|
{ |
|
if (eqs[i].GetComponent<Toggle>().isOn) |
|
{ |
|
eqs[i].GetComponent<Toggle>().isOn = false; |
|
} |
|
eqs[i].GetComponent<EquipItemSelect>().SetImgHide(); |
|
} |
|
|
|
} |
|
/// <summary> |
|
/// 是否可提交,false为服装多选了,不可提交 |
|
/// </summary> |
|
/// <returns></returns> |
|
public bool CheckCloth() |
|
{ |
|
bool can = true; |
|
Bag b = SelectedObjs.selectedCharacters[0].GetComponent<Bag>(); |
|
if (CheckHasCloth(b.EquipList)>0) |
|
{//当前消防员有服装 |
|
if (CheckHasCloth(selectEquips)>0) |
|
{ |
|
can = false; |
|
ResourceLoadWindow.Instance.LoadTextHintWindow("背包中已有服装,不可再选择服装", 0.5f); |
|
} |
|
else |
|
{ |
|
can = true; |
|
} |
|
} |
|
else |
|
{ |
|
if (CheckHasCloth(selectEquips) > 1) |
|
{ |
|
can = false; |
|
ResourceLoadWindow.Instance.LoadTextHintWindow("只能选择一套服装", 0.5f); |
|
} |
|
else |
|
{ |
|
can = true; |
|
} |
|
} |
|
return can; |
|
} |
|
/// <summary> |
|
/// 检查list里有几件服装 |
|
/// </summary> |
|
/// <param name="lists"></param> |
|
/// <returns></returns> |
|
private int CheckHasCloth(List<Equip> lists) |
|
{ |
|
int num = 0; |
|
|
|
for (int i = 0; i < lists.Count; i++) |
|
{ |
|
if (lists[i].Name== "隔热防护服" || |
|
lists[i].Name == "消防避火服" || |
|
lists[i].Name == "消防防化服") |
|
{ |
|
num++; |
|
} |
|
} |
|
return num; |
|
} |
|
IEnumerator WaitClose(float time) |
|
{ |
|
yield return new WaitForSeconds(time); |
|
Close(); |
|
} |
|
}
|
|
|