using RichCreator.Utility.Structs; using RichCreator.Utility.Utilitys; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RichCreator.Utility.Maps { /// /// 路径信息 /// public class HousePathInfo { public HousePathInfo() { Obstacles = new List(); LocationPoints = new List(); FindPathPoints = new List(); FindPathLines = new List(); NextGates = new List(); PathGuides = new List(); LoopPoint = new ZTPoint(); } public HousePathInfo(Int32 width, Int32 height) : this() { this.Width = width; this.Height = height; } #region Propertys /// /// 宽 /// public int Width { get; set; } /// /// 高 /// public int Height { get; set; } /// /// 障碍物 /// public List Obstacles { get; set; } /// /// 定位点 /// public List LocationPoints { get; set; } /// /// 寻路点 /// public List FindPathPoints { get; set; } /// /// 寻路线 /// public List FindPathLines { get; set; } /// /// 巡逻点 /// public ZTPoint LoopPoint { get; set; } /// /// 下一关进门点 /// 参数意义 0:上 1:右 2:下 3:左 /// public List NextGates { get; set; } /// /// 路径向导,把某一区域的点导向某一点 /// public List PathGuides { get; set; } #endregion #region Editor /// /// 添加障碍物 /// /// public bool AddObstacle(ZTPolygon obstacle) { Int32 index = 0; if (ExistsObstacle(out index, obstacle)) { return false; } this.Obstacles.Add(obstacle); return true; } /// /// 删除障碍物 /// /// /// public bool RemoveObstacle(ZTPolygon obstacle) { Int32 index = 0; if (!ExistsObstacle(out index, obstacle)) { return true; } this.Obstacles.RemoveAt(index); return true; } /// /// 是否存在障碍物 /// /// /// private bool ExistsObstacle(out Int32 index, ZTPolygon obstacle) { index = 0; for (int i = 0; i < Obstacles.Count; i++) { if (Obstacles[i] .Equals( obstacle)) { index = i; return true; } } return false; } /// /// 添加定位点 /// /// /// /// public bool AddLocationPosition(ParametersPoint point) { if (this.LocationPoints.Exists((p) => { return point.Point.Equals(p.Point); })) { return false; } this.LocationPoints.Add(point); return true; } /// /// 清空定位点 /// /// public void ClearLocationPosiltion() { this.LocationPoints.Clear(); } /// /// 添加寻路点 /// /// /// public bool AddFindPathPoint(ZTPoint point) { Int32 index = 0; if (ExistsFindPathPoint(out index, point)) { return false; } this.FindPathPoints.Add(point); return true; } /// /// 删除寻路点 /// /// /// public bool RemoveFindPathPoint(ZTPoint point) { Int32 index = 0; if (!ExistsFindPathPoint(out index, point)) { return true; } //查询巡逻线和寻路线是否有用到此点 if (ExistsPointInAllLine(point)) { return false; } this.FindPathPoints.RemoveAt(index); return true; } /// /// 是否存在指定寻路点 /// /// /// /// private bool ExistsFindPathPoint(out Int32 index, ZTPoint point) { index = 0; for (int i = 0; i < this.FindPathPoints.Count; i++) { if (this.FindPathPoints[i].Equals(point)) { index = i; return true; } } return false; } /// /// 添加寻路线 /// /// /// public bool AddFindPathLine(ZTLinePoint line) { Int32 index = 0; if (ExistsFindPathLine(out index, line)) { return false; } this.FindPathLines.Add(line); return true; } /// /// 删除寻路线 /// /// /// public bool RemoveFindPathLine(ZTLinePoint line) { Int32 index = 0; if (!ExistsFindPathLine(out index, line)) { return true; } this.FindPathLines.RemoveAt(index); return true; } /// /// 是否存在寻路线 /// /// /// /// private bool ExistsFindPathLine(out Int32 index, ZTLinePoint line) { index = 0; for (int i = 0; i < this.FindPathLines.Count; i++) { if (this.FindPathLines[i] .Equals( line)) { index = i; return true; } } return false; } /// /// 巡逻线或寻路线中是否存在某点 /// /// /// private bool ExistsPointInAllLine(ZTPoint point) { for (int i = 0; i < this.FindPathLines.Count; i++) { if (this.FindPathLines[i].P1.Equals(point) || this.FindPathLines[i].P2.Equals(point)) { return true; } } return false; } /// /// 添加下一关的门 /// /// /// public bool AddNextGate(ParametersPoint point) { Int32 index = 0; if (ExistsNextGate(out index, point)) { return false; } this.NextGates.Add(point); return true; } /// /// 移除下一关的门 /// /// /// public bool RemoveNextGate(ParametersPoint point) { Int32 index = 0; if (!ExistsNextGate(out index, point)) { return true; } this.NextGates.RemoveAt(index); return true; } /// /// 是否存在到达下一关的点 /// /// /// public bool ExistsNextGate(out Int32 index,ParametersPoint point) { index = -1; for (int i = 0; i < this.NextGates.Count; i++) { if (this.NextGates[i].Equals(point)) { index = i; return true; } } return false; } /// /// 添加路径导引多边形 /// /// /// public bool AddPathGuide(ZTTargetPolygon polygon) { Int32 index = 0; if (ExistsPathGuide(out index, polygon)) { return false; } this.PathGuides.Add(polygon); return true; } /// /// 移除路径导引多边形 /// /// /// public bool RemovePathGuide(ZTTargetPolygon polygon) { Int32 index = 0; if (!ExistsPathGuide(out index, polygon)) { return true; } this.PathGuides.RemoveAt(index); return true; } /// /// 是否存在路径导引 /// /// /// /// public bool ExistsPathGuide(out Int32 index, ZTTargetPolygon polygon) { index = -1; for (int i = 0; i < this.PathGuides.Count; i++) { if (this.PathGuides[i].Equals(polygon)) { index = i; return true; } } return false; } #endregion #region json info /// /// 获取json /// /// public string ToJsonString(Int32 width,Int32 height) { this.Width = width; this.Height = height; HousePathInfoObj obj=HousePathInfoObj.From(this); string json = ZTImage.Json.JsonBuilder.ToJsonString(obj); return json; } /// /// 从json字符串生成对象 /// /// /// public static HousePathInfo FromJsonString(string json) { HousePathInfoObj obj= ZTImage.Json.JsonParser.ToObject(json); return obj.To(); } public static HousePathInfo From(MapType mapType, Int32 houseIndex) { string info = ReadConfig(mapType, houseIndex); try { return HousePathInfo.FromJsonString(info); } catch (Exception e) { throw e; } } private static String ReadConfig(MapType mapType, Int32 houseIndex) { string file = string.Empty; if (mapType == MapType.Lingdong) { file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configs", "lingdong.txt"); } else if (mapType == MapType.Kalete) { file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configs", "kalete.txt"); } if (!File.Exists(file)) { throw new FieldAccessException("config file not exists," + file); } string content = System.IO.File.ReadAllText(file); if (string.IsNullOrEmpty(content)) { throw new FieldAccessException("file empty," + file); } string[] lines = content.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Int32 row = houseIndex * 2 + 1; if (row > lines.Length) { throw new ArgumentOutOfRangeException("超出的行"); } return lines[row]; } #endregion } }