private void MarkMoveZoneRecursive(MarkersSet moveMarkers, Coord coord, Unit unit, int actionPoints) { var cell = map[coord]; actionPoints -= cell.MoveCost; if (actionPoints < 0) // No action points { return; } if (!cell.CanMoveAcrossBy(unit.MoveTerrainMask)) // Can't move across cell type of terrain { return; } if (actionPoints <= moveMarkers[coord]) // cell already pacified { return; } if (cell.HasEnemyUnit(unit.Faction)) // enemy unit on cell { return; } if (cell.HasEnemyCity(unit.Faction)) // enemy city on cell { return; } moveMarkers[coord] = actionPoints; for (int i = 0; i < Neigbhors.Length; i++) { MarkMoveZoneRecursive(moveMarkers, coord + Neigbhors[i], unit, actionPoints); } }
public MarkersSet GetMoveZone(Unit unit) { var moveMarkers = new MarkersSet(); MarkMoveZoneRecursive(moveMarkers, unit.Coordinate, unit, unit.ActionPoints + map[unit.Coordinate].MoveCost); return(moveMarkers); }
private void SelectUnit(Unit unit, City city) { selectedUnit = unit; moveZone.Clear(); fireZone.Clear(); fireTargetCoord = Empty; moveTargetCoord = Empty; turnTargetCoord = Empty; moveMarkersView.Hide(); fireMarkersView.Hide(); moveZoneMarkersView.Hide(); selectedMarkersView.Hide(); moveSelector.Hide(); fireSelector.Hide(); if (unit == null) { gameUI.ShowUnit(null); if (city != null && city.Faction == currentFaction) { CityUI.ShowCityUI(city, game); } return; } if (unit.Faction != currentFaction) { selectedUnit = null; gameUI.ShowUnit(null); return; } gameUI.ShowUnit(unit); selectedCoord = unit.Coordinate; selectedMarkersView.Show(mapView.CellCoordToPosition(selectedCoord)); if (selectedUnit.ActionPoints > 0) { moveZone = game.Map.GetMoveZone(unit); var moveCoords = moveZone.GetCoordList(); fireZone = game.Map.GetFireZoneForMoveZone(unit, moveZone); var fireCoords = fireZone.GetCoordList(); moveZoneMarkersView.Show(CoordToPositions(moveCoords)); fireMarkersView.Show(CoordToPositions(fireCoords)); } if (unitTargetPoints.ContainsKey(selectedUnit)) { ShowCurrentPath(selectedUnit, unitTargetPoints[selectedUnit]); } }
public List <Coord> TryFindPath(Coord from, Coord to, MarkersSet moveMarkers) { List <Coord> pathPoints = new List <Coord>(); if (moveMarkers[to] >= 0) { pathPoints.Add(to); Coord current = to; const int maxSearchDeep = 100; int deep = 0; int nearestDistance; Coord neigbhor; Coord nearest = new Coord(); while (deep < maxSearchDeep) { nearestDistance = 0; for (int i = 0; i < Neigbhors.Length; i++) { neigbhor = current + Neigbhors[i]; if (nearestDistance < moveMarkers[neigbhor]) { nearestDistance = moveMarkers[neigbhor]; nearest = neigbhor; } } current = nearest; pathPoints.Add(current); if (current == from) { break; } deep += 1; } if (pathPoints.Count >= 2) { pathPoints.Reverse(); return(pathPoints); } } Debug.LogError("Error find path by markers set"); return(new List <Coord>() { from, to }); }
private Coord FindFarestPointInPath(List <Coord> pathCoords, MarkersSet moveZone) { var pathCoord = pathCoords.First(); Coord nextCoord; for (int i = 1; i < pathCoords.Count; i++) { nextCoord = pathCoords[i]; if (moveZone[nextCoord] < 0) { return(pathCoord); } if (!game.Map[nextCoord].HasUnit) { pathCoord = nextCoord; } } return(pathCoord); }