141 lines
4.8 KiB
C#
141 lines
4.8 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Xml;
|
||
using UnityEngine;
|
||
|
||
namespace HxGame.Data
|
||
{
|
||
public class Region
|
||
{
|
||
public int indexX;
|
||
public int indexY;
|
||
public string regionName;
|
||
}
|
||
|
||
|
||
[Serializable]
|
||
public class MapRegions
|
||
{
|
||
public int mapWidth;
|
||
public int mapHeight;
|
||
public int regionWidth;
|
||
public int regionHeight;
|
||
public Region[,] regions;
|
||
|
||
|
||
public void SaveXML(int mapId)
|
||
{
|
||
string path = PathUtil.GetXmlPath(mapId, "MapRegions");
|
||
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 Settings = xml.CreateElement("Settings");//节点Settings
|
||
xml.DocumentElement.AppendChild(Settings);
|
||
SaveMapInfoConfig(mapId, Settings);
|
||
|
||
XmlElement Regions = xml.CreateElement("Regions");//节点Settings
|
||
xml.DocumentElement.AppendChild(Regions);
|
||
SaveRegions(xml, Regions);
|
||
|
||
xml.Save(path);
|
||
UIWindow.Instance.ShowMessage("保存地图配置成功");
|
||
}
|
||
|
||
private void SaveMapInfoConfig(int mapId, XmlElement xmlSettings)
|
||
{
|
||
xmlSettings.SetAttribute("mapID", mapId.ToString());
|
||
xmlSettings.SetAttribute("mapWidth", mapWidth.ToString());
|
||
xmlSettings.SetAttribute("mapHeight", mapHeight.ToString());
|
||
xmlSettings.SetAttribute("regionWidth", regionWidth.ToString());
|
||
xmlSettings.SetAttribute("regionHeight", regionHeight.ToString());
|
||
}
|
||
|
||
private void SaveRegions(XmlDocument xml, XmlElement xmlRegions)
|
||
{
|
||
XmlElement child = null;
|
||
foreach(var region in regions)
|
||
{
|
||
child = xml.CreateElement("Region");
|
||
|
||
child.SetAttribute("IndexX", region.indexX.ToString());
|
||
child.SetAttribute("IndexY", region.indexY.ToString());
|
||
child.SetAttribute("regionName", region.regionName);
|
||
|
||
xmlRegions.AppendChild(child);
|
||
}
|
||
}
|
||
|
||
public bool LoadXML(int mapId)
|
||
{
|
||
string path = PathUtil.GetXmlPath(mapId, "MapRegions");
|
||
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");
|
||
XmlNode xmlRegions = xmlRoot.SelectSingleNode("Regions");
|
||
int id = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapID").Value);
|
||
if (id != mapId)
|
||
{
|
||
UIWindow.Instance.ShowMessage("地图ID错误,请检查MapRegions配置文件");
|
||
return false;
|
||
}
|
||
|
||
LoadMapInfoConfig(xmlSettings);
|
||
LoadRegions(xmlRegions);
|
||
|
||
return true;
|
||
}
|
||
|
||
private void LoadMapInfoConfig(XmlNode xmlSettings)
|
||
{
|
||
mapWidth = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapWidth").Value);
|
||
mapHeight = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapHeight").Value);
|
||
regionWidth = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionWidth").Value);
|
||
regionHeight = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("regionHeight").Value);
|
||
}
|
||
|
||
private void LoadRegions(XmlNode xmlRegions)
|
||
{
|
||
int row = mapWidth / MapManager.CELLSCALE / regionWidth;
|
||
int col = mapHeight / MapManager.CELLSCALE / regionHeight;
|
||
|
||
XmlNode xmlNode = null;
|
||
XmlNodeList xmlNodeList = xmlRegions.ChildNodes;
|
||
regions = new Region[row, col];
|
||
for (int i = 0; i < xmlNodeList.Count; i++)
|
||
{
|
||
xmlNode = xmlNodeList.Item(i);
|
||
if (xmlNode.Name != "Region")
|
||
continue;
|
||
|
||
Region r = new Region();
|
||
r.indexX = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("IndexX").Value);
|
||
r.indexY = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("IndexY").Value);
|
||
r.regionName = xmlNode.Attributes.GetNamedItem("regionName").Value;
|
||
regions[r.indexX, r.indexY] = r;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|