第一次提交
This commit is contained in:
134
Assets/Scripts/System/TriggerConfig.cs
Normal file
134
Assets/Scripts/System/TriggerConfig.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HxGame.Data
|
||||
{
|
||||
//<2F><><EFBFBD><EFBFBD>ģʽ
|
||||
public enum TriggerMode
|
||||
{
|
||||
KillMonster, //<2F><>ɱ<EFBFBD><C9B1><EFBFBD><EFBFBD>
|
||||
UseItem, //ʹ<>õ<EFBFBD><C3B5><EFBFBD>
|
||||
Task, //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Npc, //<2F><><EFBFBD><EFBFBD>NPC
|
||||
}
|
||||
|
||||
public class TriggerConfig
|
||||
{
|
||||
public TriggerMode mode; //<2F><><EFBFBD><EFBFBD>ģʽ
|
||||
public Vector2Int pos; //<2F><><EFBFBD><EFBFBD>
|
||||
public int radius; //<2F>뾶
|
||||
public int eventId; //<2F>¼<EFBFBD>id
|
||||
}
|
||||
|
||||
public class TriggersConfig
|
||||
{
|
||||
public List<TriggerConfig> triggersConfig = new List<TriggerConfig>();
|
||||
|
||||
public void SaveXML(int mapId)
|
||||
{
|
||||
string path = PathUtil.GetXmlPath(mapId, "TriggersConfig");
|
||||
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 triggers = xml.CreateElement("Triggers");//<2F>ڵ<EFBFBD>Settings
|
||||
xml.DocumentElement.AppendChild(triggers);
|
||||
|
||||
SaveTriggers(xml, triggers);
|
||||
|
||||
xml.Save(path);
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD>津<EFBFBD><E6B4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>óɹ<C3B3>");
|
||||
}
|
||||
|
||||
private void SaveTriggers(XmlDocument xml, XmlElement xmlElement)
|
||||
{
|
||||
XmlElement child = null;
|
||||
|
||||
for (int i = 0; i < triggersConfig.Count; i++)
|
||||
{
|
||||
child = xml.CreateElement("Trigger");
|
||||
child.SetAttribute("mode", ((int)triggersConfig[i].mode).ToString());
|
||||
child.SetAttribute("indexX", triggersConfig[i].pos.x.ToString());
|
||||
child.SetAttribute("indexY", triggersConfig[i].pos.y.ToString());
|
||||
child.SetAttribute("radius", triggersConfig[i].radius.ToString());
|
||||
child.SetAttribute("eventId", triggersConfig[i].eventId.ToString());
|
||||
|
||||
xmlElement.AppendChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
public bool LoadXML(int mapId)
|
||||
{
|
||||
string path = PathUtil.GetXmlPath(mapId, "TriggersConfig");
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><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");
|
||||
|
||||
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 triggers = xmlRoot.SelectSingleNode("Triggers");
|
||||
LoadTriggers(triggers);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void LoadTriggers(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 != "Trigger")
|
||||
continue;
|
||||
|
||||
var t = new TriggerConfig();
|
||||
t.mode = (TriggerMode)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("mode").Value);
|
||||
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
|
||||
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
|
||||
t.pos = new Vector2Int(x, y);
|
||||
t.radius = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("radius").Value);
|
||||
t.eventId = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("eventId").Value);
|
||||
triggersConfig.Add(t);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user