第一次提交
This commit is contained in:
140
Assets/Scripts/System/MapRegions.cs
Normal file
140
Assets/Scripts/System/MapRegions.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
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");//<2F>ڵ<EFBFBD>Settings
|
||||
xml.DocumentElement.AppendChild(Settings);
|
||||
SaveMapInfoConfig(mapId, Settings);
|
||||
|
||||
XmlElement Regions = xml.CreateElement("Regions");//<2F>ڵ<EFBFBD>Settings
|
||||
xml.DocumentElement.AppendChild(Regions);
|
||||
SaveRegions(xml, Regions);
|
||||
|
||||
xml.Save(path);
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>óɹ<C3B3>");
|
||||
}
|
||||
|
||||
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("û<><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");
|
||||
XmlNode xmlRegions = xmlRoot.SelectSingleNode("Regions");
|
||||
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>MapRegions<6E><73><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user