160 lines
5.1 KiB
C#
160 lines
5.1 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Xml;
|
||
using UnityEngine;
|
||
|
||
namespace HxGame.Data
|
||
{
|
||
public enum Direction
|
||
{
|
||
RightUp, //右上
|
||
Right, //右
|
||
RightDown, //右下
|
||
Down, //下
|
||
LeftDown, //左下
|
||
Left, //左
|
||
LeftUp, //左上
|
||
Up, //上
|
||
}
|
||
|
||
|
||
public class NpcConfig
|
||
{
|
||
public int id;
|
||
public Vector2Int pos; //刷怪中心点
|
||
public Direction dir;
|
||
public List<string> paths = new List<string>();
|
||
}
|
||
|
||
public class NpcsConfig
|
||
{
|
||
public List<NpcConfig> npcConfigs = new List<NpcConfig>();
|
||
|
||
public void SaveXML(int mapId)
|
||
{
|
||
string path = PathUtil.GetXmlPath(mapId, "NpcsConfig");
|
||
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 Npcs = xml.CreateElement("Npcs");//节点Settings
|
||
xml.DocumentElement.AppendChild(Npcs);
|
||
|
||
SaveNpcs(xml, Npcs);
|
||
|
||
xml.Save(path);
|
||
UIWindow.Instance.ShowMessage("保存NPC配置成功");
|
||
}
|
||
|
||
private void SaveNpcs(XmlDocument xml, XmlElement xmlElement)
|
||
{
|
||
XmlElement child = null;
|
||
for (int i = 0; i < npcConfigs.Count; i++)
|
||
{
|
||
child = xml.CreateElement("Npc");
|
||
child.SetAttribute("id", npcConfigs[i].id.ToString());
|
||
child.SetAttribute("indexX", npcConfigs[i].pos.x.ToString());
|
||
child.SetAttribute("indexY", npcConfigs[i].pos.y.ToString());
|
||
child.SetAttribute("dir", ((int)npcConfigs[i].dir).ToString());
|
||
XmlElement NpcPaths = xml.CreateElement("NpcPaths");
|
||
for(int m=0; m<npcConfigs[i].paths.Count; m++)
|
||
{
|
||
XmlElement path = xml.CreateElement($"Path{m}");
|
||
path.SetAttribute("pos", npcConfigs[i].paths[m]);
|
||
NpcPaths.AppendChild(path);
|
||
}
|
||
|
||
child.AppendChild(NpcPaths);
|
||
xmlElement.AppendChild(child);
|
||
}
|
||
}
|
||
|
||
public bool LoadXML(int mapId)
|
||
{
|
||
string path = PathUtil.GetXmlPath(mapId, "NpcsConfig");
|
||
if (!File.Exists(path))
|
||
{
|
||
UIWindow.Instance.ShowMessage("没有找到NPC配置文件");
|
||
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错误,请检查NpcsConfig配置文件");
|
||
return false;
|
||
}
|
||
|
||
XmlNode Npcs = xmlRoot.SelectSingleNode("Npcs");
|
||
LoadNpcs(Npcs);
|
||
|
||
return true;
|
||
}
|
||
|
||
private void LoadNpcs(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 != "Npc")
|
||
continue;
|
||
|
||
NpcConfig nc = new NpcConfig();
|
||
nc.id = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("id").Value);
|
||
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
|
||
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
|
||
nc.pos = new Vector2Int(x, y);
|
||
nc.dir = (Direction)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("dir").Value);
|
||
|
||
XmlNode xmlPaths = xmlNode.SelectSingleNode("NpcPaths");
|
||
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;
|
||
nc.paths.Add(path);
|
||
}
|
||
}
|
||
|
||
npcConfigs.Add(nc);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|