279 lines
9.1 KiB
C#
279 lines
9.1 KiB
C#
using HxGame.Data;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using static HxGame.Data.MonstersConfig;
|
||
using static System.Net.Mime.MediaTypeNames;
|
||
|
||
/*
|
||
* 如果坐标点一样
|
||
* 算是一个分页
|
||
*
|
||
*/
|
||
|
||
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;
|
||
|
||
public MonstersConfig monstersConfig = new();
|
||
private void Awake()
|
||
{
|
||
itemIdx = 0;
|
||
btnClose.onClick.AddListener(OnClickClose);
|
||
btnAddGroup.onClick.AddListener(OnAddGroup);
|
||
txtMonsterID.onEndEdit.AddListener(OnEditorMonsterId);
|
||
txtName.onEndEdit.AddListener(OnEditorMonsterName);
|
||
txtRadius.onEndEdit.AddListener(OnEditorMonsterRadius);
|
||
txtCount.onEndEdit.AddListener(OnEditorMonsterCount);
|
||
}
|
||
|
||
private void OnEditorMonsterCount(string arg0)
|
||
{
|
||
if (!int.TryParse(arg0, out var intValue))
|
||
{
|
||
UIWindow.Instance.ShowMessage("格式不正确");
|
||
return;
|
||
}
|
||
_curMonsterItem.monster.id = intValue;
|
||
_curMonsterItem.RefreshItem();
|
||
}
|
||
|
||
private void OnEditorMonsterRadius(string arg0)
|
||
{
|
||
if (!int.TryParse(arg0, out var intValue))
|
||
{
|
||
UIWindow.Instance.ShowMessage("格式不正确");
|
||
return;
|
||
}
|
||
_curMonsterItem.monster.radius = intValue;
|
||
_curMonsterItem.RefreshItem();
|
||
}
|
||
|
||
private void OnEditorMonsterName(string arg0)
|
||
{
|
||
if (_curMonsterItem == null) return;
|
||
_curMonsterItem.RefreshItem();
|
||
}
|
||
|
||
private void OnEditorMonsterId(string arg0)
|
||
{
|
||
if (_curMonsterItem == null) return;
|
||
if (!int.TryParse(arg0, out var intValue))
|
||
{
|
||
UIWindow.Instance.ShowMessage("格式不正确");
|
||
return;
|
||
}
|
||
_curMonsterItem.monster.id = intValue;
|
||
_curMonsterItem.RefreshItem();
|
||
}
|
||
|
||
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;
|
||
}
|
||
var gropcfg = monstersConfig.monsterConfigs.Find((monster) => monster.itemIdx == _curMonsterItem.monster.itemIdx);
|
||
if (gropcfg == null) return;
|
||
int index = monstersConfig.monsterConfigs.IndexOf(gropcfg);
|
||
MonstersConfig.MonsterConfig newmonster = new MonstersConfig.MonsterConfig();
|
||
monstersConfig.monsterConfigs.Insert(index + 1, newmonster);
|
||
foreach (var prop in typeof(MonsterConfig).GetFields())
|
||
{
|
||
prop.SetValue(newmonster, prop.GetValue(gropcfg));
|
||
}
|
||
newmonster.itemIdx = -1;
|
||
newmonster.groupId = gropcfg.itemIdx;
|
||
AddItem(newmonster);
|
||
}
|
||
public void AddItem()
|
||
{
|
||
Cleanup();
|
||
MonstersConfig.MonsterConfig monster = new MonstersConfig.MonsterConfig();
|
||
monster.itemIdx = ++itemIdx;
|
||
monster.groupId = -1;
|
||
monstersConfig.monsterConfigs.Insert(0, monster);
|
||
AddItem(monster);
|
||
}
|
||
|
||
public void LoadMonsterConfig(int mapId)
|
||
{
|
||
monstersConfig.ClearAll();
|
||
if (!monstersConfig.LoadXML(mapId))
|
||
return;
|
||
for (int i = 0; i < monstersConfig.monsterConfigs.Count; i++)
|
||
{
|
||
MonstersConfig.MonsterConfig monster = monstersConfig.monsterConfigs[i];
|
||
if (monster.itemIdx == -1) continue;
|
||
var itemMonster = AddItem(monster);
|
||
if (itemMonster == null) continue;
|
||
var groupList = monstersConfig.monsterConfigs.Where(item => item.groupId == monster.itemIdx).ToList();
|
||
foreach (var item in groupList)
|
||
{
|
||
AddItem(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
public UIMonsterItem AddItem(MonstersConfig.MonsterConfig monster)
|
||
{
|
||
UnityEngine.Object obj = Resources.Load("Prefabs/monsterItem");
|
||
if (obj == null)
|
||
{
|
||
UIWindow.Instance.ShowMessage("monsterItem.prefab失败");
|
||
return null;
|
||
}
|
||
var cfgindex = monstersConfig.monsterConfigs.IndexOf(monster);
|
||
itemIdx = Math.Max(monster.itemIdx, itemIdx);
|
||
GameObject go = Instantiate(obj) as GameObject; ;
|
||
go.transform.SetParent(itemParent, false);
|
||
go.transform.SetSiblingIndex(cfgindex);
|
||
UIMonsterItem item = go.transform.Find("monsterSubItem").GetComponent<UIMonsterItem>();
|
||
item.editCellType = MapManager.EditCellType.MonsterArea;
|
||
item.SetItem(monster);
|
||
item.OnClick = OnClickItem;
|
||
item.OnCopyItem = OnCopyItem;
|
||
_curMonsterItem = item;
|
||
return item;
|
||
}
|
||
public void OnClickItem(UIMonsterItem item)
|
||
{
|
||
//先保存上一条信息
|
||
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;
|
||
}
|
||
public void OnCopyItem(UIMonsterItem item)
|
||
{
|
||
var gropcfg = monstersConfig.monsterConfigs.Find((monster) => monster.itemIdx == item.monster.itemIdx);
|
||
if (gropcfg == null) return;
|
||
int index = monstersConfig.monsterConfigs.IndexOf(gropcfg);
|
||
if (item.monster.itemIdx == -1)
|
||
{
|
||
MonstersConfig.MonsterConfig newmonster = new MonstersConfig.MonsterConfig();
|
||
monstersConfig.monsterConfigs.Insert(index + 1, newmonster);
|
||
foreach (var prop in typeof(MonsterConfig).GetFields())
|
||
{
|
||
prop.SetValue(newmonster, prop.GetValue(gropcfg));
|
||
}
|
||
newmonster.itemIdx = -1;
|
||
newmonster.groupId = gropcfg.itemIdx;
|
||
AddItem(newmonster);
|
||
}
|
||
else {
|
||
//先拷贝自己,再拷贝组内
|
||
MonstersConfig.MonsterConfig newmonster = new MonstersConfig.MonsterConfig();
|
||
monstersConfig.monsterConfigs.Insert(index,newmonster);
|
||
foreach (var prop in typeof(MonsterConfig).GetFields())
|
||
{
|
||
prop.SetValue(newmonster, prop.GetValue(item.monster));
|
||
}
|
||
newmonster.itemIdx = ++itemIdx;
|
||
newmonster.groupId = -1;
|
||
AddItem(newmonster);
|
||
var grops = monstersConfig.monsterConfigs.FindAll((tt) => tt.groupId == item.monster.itemIdx);
|
||
foreach (var gropItem in grops)
|
||
{
|
||
MonsterConfig config = new MonsterConfig();
|
||
monstersConfig.monsterConfigs.Insert(++index, config);
|
||
foreach (var prop in typeof(MonsterConfig).GetFields())
|
||
{
|
||
prop.SetValue(config, prop.GetValue(gropItem));
|
||
}
|
||
config.groupId = gropItem.itemIdx;
|
||
config.itemIdx = -1;
|
||
AddItem(config);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void SaveMonsterConfig()
|
||
{
|
||
MonstersConfig mc = new MonstersConfig();
|
||
int mapId = MapManager.Instance._curOpenMapId;
|
||
mc.SaveXML(mapId);
|
||
}
|
||
|
||
public void RemoveAllGrop(MonsterConfig config)
|
||
{
|
||
if (config.itemIdx == -1)
|
||
return;
|
||
foreach (Transform child in svMonsters.content)
|
||
{
|
||
UIMonsterItem item = child.GetComponentInChildren<UIMonsterItem>();
|
||
if (item != null && item.monster.groupId == config.itemIdx)
|
||
{
|
||
item.DestoryGrop();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void RemoveAll()
|
||
{
|
||
int size = itemParent.childCount;
|
||
for (int i=0; i<size; i++)
|
||
{
|
||
DestroyImmediate(itemParent.GetChild(0).gameObject);
|
||
}
|
||
itemIdx = 0;
|
||
}
|
||
}
|