Files
HX_MapEditor/Assets/Scripts/System/MonstersConfig.cs
2025-07-18 22:28:40 +08:00

219 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
namespace HxGame.Data
{
//刷怪模式
public enum CreateMonsterMode
{
Death, //死亡
FixedTime, //固定时间刷怪
}
//巡逻类型
public enum PatrolMode
{
Range, //范围
Path, //路径
}
public class MonstersConfig
{
public class MonsterConfig
{
public int itemIdx;
public int groupId; //是否是分组
public int id;
public CreateMonsterMode createMode; //刷怪模式
public PatrolMode patrolMode; //巡逻类型
public Vector2Int pos; //刷怪中心点
public int radius; //半径
public int num; //数量
public int delayTime; //刷怪时间,死亡模式使用
public string strTime; //格式 天数,时间 1,10:20 每天10点20
public List<string> paths = new List<string>();
public MonsterConfig()
{
groupId = -1;
delayTime = 0;
strTime = string.Empty;
}
}
public List<MonsterConfig> monsterConfigs = new List<MonsterConfig>();
public void ClearAll()
{
monsterConfigs.Clear();
}
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");//节点Settings
xml.DocumentElement.AppendChild(xmlSettings);
xmlSettings.SetAttribute("mapID", mapId.ToString());
XmlElement MonsterAreas = xml.CreateElement("MonsterAreas");//节点Settings
xml.DocumentElement.AppendChild(MonsterAreas);
SaveMonsterAreas(xml, MonsterAreas);
xml.Save(path);
UIWindow.Instance.ShowMessage("保存怪物配置成功");
}
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("没有找到怪物配置文件");
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("地图ID错误请检查MonstersConfig配置文件");
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);
}
}
}
}