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
|
{
|
/// <summary>
|
/// 路径信息
|
/// </summary>
|
public class HousePathInfo
|
{
|
public HousePathInfo()
|
{
|
Obstacles = new List<ZTPolygon>();
|
LocationPoints = new List<ParametersPoint>();
|
FindPathPoints = new List<ZTPoint>();
|
FindPathLines = new List<ZTLinePoint>();
|
NextGates = new List<ParametersPoint>();
|
PathGuides = new List<ZTTargetPolygon>();
|
LoopPoint = new ZTPoint();
|
}
|
|
|
|
public HousePathInfo(Int32 width, Int32 height) : this()
|
{
|
this.Width = width;
|
this.Height = height;
|
}
|
|
|
#region Propertys
|
/// <summary>
|
/// 宽
|
/// </summary>
|
public int Width { get; set; }
|
|
/// <summary>
|
/// 高
|
/// </summary>
|
public int Height { get; set; }
|
|
|
|
|
/// <summary>
|
/// 障碍物
|
/// </summary>
|
public List<ZTPolygon> Obstacles { get; set; }
|
|
|
/// <summary>
|
/// 定位点
|
/// </summary>
|
public List<ParametersPoint> LocationPoints { get; set; }
|
|
|
/// <summary>
|
/// 寻路点
|
/// </summary>
|
public List<ZTPoint> FindPathPoints { get; set; }
|
|
/// <summary>
|
/// 寻路线
|
/// </summary>
|
public List<ZTLinePoint> FindPathLines { get; set; }
|
|
/// <summary>
|
/// 巡逻点
|
/// </summary>
|
public ZTPoint LoopPoint { get; set; }
|
|
/// <summary>
|
/// 下一关进门点
|
/// 参数意义 0:上 1:右 2:下 3:左
|
/// </summary>
|
public List<ParametersPoint> NextGates { get; set; }
|
|
/// <summary>
|
/// 路径向导,把某一区域的点导向某一点
|
/// </summary>
|
public List<ZTTargetPolygon> PathGuides { get; set; }
|
|
#endregion
|
|
|
#region Editor
|
/// <summary>
|
/// 添加障碍物
|
/// </summary>
|
/// <returns></returns>
|
public bool AddObstacle(ZTPolygon obstacle)
|
{
|
Int32 index = 0;
|
if (ExistsObstacle(out index, obstacle))
|
{
|
return false;
|
}
|
this.Obstacles.Add(obstacle);
|
return true;
|
}
|
|
/// <summary>
|
/// 删除障碍物
|
/// </summary>
|
/// <param name="obstacle"></param>
|
/// <returns></returns>
|
public bool RemoveObstacle(ZTPolygon obstacle)
|
{
|
Int32 index = 0;
|
if (!ExistsObstacle(out index, obstacle))
|
{
|
return true;
|
}
|
|
this.Obstacles.RemoveAt(index);
|
return true;
|
}
|
|
/// <summary>
|
/// 是否存在障碍物
|
/// </summary>
|
/// <param name="obstacle"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
|
/// <summary>
|
/// 添加定位点
|
/// </summary>
|
/// <param name="point"></param>
|
/// <param name="parameters"></param>
|
/// <returns></returns>
|
public bool AddLocationPosition(ParametersPoint point)
|
{
|
if (this.LocationPoints.Exists((p) => { return point.Point.Equals(p.Point); }))
|
{
|
return false;
|
}
|
|
this.LocationPoints.Add(point);
|
return true;
|
}
|
|
/// <summary>
|
/// 清空定位点
|
/// </summary>
|
/// <returns></returns>
|
public void ClearLocationPosiltion()
|
{
|
this.LocationPoints.Clear();
|
}
|
|
|
/// <summary>
|
/// 添加寻路点
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public bool AddFindPathPoint(ZTPoint point)
|
{
|
Int32 index = 0;
|
if (ExistsFindPathPoint(out index, point))
|
{
|
return false;
|
}
|
|
this.FindPathPoints.Add(point);
|
return true;
|
}
|
|
/// <summary>
|
/// 删除寻路点
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 是否存在指定寻路点
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
|
/// <summary>
|
/// 添加寻路线
|
/// </summary>
|
/// <param name="line"></param>
|
/// <returns></returns>
|
public bool AddFindPathLine(ZTLinePoint line)
|
{
|
Int32 index = 0;
|
if (ExistsFindPathLine(out index, line))
|
{
|
return false;
|
}
|
this.FindPathLines.Add(line);
|
return true;
|
}
|
|
/// <summary>
|
/// 删除寻路线
|
/// </summary>
|
/// <param name="line"></param>
|
/// <returns></returns>
|
public bool RemoveFindPathLine(ZTLinePoint line)
|
{
|
Int32 index = 0;
|
if (!ExistsFindPathLine(out index, line))
|
{
|
return true;
|
}
|
this.FindPathLines.RemoveAt(index);
|
return true;
|
}
|
|
|
/// <summary>
|
/// 是否存在寻路线
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="line"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 巡逻线或寻路线中是否存在某点
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 添加下一关的门
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public bool AddNextGate(ParametersPoint point)
|
{
|
Int32 index = 0;
|
if (ExistsNextGate(out index, point))
|
{
|
return false;
|
}
|
|
this.NextGates.Add(point);
|
return true;
|
}
|
|
/// <summary>
|
/// 移除下一关的门
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public bool RemoveNextGate(ParametersPoint point)
|
{
|
Int32 index = 0;
|
if (!ExistsNextGate(out index, point))
|
{
|
return true;
|
}
|
this.NextGates.RemoveAt(index);
|
return true;
|
}
|
|
/// <summary>
|
/// 是否存在到达下一关的点
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 添加路径导引多边形
|
/// </summary>
|
/// <param name="polygon"></param>
|
/// <returns></returns>
|
public bool AddPathGuide(ZTTargetPolygon polygon)
|
{
|
Int32 index = 0;
|
if (ExistsPathGuide(out index, polygon))
|
{
|
return false;
|
}
|
|
this.PathGuides.Add(polygon);
|
return true;
|
}
|
|
|
/// <summary>
|
/// 移除路径导引多边形
|
/// </summary>
|
/// <param name="polygon"></param>
|
/// <returns></returns>
|
public bool RemovePathGuide(ZTTargetPolygon polygon)
|
{
|
Int32 index = 0;
|
if (!ExistsPathGuide(out index, polygon))
|
{
|
return true;
|
}
|
this.PathGuides.RemoveAt(index);
|
return true;
|
}
|
|
/// <summary>
|
/// 是否存在路径导引
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="polygon"></param>
|
/// <returns></returns>
|
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
|
/// <summary>
|
/// 获取json
|
/// </summary>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 从json字符串生成对象
|
/// </summary>
|
/// <param name="json"></param>
|
/// <returns></returns>
|
public static HousePathInfo FromJsonString(string json)
|
{
|
HousePathInfoObj obj= ZTImage.Json.JsonParser.ToObject<HousePathInfoObj>(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
|
|
}
|
|
}
|