海淀天下城电子沙盘单机版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.3 KiB

4 years ago
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class DisasterManager : MonoBehaviour
{
public static DisasterInfo disaster;
public static List<FirstNodeObject> firstNodeObjects;
public static List<SecondNodeObject> secondNodeObjects;
public static bool updateFlag;
public static PlayModel model;
public static bool editorRight;
private void Awake()
{
//updateFlag = false;
//editorRight = true;//单机版
//editorRight = true;//联机版
}
private void Start()
{
/*disaster = new DisasterInfo();
disaster.Id = 111;
disaster.Name = "aaa";*/
MessageDispatcher.AddListener("DisasterUpdateCompelete", ResetData);//更新完成重置数据
firstNodeObjects = new List<FirstNodeObject>();
secondNodeObjects = new List<SecondNodeObject>();
InstantiateData();
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("DisasterUpdateCompelete", ResetData);//更新完成重置数据
}
private void ResetData(IMessage obj)
{
InstantiateData();
}
public void InstantiateData()
{
firstNodeObjects.Clear();
secondNodeObjects.Clear();
string filepath = GetNodeListXML();
if (!File.Exists(filepath))
{
CreateNodeListXML();
}
else
{
LoadNodeListXML();
}
MessageDispatcher.SendMessage("DataInitializationComplete");
}
/// <summary>
/// 获取节点存储文件路径
/// </summary>
/// <returns></returns>
public static string GetNodeListXML()
{
return Application.dataPath + @"/Data/" + disaster.Id + "/File/NodeList.xml";
}
/// <summary>
/// 创建节点存储文件
/// </summary>
private void CreateNodeListXML()
{
string path = Application.dataPath + @"/Data/" + disaster.Id + "/File/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
XmlDocument document = new XmlDocument();
XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes");
document.AppendChild(xmldecl);
XmlElement nodeList = document.CreateElement("NodeList");
document.AppendChild(nodeList);
XmlElement firstNode = document.CreateElement("FirstNode");
nodeList.AppendChild(firstNode);
XmlElement nextID = document.CreateElement("NextID");
nextID.InnerText = "0";
firstNode.AppendChild(nextID);
XmlElement nodes = document.CreateElement("Nodes");
firstNode.AppendChild(nodes);
XmlElement secondNode = document.CreateElement("SecondNode");
nodeList.AppendChild(secondNode);
nextID = document.CreateElement("NextID");
nextID.InnerText = "0";
secondNode.AppendChild(nextID);
nodes = document.CreateElement("Nodes");
secondNode.AppendChild(nodes);
document.Save(GetNodeListXML());
document = null;
}
/// <summary>
/// 读取节点文件,获取节点数据
/// </summary>
private void LoadNodeListXML()
{
string filepath = GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode firstNodes = xmlDoc.SelectSingleNode("NodeList/FirstNode/Nodes");
foreach (XmlElement node in firstNodes)
{
FirstNodeObject obj = new FirstNodeObject();
obj.nodeID = int.Parse(node.GetAttribute("ID"));
obj.nodeName = node.GetAttribute("Name");
firstNodeObjects.Add(obj);
}
XmlNode secondNodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes");
foreach (XmlElement node in secondNodes)
{
SecondNodeObject obj = new SecondNodeObject();
obj.nodeID = int.Parse(node.GetAttribute("ID"));
obj.nodeName = node.GetAttribute("Name");
obj.nodeDetail = node.GetAttribute("Detail");
obj.ParentID = int.Parse(node.GetAttribute("ParentID"));
secondNodeObjects.Add(obj);
}
}
}
}