/// <summary> /// 检查移除障碍物 /// </summary> /// <param name="tile"></param> /// <returns></returns> public bool CheckRemoveObstacles(MapTile tile) { // block 为5*5建筑 if (tile == null || tile.blocked == false) { return(false); } IntVector2 pos = tile.Pos; foreach (IntVector2 p in obstacles.Keys) { if (Mathf.Abs(pos.x - p.x) <= 2 && Mathf.Abs(pos.y - p.y) <= 2) { List <MapTile> l = new List <MapTile>(); if (TileHelp.GetAreaTile(p, 2, true, ref l) == true) { Destroy(obstacles[p]); obstacles.Remove(p); MapChangeManger.UnBlockTile(l); return(true); } } } return(false); }
private void InitAPI() { TileHelp.Init(this); SectorHelp.Init(this); AStar.Init(this); FlowField.Init(this); JumpFlowFiled.Init(this); IggPathFinder.Init(this); MapChangeManger.Init(this); }
/// <summary> /// 获取路径 /// </summary> /// <param name="end"></param> /// <param name="start"></param> /// <returns></returns> private static List <IntVector2> GetRoadList(MapTile start, MapTile end) { List <IntVector2> l = new List <IntVector2>(); MapTile t = TileHelp.GetLowestIntergrationCostTile(end); while (t != null && t != start) { l.Add(t.Pos); t = TileHelp.GetLowestIntergrationCostTile(t); } l.Reverse(); return(l); }
/// <summary> /// 测试动态挡格,以tile为中心5 * 5的添加动态挡格 /// </summary> /// <param name="tile"></param> /// <returns></returns> public float TestRemoveBlock(MapTile tile) { List <MapTile> l = new List <MapTile>(); if (TileHelp.GetAreaTile(tile.Pos, 2, false, ref l) == true) { MapChangeManger.UnBlockTile(l); } Stopwatch sw = new Stopwatch(); sw.Start(); MapChangeManger.InputChanges(); sw.Stop(); return(sw.ElapsedTicks / 10000.0f); }
/// <summary> /// 测试动态挡格,以tile为中心5 * 5的添加动态挡格 /// </summary> /// <param name="tile"></param> /// <returns></returns> public float TestAddBlock(MapTile tile) { List <MapTile> l = new List <MapTile>(); if (TileHelp.GetAreaTile(tile.Pos, 2, false, ref l) == true) { MapChangeManger.BlockTile(l); } Stopwatch sw = new Stopwatch(); sw.Start(); Profiler.BeginSample("TestMethod"); MapChangeManger.InputChanges(); Profiler.EndSample(); sw.Stop(); return(sw.ElapsedTicks / 10000.0f); }
/// <summary> /// 在一个扇区内进行流程寻路算法 /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <param name="map"></param> /// <param name="lSector">指定扇区内</param> /// <returns></returns> public static List <IntVector2> GetRoadInSector(MapTile start, MapTile end, List <ushort> lSector) { bool ret = false; openSet.Clear(); closedSet.Clear(); openSet.Add(start); start.integrationValue = 0; while (openSet.Count > 0 && ret == false) { MapTile currentNode = openSet[0]; MapTileSearchResult result = TileHelp.GetNeighboursExpansionSearch(currentNode, lSector); for (int i = 0; i < result.Count; i++) { MapTile neighbour = result.Get(i); if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); // if true, there is a higher node here if (neighbour == end) { ret = true; break; } } } closedSet.Add(currentNode); openSet.Remove(currentNode); } // 得到路径了,回溯路径 List <IntVector2> l = GetRoadList(start, end); // reset for (int i = 0; i < openSet.Count; i++) { openSet[i].integrationValue = TileHelp.tileResetIntegrationValue; } for (int i = 0; i < closedSet.Count; i++) { closedSet[i].integrationValue = TileHelp.tileResetIntegrationValue; } return(l); }
/// <summary> /// 检查添加障碍物 /// </summary> /// <param name="tile"></param> /// <returns></returns> public bool CheckAddObstacles(MapTile tile) { if (tile == null || tile.blocked == true) { return(false); } List <MapTile> l = new List <MapTile>(); if (TileHelp.GetAreaTile(tile.Pos, 2, false, ref l) == true) { GameObject b = Instantiate(unitblock, PathFind.instance.m_map.GetMapTileWorldPos(tile), Quaternion.identity) as GameObject; b.transform.parent = transform; obstacles.Add(tile.Pos, b); MapChangeManger.BlockTile(l); return(true); } return(false); }
/// <summary> /// 使用流程寻路算法计算start点到扇区的各个gate,并保存的way point的cost中。 /// </summary> /// <param name="start">sector中的起点</param> /// <param name="sector">指定的扇区</param> /// <param name="map">指定地图</param> public static void CalcSectorWayPointCost(PathNode start) { openSet.Clear(); closedSet.Clear(); openSet.Add(start.tileConnection); start.tileConnection.integrationValue = 0; while (openSet.Count > 0) { MapTile currentNode = openSet[0]; MapTileSearchResult result = TileHelp.GetAllNeighboursInSectorFlowFieldSearch(currentNode); for (int i = 0; i < result.Count; i++) { MapTile neighbour = result.Get(i); if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); // if true, there is a higher node here if (neighbour.hasPathNodeConnection) { PathNode neighbourSectorNode = g_map.GetWayPointPathNode(0, neighbour); if (neighbourSectorNode != null) { List <IntVector2> l = GetRoadList(start.tileConnection, neighbour); PathNode.LinkSectorNode(start, neighbourSectorNode, neighbour.integrationValue / 10, l); } } } } closedSet.Add(currentNode); openSet.Remove(currentNode); } // reset for (int i = 0; i < openSet.Count; i++) { openSet[i].integrationValue = TileHelp.tileResetIntegrationValue; } for (int i = 0; i < closedSet.Count; i++) { closedSet[i].integrationValue = TileHelp.tileResetIntegrationValue; } }
private void AddUnit(MapTile t) { if (t != null && m_UnitKey < 10) { List <PathRun> list = new List <PathRun>(); foreach (MapTile tile in TileHelp.GetAllNeighbours(t)) { GameObject g = GameObject.Instantiate(unitGo); g.transform.parent = tnode; Vector3 v = PathFind.instance.m_map.GetMapTileWorldPos(tile); g.transform.position = v; PathRun s = g.GetComponent <PathRun>(); s.group = m_UnitKey; s.currentTile = tile; list.Add(s); } m_DicUnits.Add(m_UnitKey++, list); } }