Esempio n. 1
0
    public HexCell Patrol(HexCell hexCell, int radius)
    {
        HexCell        target = null;
        List <HexUnit> units  = hexGrid.GetUnitsInRange(hexCell, radius);

        foreach (HexUnit unit in units)
        {
            if (unit.HexUnitType == HexUnit.UnitType.COMBAT)
            {
                if (unit.GetComponent <Unit>().CityState&& unit.GetComponent <Unit>().CityState.CityStateID != ActiveUnit.GetComponent <Unit>().CityState.CityStateID)
                {
                    hexGrid.FindPath(ActiveUnit.HexUnit.Location, unit.Location, ActiveUnit.HexUnit, true, false);
                    target = GetFirstCellFromPath();
                    if (target)
                    {
                        return(target);
                    }
                }
            }
        }
        if (hexCell.coordinates.DistanceTo(unit.HexUnit.Location.coordinates) > radius)
        {
            hexGrid.FindPath(unit.HexUnit.Location, hexCell, unit.HexUnit);
            return(GetFirstCellFromPath());
        }

        List <HexDirection> directions = Enum.GetValues(typeof(HexDirection)).Cast <HexDirection>().ToList();

        while (directions.Count > 0 && !target)
        {
            HexDirection direction = directions[UnityEngine.Random.Range(0, directions.Count)];
            directions.Remove(direction);
            HexCell neighbour = unit.HexUnit.Location.GetNeighbor(direction);
            if (hexCell.coordinates.DistanceTo(neighbour.coordinates) <= radius && unit.HexUnit.IsValidDestination(neighbour, true))
            {
                target = neighbour;
            }
        }
        return(target);
    }
Esempio n. 2
0
    public HexCell Defend(City city)
    {
        HexCell        target = null;
        List <HexUnit> units  = hexGrid.GetUnitsInRange(city.GetHexCell(), 2);

        foreach (HexUnit unit in units)
        {
            if (unit.HexUnitType == HexUnit.UnitType.COMBAT)
            {
                if (unit.GetComponent <Unit>().CityState&& unit.GetComponent <Unit>().CityState.CityStateID != ActiveUnit.GetComponent <Unit>().CityState.CityStateID)
                {
                    hexGrid.FindPath(ActiveUnit.HexUnit.Location, unit.Location, ActiveUnit.HexUnit, true, false);
                    target = GetFirstCellFromPath();
                    if (target)
                    {
                        return(target);
                    }
                }
            }
        }

        if (city.GetHexCell().CanUnitMoveToCell(ActiveUnit.HexUnit))
        {
            hexGrid.FindPath(ActiveUnit.HexUnit.Location, city.GetHexCell(), ActiveUnit.HexUnit, true, false);
            target = GetFirstCellFromPath();
            if (target)
            {
                return(target);
            }
        }


        foreach (HexCell cell in PathFindingUtilities.GetCellsInRange(city.GetHexCell(), 1))
        {
            if (cell.CanUnitMoveToCell(ActiveUnit.HexUnit))
            {
                hexGrid.FindPath(ActiveUnit.HexUnit.Location, city.GetHexCell(), ActiveUnit.HexUnit, true, false);
                target = GetFirstCellFromPath();
                if (target)
                {
                    return(target);
                }
            }
        }
        return(target);
    }