Files
HX_MapEditor/Assets/Scripts/System/MapConfig.cs
2025-06-14 13:46:24 +08:00

424 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
namespace HxGame.Data
{
//角色游戏模式
[Flags]
public enum RoleMode
{
PK = 1 << 0, //PK
ShiZu = 1 << 1, //氏族
Team = 1 << 2, //团队
LianJi = 1 << 3, //练级
ChengZhan = 1 << 4 //城战
}
//使用模式
[Flags]
public enum UseMode
{
UseRandom = 1 << 0, //能否使用随机卷
UseUnderground = 1 << 1, //能否使用地城
UseBack = 1 << 2, //能否使用回城
UseItem = 1 << 3, //能否使用药品
FuBen = 1 << 4, //是否副本地图
}
//复活点类型
public enum ReliveType
{
Normal, //普通复活点
ChengZhan1, //城战复活点1
ChengZhan2, //城战复活点2
}
//进入条件
public enum ConditionMode
{
GM, //GM
Team, //组队
Clan, //氏族
Task, //任务
Activity, //活动
}
[Serializable]
public class MapConfig
{
//传送点
public class TeleportConfig
{
public Vector2Int pos; //传送坐标
public int nextMapId; //下级地图
public Vector2Int nextMapPos; //下级地图坐标
}
//区域
public class AreaConfig
{
public Vector2Int pos; //中心点坐标
public int radius; //半径
}
//复活点
public class ReliveConfig
{
public ReliveType reliveType; //复活点类型
public int mapId; //复活地图
public Vector2Int pos; //中心点坐标
public int radius; //半径
}
//音效触发
public class AudioTriggerConfig
{
public Vector2Int pos; //中心点坐标
public int radius; //半径
public string name; //音效名称
}
public class ConditionConfig
{
public ConditionMode mode; //进入条件
public int level; //进入等级
public int itemId; //消耗道具
public int money; //消耗货币
public int time; //消耗时间
}
public string mapName; //地图名称
public int enterLv; //进入等级
public int returnMapId; //回城地图ID
public Vector2Int returnMapCellPoint; //回城地图坐标
public RoleMode roleMode; //角色游戏模式
public UseMode useMode; //使用模式
public bool deadFall; //是否死亡掉落
public List<TeleportConfig> teleportConfigs = new List<TeleportConfig>();
public List<ReliveConfig> relivesConfig = new List<ReliveConfig>();
public List<AreaConfig> sellAreasConfig = new List<AreaConfig>();
public List<AudioTriggerConfig> audioTriggersConfig = new List<AudioTriggerConfig>();
public List<ConditionConfig> conditionsConfig = new List<ConditionConfig>();
public MapConfig()
{
roleMode = 0;
useMode = 0;
deadFall = false;
}
public void SaveXML(int mapId)
{
string path = PathUtil.GetXmlPath(mapId, "MapConfig");
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);
XmlElement Teleports = xml.CreateElement("Teleports");
xml.DocumentElement.AppendChild(Teleports);
XmlElement ReliveAreas = xml.CreateElement("ReliveAreas");
xml.DocumentElement.AppendChild(ReliveAreas);
XmlElement SellAreas = xml.CreateElement("SellAreas");
xml.DocumentElement.AppendChild(SellAreas);
XmlElement AudioTriggers = xml.CreateElement("AudioTriggers");
xml.DocumentElement.AppendChild(AudioTriggers);
XmlElement Conditions = xml.CreateElement("Conditions");
xml.DocumentElement.AppendChild(Conditions);
SaveMapInfoConfig(mapId, Settings);
SaveTeleports(xml, Teleports);
SaveRelives(xml, ReliveAreas);
SaveSellAreas(xml, SellAreas);
SaveAudioTriggers(xml, AudioTriggers);
SaveConditions(xml, Conditions);
xml.Save(path);
UIWindow.Instance.ShowMessage("保存地图配置成功");
}
private void SaveMapInfoConfig(int mapId, XmlElement xmlSettings)
{
xmlSettings.SetAttribute("mapID", mapId.ToString());
xmlSettings.SetAttribute("mapName", mapName);
xmlSettings.SetAttribute("enterLv", enterLv.ToString());
xmlSettings.SetAttribute("returnMapId", returnMapId.ToString());
xmlSettings.SetAttribute("returnMapCellPointX", returnMapCellPoint.x.ToString());
xmlSettings.SetAttribute("returnMapCellPointY", returnMapCellPoint.y.ToString());
xmlSettings.SetAttribute("roleMode", ((int)roleMode).ToString());
xmlSettings.SetAttribute("useMode", ((int)useMode).ToString());
xmlSettings.SetAttribute("deadFall", deadFall ? "1" : "0");
}
private void SaveTeleports(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for(int i=0; i<teleportConfigs.Count; i++)
{
child = xml.CreateElement("Teleport");
child.SetAttribute("posIndexX", teleportConfigs[i].pos.x.ToString());
child.SetAttribute("posIndexY", teleportConfigs[i].pos.y.ToString());
child.SetAttribute("nextMapId", teleportConfigs[i].nextMapId.ToString());
child.SetAttribute("nextMapPosIndexX", teleportConfigs[i].nextMapPos.x.ToString());
child.SetAttribute("nextMapPosIndexY", teleportConfigs[i].nextMapPos.y.ToString());
xmlElement.AppendChild(child);
}
}
private void SaveRelives(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for (int i = 0; i < relivesConfig.Count; i++)
{
child = xml.CreateElement("ReliveArea");
child.SetAttribute("type", ((int)relivesConfig[i].reliveType).ToString());
child.SetAttribute("mapId", relivesConfig[i].mapId.ToString());
child.SetAttribute("indexX", relivesConfig[i].pos.x.ToString());
child.SetAttribute("indexY", relivesConfig[i].pos.y.ToString());
child.SetAttribute("radius", relivesConfig[i].radius.ToString());
xmlElement.AppendChild(child);
}
}
private void SaveSellAreas(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for (int i = 0; i < sellAreasConfig.Count; i++)
{
child = xml.CreateElement("SellArea");
child.SetAttribute("indexX", sellAreasConfig[i].pos.x.ToString());
child.SetAttribute("indexY", sellAreasConfig[i].pos.y.ToString());
child.SetAttribute("radius", sellAreasConfig[i].radius.ToString());
xmlElement.AppendChild(child);
}
}
private void SaveAudioTriggers(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for (int i = 0; i < audioTriggersConfig.Count; i++)
{
child = xml.CreateElement("AudioTrigger");
child.SetAttribute("indexX", audioTriggersConfig[i].pos.x.ToString());
child.SetAttribute("indexY", audioTriggersConfig[i].pos.y.ToString());
child.SetAttribute("radius", audioTriggersConfig[i].radius.ToString());
child.SetAttribute("name", audioTriggersConfig[i].name);
xmlElement.AppendChild(child);
}
}
private void SaveConditions(XmlDocument xml, XmlElement xmlElement)
{
XmlElement child = null;
for (int i = 0; i < conditionsConfig.Count; i++)
{
child = xml.CreateElement("Condition");
child.SetAttribute("mode", ((int)conditionsConfig[i].mode).ToString());
child.SetAttribute("level", conditionsConfig[i].level.ToString());
child.SetAttribute("itemId", conditionsConfig[i].itemId.ToString());
child.SetAttribute("money", conditionsConfig[i].money.ToString());
child.SetAttribute("time", conditionsConfig[i].time.ToString());
xmlElement.AppendChild(child);
}
}
public bool LoadXML(int mapId)
{
string path = PathUtil.GetXmlPath(mapId, "MapConfig");
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 xmlTeleports = xmlRoot.SelectSingleNode("Teleports");
XmlNode xmlReliveAreas = xmlRoot.SelectSingleNode("ReliveAreas");
XmlNode xmlSellAreas = xmlRoot.SelectSingleNode("SellAreas");
XmlNode xmlAudioTriggers = xmlRoot.SelectSingleNode("AudioTriggers");
XmlNode xmlConditions = xmlRoot.SelectSingleNode("Conditions");
int id = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("mapID").Value);
if(id != mapId)
{
UIWindow.Instance.ShowMessage("地图ID错误请检查MapConfig配置文件");
return false;
}
LoadMapInfoConfig(xmlSettings);
LoadTeleports(xmlTeleports);
LoadRelivesArea(xmlReliveAreas);
LoadSellAreas(xmlSellAreas);
LoadAudioTriggers(xmlAudioTriggers);
LoadConditions(xmlConditions);
return true;
}
private void LoadMapInfoConfig(XmlNode xmlSettings)
{
mapName = xmlSettings.Attributes.GetNamedItem("mapName").Value;
enterLv = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("enterLv").Value);
returnMapId = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("returnMapId").Value);
int x, y;
x = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("returnMapCellPointX").Value);
y = Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("returnMapCellPointY").Value);
returnMapCellPoint = new Vector2Int(x, y);
roleMode = (RoleMode)Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("roleMode").Value);
useMode = (UseMode)Convert.ToInt32(xmlSettings.Attributes.GetNamedItem("useMode").Value);
deadFall = xmlSettings.Attributes.GetNamedItem("enterLv").Value == "1" ? true : false;
}
private void LoadTeleports(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 != "Teleport")
continue;
TeleportConfig tc = new TeleportConfig();
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("posIndexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("posIndexY").Value);
tc.pos = new Vector2Int(x, y);
tc.nextMapId = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("nextMapId").Value);
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("nextMapPosIndexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("nextMapPosIndexY").Value);
tc.nextMapPos = new Vector2Int(x, y);
teleportConfigs.Add(tc);
}
}
private void LoadRelivesArea(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 != "ReliveArea")
continue;
ReliveConfig rc = new ReliveConfig();
rc.reliveType = (ReliveType)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("type").Value);
rc.mapId = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("mapId").Value);
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
rc.pos = new Vector2Int(x, y);
rc.radius = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("radius").Value);
relivesConfig.Add(rc);
}
}
private void LoadSellAreas(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 != "SellArea")
continue;
AreaConfig ac = new AreaConfig();
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
ac.pos = new Vector2Int(x, y);
ac.radius = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("radius").Value);
sellAreasConfig.Add(ac);
}
}
private void LoadAudioTriggers(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 != "AudioTrigger")
continue;
AudioTriggerConfig at = new AudioTriggerConfig();
x = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexX").Value);
y = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("indexY").Value);
at.pos = new Vector2Int(x, y);
at.radius = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("radius").Value);
at.name = xmlNode.Attributes.GetNamedItem("name").Value;
audioTriggersConfig.Add(at);
}
}
private void LoadConditions(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 != "Condition")
continue;
var t = new ConditionConfig();
t.mode = (ConditionMode)Convert.ToInt32(xmlNode.Attributes.GetNamedItem("mode").Value);
t.level = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("level").Value);
t.itemId = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("itemId").Value);
t.money = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("money").Value);
t.time = Convert.ToInt32(xmlNode.Attributes.GetNamedItem("time").Value);
conditionsConfig.Add(t);
}
}
}
}