399 lines
12 KiB
C#
399 lines
12 KiB
C#
using HxGame.Data;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/*
|
||
* 如果坐标点一样
|
||
* 算是一个分页
|
||
*
|
||
*/
|
||
|
||
public class UIMonsterPanel : MonoBehaviour
|
||
{
|
||
[HideInInspector]
|
||
public InputField curActiveInput;
|
||
|
||
[HideInInspector]
|
||
public List<string> curActiveList;
|
||
|
||
public Button btnClose;
|
||
public Button btnAddMonster;
|
||
public Button btnAddGroup;
|
||
public Button btnDeleteGroup;
|
||
|
||
#region MonsterInfo
|
||
public InputField txtMonsterID; //怪物ID
|
||
public InputField txtName; //怪物名称
|
||
public Dropdown dropRefreshType; //刷怪类型
|
||
public InputField txtRefreshTime; //刷怪时间(毫秒)
|
||
public InputField txtCount; //刷怪数量
|
||
public InputField txtRadius; //刷怪范围
|
||
public Dropdown dropPatrol; //巡逻类型
|
||
public InputField txtPath; //巡逻路径
|
||
#endregion
|
||
|
||
public ScrollRect svMonsters;
|
||
public Transform itemParent;
|
||
public int itemIdx;
|
||
|
||
private UIMonsterItem _curMonsterItem;
|
||
//private Dictionary<int, List<UIMonsterItem>> _dicGroupItem;
|
||
private Dictionary<int, UIMonsterItem> _dicGroupItem; //分组归属的item
|
||
|
||
private void Awake()
|
||
{
|
||
itemIdx = 0;
|
||
//_dicGroupItem = new Dictionary<int, List<UIMonsterItem>>();
|
||
_dicGroupItem = new Dictionary<int, UIMonsterItem>();
|
||
btnClose.onClick.AddListener(OnClickClose);
|
||
btnAddGroup.onClick.AddListener(OnAddGroup);
|
||
btnDeleteGroup.onClick.AddListener(OnDeleteGroup);
|
||
}
|
||
|
||
private void OnClickClose()
|
||
{
|
||
UIWindow.Instance.uiEditMapConfig.togEditMonsters.isOn = false;
|
||
}
|
||
|
||
private void Cleanup()
|
||
{
|
||
txtMonsterID.text = string.Empty;
|
||
txtName.text = string.Empty;
|
||
txtRefreshTime.text = string.Empty;
|
||
txtCount.text = string.Empty;
|
||
txtRadius.text = string.Empty;
|
||
txtPath.text = string.Empty;
|
||
}
|
||
|
||
//添加分组,将item赋予分组ID
|
||
private void OnAddGroup()
|
||
{
|
||
if (_curMonsterItem == null)
|
||
return;
|
||
|
||
if(_curMonsterItem.groupId > 0)
|
||
{
|
||
UIWindow.Instance.ShowMessage("不能在分页上创建分页");
|
||
return;
|
||
}
|
||
|
||
_curMonsterItem.transform.parent.GetComponent<RectTransform>().sizeDelta += Vector2Int.up * 30;
|
||
|
||
UnityEngine.Object obj = Resources.Load("Prefabs/monsterSubItem");
|
||
if (obj == null)
|
||
{
|
||
UIWindow.Instance.ShowMessage("monsterSubItem.prefab失败");
|
||
return;
|
||
}
|
||
|
||
GameObject go = Instantiate(obj) as GameObject; ;
|
||
go.transform.SetParent(_curMonsterItem.transform.parent, false);
|
||
UIMonsterItem item = go.GetComponent<UIMonsterItem>();
|
||
item.SetGroup(_curMonsterItem.monster.itemIdx); //将item的索引设置为groupId
|
||
MonstersConfig.MonsterConfig monster = new MonstersConfig.MonsterConfig();
|
||
monster.groupId = _curMonsterItem.monster.itemIdx;
|
||
monster.itemIdx = -1;
|
||
item.SetItem(monster);
|
||
item.OnClick = OnClickItem;
|
||
item.OnCopyItem = OnCopyItem;
|
||
item.btnClickSelf.Select();
|
||
|
||
|
||
AddGroupMap(_curMonsterItem.monster.itemIdx, _curMonsterItem);
|
||
}
|
||
|
||
private void OnAddGroup(MonstersConfig.MonsterConfig monster)
|
||
{
|
||
if (_curMonsterItem == null)
|
||
return;
|
||
|
||
_curMonsterItem.transform.parent.GetComponent<RectTransform>().sizeDelta += Vector2Int.up * 30;
|
||
|
||
UnityEngine.Object obj = Resources.Load("Prefabs/monsterSubItem");
|
||
if (obj == null)
|
||
{
|
||
UIWindow.Instance.ShowMessage("monsterSubItem.prefab失败");
|
||
return;
|
||
}
|
||
|
||
GameObject go = Instantiate(obj) as GameObject; ;
|
||
go.transform.SetParent(_curMonsterItem.transform.parent, false);
|
||
UIMonsterItem item = go.GetComponent<UIMonsterItem>();
|
||
item.SetGroup(_curMonsterItem.monster.itemIdx); //将item的索引设置为groupId
|
||
monster.groupId = _curMonsterItem.monster.itemIdx;
|
||
item.SetItem(monster);
|
||
item.OnClick = OnClickItem;
|
||
item.OnCopyItem = OnCopyItem;
|
||
|
||
AddGroupMap(_curMonsterItem.monster.itemIdx, _curMonsterItem);
|
||
}
|
||
|
||
private void OnDeleteGroup()
|
||
{
|
||
if (_curMonsterItem == null)
|
||
return;
|
||
|
||
_curMonsterItem.transform.parent.GetComponent<RectTransform>().sizeDelta -= Vector2Int.up * 30;
|
||
|
||
DestroyImmediate(_curMonsterItem.gameObject);
|
||
|
||
_curMonsterItem = null;
|
||
}
|
||
|
||
public void AddItem()
|
||
{
|
||
Cleanup();
|
||
UnityEngine.Object obj = Resources.Load("Prefabs/monsterItem");
|
||
if (obj == null)
|
||
{
|
||
UIWindow.Instance.ShowMessage("monsterItem.prefab失败");
|
||
return;
|
||
}
|
||
|
||
GameObject go = Instantiate(obj) as GameObject; ;
|
||
go.transform.SetParent(itemParent, false);
|
||
UIMonsterItem item = go.transform.Find("monsterSubItem").GetComponent<UIMonsterItem>();
|
||
|
||
MonstersConfig.MonsterConfig monster = new MonstersConfig.MonsterConfig();
|
||
monster.itemIdx = ++itemIdx;
|
||
item.SetItem(monster);
|
||
item.OnClick = OnClickItem;
|
||
item.OnCopyItem = OnCopyItem;
|
||
_curMonsterItem = item;
|
||
item.btnClickSelf.Select();
|
||
}
|
||
|
||
public void LoadMonsterConfig(int mapId)
|
||
{
|
||
MonstersConfig mc = new MonstersConfig();
|
||
if (!mc.LoadXML(mapId))
|
||
return;
|
||
|
||
//怪物
|
||
for (int i = 0; i < mc.monsterConfigs.Count; i++)
|
||
{
|
||
MonstersConfig.MonsterConfig monster = mc.monsterConfigs[i];
|
||
if (_curMonsterItem != null && monster.groupId >= 0 && monster.groupId == _curMonsterItem.monster.itemIdx)
|
||
OnAddGroup(monster);
|
||
else
|
||
AddItem(monster);
|
||
}
|
||
}
|
||
|
||
public void AddItem(MonstersConfig.MonsterConfig monster)
|
||
{
|
||
UnityEngine.Object obj = Resources.Load("Prefabs/monsterItem");
|
||
if (obj == null)
|
||
{
|
||
UIWindow.Instance.ShowMessage("monsterItem.prefab失败");
|
||
return;
|
||
}
|
||
|
||
if (monster.itemIdx < 0)
|
||
monster.itemIdx = itemIdx++;
|
||
else
|
||
itemIdx = monster.itemIdx;
|
||
|
||
GameObject go = Instantiate(obj) as GameObject; ;
|
||
go.transform.SetParent(itemParent, false);
|
||
UIMonsterItem item = go.transform.Find("monsterSubItem").GetComponent<UIMonsterItem>();
|
||
//item.SetItem(itemIdx++, monster);
|
||
item.SetItem(monster);
|
||
item.OnClick = OnClickItem;
|
||
item.OnCopyItem = OnCopyItem;
|
||
_curMonsterItem = item;
|
||
}
|
||
|
||
private void SaveItem()
|
||
{
|
||
if (_curMonsterItem == null)
|
||
return;
|
||
|
||
if (string.IsNullOrEmpty(txtMonsterID.text))
|
||
return;
|
||
|
||
//分组的不保存位置
|
||
if(_curMonsterItem.groupId < 0)
|
||
{
|
||
string[] tmp = _curMonsterItem.txtPos.text.Split(',');
|
||
if (tmp.Length != 2)
|
||
return;
|
||
|
||
int x = Convert.ToInt32(tmp[0]);
|
||
int y = Convert.ToInt32(tmp[1]);
|
||
_curMonsterItem.monster.pos = new Vector2Int(x, y);
|
||
}
|
||
else
|
||
{
|
||
if(_dicGroupItem.ContainsKey(_curMonsterItem.groupId))
|
||
{
|
||
string[] tmp = _dicGroupItem[_curMonsterItem.groupId].txtPos.text.Split(',');
|
||
if (tmp.Length != 2)
|
||
return;
|
||
|
||
int x = Convert.ToInt32(tmp[0]);
|
||
int y = Convert.ToInt32(tmp[1]);
|
||
|
||
_curMonsterItem.monster.pos = new Vector2Int(x, y);
|
||
}
|
||
}
|
||
|
||
_curMonsterItem.monster.id = Convert.ToInt32(txtMonsterID.text);
|
||
_curMonsterItem.monster.createMode = (CreateMonsterMode)dropRefreshType.value;
|
||
if (_curMonsterItem.monster.createMode == CreateMonsterMode.Death)
|
||
_curMonsterItem.monster.delayTime = Convert.ToInt32(txtRefreshTime.text);
|
||
else
|
||
_curMonsterItem.monster.strTime = txtRefreshTime.text;
|
||
|
||
_curMonsterItem.monster.num = Convert.ToInt32(txtCount.text);
|
||
_curMonsterItem.monster.radius = Convert.ToInt32(txtRadius.text);
|
||
_curMonsterItem.monster.patrolMode = (PatrolMode)dropPatrol.value;
|
||
_curMonsterItem.RefreshItem();
|
||
}
|
||
|
||
public void OnClickItem(UIMonsterItem item)
|
||
{
|
||
//先保存上一条信息
|
||
SaveItem();
|
||
Cleanup();
|
||
|
||
_curMonsterItem = item;
|
||
MonstersConfig.MonsterConfig monster = _curMonsterItem.monster;
|
||
txtMonsterID.text = monster.id.ToString();
|
||
dropRefreshType.value = (int)monster.createMode;
|
||
if (monster.createMode == CreateMonsterMode.Death)
|
||
txtRefreshTime.text = monster.delayTime.ToString();
|
||
else
|
||
txtRefreshTime.text = monster.strTime;
|
||
txtCount.text = monster.num.ToString();
|
||
txtRadius.text = monster.radius.ToString();
|
||
dropPatrol.value = (int)monster.patrolMode;
|
||
|
||
for (int i = 0; i < monster.paths.Count; i++)
|
||
{
|
||
string[] pos = monster.paths[i].Split('_');
|
||
int x = Convert.ToInt32(pos[0]);
|
||
int y = Convert.ToInt32(pos[1]);
|
||
txtPath.text += $"[{monster.paths[i].Replace('_', ',')}]";
|
||
MapManager.Instance.AddMonsterPathSize();
|
||
MapManager.Instance.CreateSpecialPoint(x, y, MapManager.EditCellType.MonsterPath);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
private void CopyItem(MonstersConfig.MonsterConfig monster)
|
||
{
|
||
MonstersConfig.MonsterConfig m = new MonstersConfig.MonsterConfig();
|
||
m.itemIdx = ++itemIdx;
|
||
m.id = monster.id;
|
||
m.num = monster.num;
|
||
m.radius = monster.radius;
|
||
m.pos = monster.pos;
|
||
m.createMode = monster.createMode;
|
||
m.delayTime = monster.delayTime;
|
||
m.strTime = monster.strTime;
|
||
m.patrolMode = monster.patrolMode;
|
||
m.paths = monster.paths;
|
||
AddItem(m);
|
||
}
|
||
|
||
//public void OnCopyItem(MonstersConfig.MonsterConfig monster)
|
||
public void OnCopyItem(UIMonsterItem item)
|
||
{
|
||
SaveItem();
|
||
CopyItem(item.monster);
|
||
|
||
if (!_dicGroupItem.ContainsKey(item.monster.itemIdx))
|
||
return;
|
||
|
||
var count = item.transform.parent.childCount;
|
||
if (count == 1)
|
||
return;
|
||
|
||
//忽略第一条
|
||
for (int i = 1; i < count; i++)
|
||
{
|
||
var tf = item.transform.parent.GetChild(i);
|
||
UIMonsterItem item_new = tf.GetComponent<UIMonsterItem>();
|
||
MonstersConfig.MonsterConfig m = new MonstersConfig.MonsterConfig();
|
||
m.itemIdx = -1;
|
||
m.id = item_new.monster.id;
|
||
m.num = item_new.monster.num;
|
||
m.radius = item_new.monster.radius;
|
||
m.pos = item_new.monster.pos;
|
||
m.createMode = item_new.monster.createMode;
|
||
m.delayTime = item_new.monster.delayTime;
|
||
m.strTime = item_new.monster.strTime;
|
||
m.patrolMode = item_new.monster.patrolMode;
|
||
m.paths = item_new.monster.paths;
|
||
|
||
//复制
|
||
OnAddGroup(m);
|
||
}
|
||
}
|
||
|
||
public void SaveMonsterConfig()
|
||
{
|
||
if(_curMonsterItem != null)
|
||
OnClickItem(_curMonsterItem);
|
||
|
||
//刷怪
|
||
int x, y;
|
||
MonstersConfig mc = new MonstersConfig();
|
||
|
||
for (int i = 0; i < itemParent.childCount; i++)
|
||
{
|
||
var tf = itemParent.GetChild(i);
|
||
for(int j=0; j<tf.childCount; j++)
|
||
{
|
||
UIMonsterItem item = tf.GetChild(j).GetComponent<UIMonsterItem>();
|
||
//如果是分组item,复制pos
|
||
if(item.groupId > 0)
|
||
{
|
||
if (_dicGroupItem.ContainsKey(item.groupId))
|
||
{
|
||
string[] tmp = _dicGroupItem[item.groupId].txtPos.text.Split(',');
|
||
if (tmp.Length != 2)
|
||
return;
|
||
|
||
x = Convert.ToInt32(tmp[0]);
|
||
y = Convert.ToInt32(tmp[1]);
|
||
|
||
item.monster.pos = new Vector2Int(x, y);
|
||
}
|
||
}
|
||
|
||
mc.monsterConfigs.Add(item.monster);
|
||
}
|
||
|
||
}
|
||
|
||
int mapId = MapManager.Instance._curOpenMapId;
|
||
mc.SaveXML(mapId);
|
||
}
|
||
|
||
private void AddGroupMap(int groupId, UIMonsterItem item)
|
||
{
|
||
if (_dicGroupItem.ContainsKey(groupId))
|
||
return;
|
||
|
||
_dicGroupItem.Add(groupId, item);
|
||
}
|
||
|
||
public void RemoveAll()
|
||
{
|
||
int size = itemParent.childCount;
|
||
for (int i=0; i<size; i++)
|
||
{
|
||
DestroyImmediate(itemParent.GetChild(0).gameObject);
|
||
}
|
||
|
||
_dicGroupItem?.Clear();
|
||
itemIdx = 0;
|
||
}
|
||
}
|