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, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
Right, //<2F><>
|
|||
|
|
RightDown, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
Down, //<2F><>
|
|||
|
|
LeftDown, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
Left, //<2F><>
|
|||
|
|
LeftUp, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
Up, //<2F><>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class NpcConfig
|
|||
|
|
{
|
|||
|
|
public int id;
|
|||
|
|
public Vector2Int pos; //ˢ<><CBA2><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
|
|||
|
|
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");//<2F>ڵ<EFBFBD>Settings
|
|||
|
|
xml.DocumentElement.AppendChild(xmlSettings);
|
|||
|
|
xmlSettings.SetAttribute("mapID", mapId.ToString());
|
|||
|
|
|
|||
|
|
XmlElement Npcs = xml.CreateElement("Npcs");//<2F>ڵ<EFBFBD>Settings
|
|||
|
|
xml.DocumentElement.AppendChild(Npcs);
|
|||
|
|
|
|||
|
|
SaveNpcs(xml, Npcs);
|
|||
|
|
|
|||
|
|
xml.Save(path);
|
|||
|
|
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>NPC<50><43><EFBFBD>óɹ<C3B3>");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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("û<><C3BB><EFBFBD>ҵ<EFBFBD>NPC<50><43><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>NpcsConfig<69><67><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|