Files
HX_MapEditor/Assets/Scripts/System/MonstersConfig.cs

219 lines
8.2 KiB
C#
Raw Normal View History

2025-06-14 13:46:24 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
namespace HxGame.Data
{
//ˢ<><CBA2>ģʽ
public enum CreateMonsterMode
{
Death, //<2F><><EFBFBD><EFBFBD>
FixedTime, //<2F>̶<EFBFBD>ʱ<EFBFBD><CAB1>ˢ<EFBFBD><CBA2>
}
//Ѳ<><D1B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public enum PatrolMode
{
Range, //<2F><>Χ
Path, //·<><C2B7>
}
public class MonstersConfig
{
public class MonsterConfig
{
public int itemIdx;
public int groupId; //<2F>Ƿ<EFBFBD><C7B7>Ƿ<EFBFBD><C7B7><EFBFBD>
public int id;
public CreateMonsterMode createMode; //ˢ<><CBA2>ģʽ
public PatrolMode patrolMode; //Ѳ<><D1B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public Vector2Int pos; //ˢ<><CBA2><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
public int radius; //<2F>
public int num; //<2F><><EFBFBD><EFBFBD>
public int delayTime; //ˢ<><CBA2>ʱ<EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>ģʽʹ<CABD><CAB9>
public string strTime; //<2F><>ʽ <20><><EFBFBD><EFBFBD><><CAB1> 1,10:20 ÿ<><C3BF>10<31><30>20
public List<string> paths = new List<string>();
public MonsterConfig()
{
groupId = -1;
delayTime = 0;
strTime = string.Empty;
}
}
public List<MonsterConfig> monsterConfigs = new List<MonsterConfig>();
2025-07-18 22:28:40 +08:00
public void ClearAll()
{
monsterConfigs.Clear();
}
2025-06-14 13:46:24 +08:00
public void SaveXML(int mapId)
{
string path = PathUtil.GetXmlPath(mapId, "MonstersConfig");
string tmp = path.Substring(0, path.LastIndexOf('/'));
if (!Directory.Exists(tmp))
Directory.CreateDirectory(tmp);
if (File.Exists(path))
File.Delete(path);
XmlDocument xml = new XmlDocument();
XmlDeclaration node = xml.CreateXmlDeclaration("1.0", "utf-8", "no");
xml.AppendChild(node);
XmlElement Root = xml.CreateElement("config");
xml.AppendChild(Root);
XmlElement xmlSettings = xml.CreateElement("Settings");//<2F>ڵ<EFBFBD>Settings
xml.DocumentElement.AppendChild(xmlSettings);
xmlSettings.SetAttribute("mapID", mapId.ToString());
XmlElement MonsterAreas = xml.CreateElement("MonsterAreas");//<2F>ڵ<EFBFBD>Settings
xml.DocumentElement.AppendChild(MonsterAreas);
SaveMonsterAreas(xml, MonsterAreas);
xml.Save(path);
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>óɹ<C3B3>");
}
private void SaveMonsterAreas(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for (int i = 0; i < monsterConfigs.Count; i++)
{
child = xml.CreateElement("MonsterArea");
child.SetAttribute("itemIdx", monsterConfigs[i].itemIdx.ToString());
child.SetAttribute("groupId", monsterConfigs[i].groupId.ToString());
child.SetAttribute("id", monsterConfigs[i].id.ToString());
child.SetAttribute("createMode", ((int)(monsterConfigs[i].createMode)).ToString());
child.SetAttribute("patrolMode", ((int)(monsterConfigs[i].patrolMode)).ToString());
child.SetAttribute("indexX", monsterConfigs[i].pos.x.ToString());
child.SetAttribute("indexY", monsterConfigs[i].pos.y.ToString());
child.SetAttribute("radius", monsterConfigs[i].radius.ToString());
child.SetAttribute("num", monsterConfigs[i].num.ToString());
child.SetAttribute("delayTime", monsterConfigs[i].delayTime.ToString());
if(monsterConfigs[i].createMode == CreateMonsterMode.Death)
{
child.SetAttribute("day", "0");
child.SetAttribute("time", "0");
}
else
{
string[] tmp = monsterConfigs[i].strTime.Split(',');
child.SetAttribute("day", tmp[0]);
child.SetAttribute("time", tmp[1]);
}
XmlElement MonsterPaths = xml.CreateElement("MonsterPaths");
for (int m = 0; m < monsterConfigs[i].paths.Count; m++)
{
XmlElement path = xml.CreateElement($"Path{m}");
path.SetAttribute("pos", monsterConfigs[i].paths[m]);
MonsterPaths.AppendChild(path);
}
child.AppendChild(MonsterPaths);
xmlElement.AppendChild(child);
}
}
public bool LoadXML(int mapId)
{
string path = PathUtil.GetXmlPath(mapId, "MonstersConfig");
if (!File.Exists(path))
{
UIWindow.Instance.ShowMessage(<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
return false;
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNode xmlRoot = xmlDocument.SelectSingleNode("config");
XmlNode xmlSettings = xmlRoot.SelectSingleNode("Settings");
int id = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapID").Value);
if (id != mapId)
{
UIWindow.Instance.ShowMessage("<22><>ͼID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MonstersConfig<69><67><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
return false;
}
XmlNode xmlMonsterAreas = xmlRoot.SelectSingleNode("MonsterAreas");
LoadMonsterAreas(xmlMonsterAreas);
return true;
}
private void LoadMonsterAreas(XmlNode xmlNodes)
{
if (xmlNodes == null)
return;
XmlNode xmlNode = null;
XmlNodeList xmlNodeList = xmlNodes.ChildNodes;
int x, y;
for (int i = 0; i < xmlNodeList.Count; i++)
{
xmlNode = xmlNodeList.Item(i);
if (xmlNode.Name != "MonsterArea")
continue;
MonsterConfig mc = new MonsterConfig();
if (xmlNode.Attributes.GetNamedItem("itemIdx") == null)
mc.itemIdx = -1;
else
mc.itemIdx = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("itemIdx").Value);
if (xmlNode.Attributes.GetNamedItem("groupId") == null)
mc.groupId = -1;
else
mc.groupId = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("groupId").Value);
mc.id = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("id").Value);
mc.createMode = (CreateMonsterMode)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("createMode").Value);
if(xmlNode.Attributes.GetNamedItem("patrolMode") != null)
mc.patrolMode = (PatrolMode)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("patrolMode").Value);
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
mc.pos = new Vector2Int(x, y);
mc.radius = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("radius").Value);
mc.num = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("num").Value);
mc.delayTime = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("delayTime").Value);
if (mc.createMode == CreateMonsterMode.FixedTime)
{
string day = xmlNode.Attributes.GetNamedItem("day").Value;
string time = xmlNode.Attributes.GetNamedItem("time").Value;
mc.strTime = $"{day},{time}";
}
XmlNode xmlPaths = xmlNode.SelectSingleNode("MonsterPaths");
if (xmlPaths != null)
{
XmlNodeList xmlPathList = xmlPaths.ChildNodes;
for (int m = 0; m < xmlPathList.Count; m++)
{
XmlNode xmlPath = xmlPathList.Item(m);
if (xmlPath.Name != $"Path{m}")
continue;
string path = xmlPath.Attributes.GetNamedItem("pos").Value;
mc.paths.Add(path);
}
}
monsterConfigs.Add(mc);
}
}
}
}