SpotPoint GetCurrentActiveSpot() { SpotPoint point = activeSpots[0]; activeSpots.RemoveAt(0); return(point); }
public List <SpotPoint> GetNeighbours(SpotPoint point, Grid grid) { List <SpotPoint> Neighbours = new List <SpotPoint>(); int[,] neighCoords = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; for (int i = 0; i < 4; ++i) { Vector2 newPos = new Vector2(point.X + neighCoords[i, 0], point.Z + neighCoords[i, 1]); if (grid.InRangePoint(newPos.x + 1, newPos.y + 1)) { bool Color = point.Walkable && grid.GetGrid()[(int)newPos.x, (int)newPos.y] == 0; Neighbours.Add(new SpotPoint(newPos, Color)); } } return(Neighbours); }
void AddToSpot(SpotPoint point) { if (TryAddToSpot(point)) { int index = 0; while (index < pointsToCover.Count) { if (point.Compare(pointsToCover[index])) { if (point.Walkable) { CoveredPoints.Add(pointsToCover[index]); } pointsToCover.RemoveAt(index); } else { index++; } } } }
bool TryAddToSpot(SpotPoint newPoint) { if (!spotsArray.ContainsKey(newPoint.X)) { spotsArray[newPoint.X] = new Dictionary <int, SpotPoint>(); } if (!spotsArray[newPoint.X].ContainsKey(newPoint.Z)) { spotsArray[newPoint.X][newPoint.Z] = newPoint; activeSpots.Add(newPoint); return(true); } else { if (newPoint.Walkable && !spotsArray[newPoint.X][newPoint.Z].Walkable) { spotsArray[newPoint.X][newPoint.Z] = newPoint; activeSpots.Add(newPoint); return(true); } } return(false); }
public List <UnitPoint> MakeSpot(Vector3 StartPos, Vector3 Center, Grid grid, int unitCount, Item item, PathfindingA pathfindingA) { Vector3 direction = (Center - StartPos).normalized; this.grid = grid; Strategy.InitStrategy(); List <Vector3> EndCells = Strategy.GetMoveCells(direction, Center, unitCount, grid, item); spotsArray.Clear(); activeSpots.Clear(); pointsToCover.Clear(); CoveredPoints.Clear(); foreach (var Cell in EndCells) { AddToCover(new UnitPoint(grid, Cell)); } Vector2 centerPos = grid.GetPointByPosition(Center); Vector2 startPosInGrid = grid.GetPointByPosition(StartPos); foreach (var startPos in Strategy.GetStartPoints(centerPos, pointsToCover)) { bool walkable = grid.GetGrid()[(int)startPos.x, (int)startPos.y] == 0; if (walkable && pathfindingA.HasPath(startPos, startPosInGrid) || !walkable) { AddToSpot(new SpotPoint(startPos, walkable)); } } while (CoveredPoints.Count < unitCount) { while (pointsToCover.Count != 0 && CoveredPoints.Count < unitCount && HasActiveSpot()) { SpotPoint point = GetCurrentActiveSpot(); List <SpotPoint> Neighbours = GetNeighbours(point, grid); AddToSpot(Neighbours, unitCount); } if (CoveredPoints.Count < unitCount) { List <Vector3> NewCells = Strategy.ExpandPoints(direction, Center, unitCount, grid, item); foreach (var cell in NewCells) { AddToCover(new UnitPoint(grid, cell)); } CheckNewPointsToCover(unitCount); } if (!HasActiveSpot()) { /*foreach (var line in spotsArray.Values) * { * foreach (SpotPoint point in line.Values) * { * grid.PlaceSpot(point.X, point.Z, point.Walkable); * } * } */ return(new List <UnitPoint>()); } } /*foreach (var line in spotsArray.Values) * { * foreach (SpotPoint point in line.Values) * { * grid.PlaceSpot(point.X, point.Z, point.Walkable); * } * }*/ return(CoveredPoints); }