//Returns all targetable hexes private List <HexCooridnates> GetHexesInRange(HexCooridnates coords, float minRange, float maxRange) { List <HexCooridnates> _reachableHexCoords = new List <HexCooridnates>(); for (int x = 0 - (int)maxRange; x <= maxRange; x++) { for (int y = 0 - (int)maxRange; y <= maxRange; y++) { int[] i = HexCoordToArrayindex(new HexCooridnates(x + coords._x, y + coords._y)); //Checks if hex is on the board if (i[0] >= 0 && i[0] <= _dimensions[0] - 1 && i[1] >= 0 && i[1] <= _dimensions[1]) { //Checks if hex is in distance if (Mathf.Abs(x + y) <= maxRange && Mathf.Abs(x) <= maxRange && Mathf.Abs(y) <= maxRange) { _reachableHexCoords.Add(new HexCooridnates(x + coords._x, y + coords._y)); } } } } //removes tiles lower than the minimum range if (minRange > 0) { List <HexCooridnates> _closeHex = GetHexesInRange(coords, minRange - 1); foreach (HexCooridnates hex in _closeHex) { _reachableHexCoords.Remove(hex); } } return(_reachableHexCoords); }
//Converts hexcoordinates to the correct spot in the 2D array private int[] HexCoordToArrayindex(HexCooridnates hexcoordinates) { int[] index = new int[2]; index[0] = hexcoordinates._x; index[1] = hexcoordinates._y + (hexcoordinates._x / 2); return(index); }
//returns a list of all the reachable hexes public List <HexCooridnates> GetMoveableHexes(HexCooridnates start, float MaxMovement, int currentFaction) { List <DistanceMarker> _reachableHexes = Flood(new DistanceMarker(start, 0), currentFaction, MaxMovement); List <HexCooridnates> _reachableHexCoords = new List <HexCooridnates>(); foreach (DistanceMarker marker in _reachableHexes) { _reachableHexCoords.Add(marker._hex); } return(_reachableHexCoords); }
//Snaps a unit to the selected hex public void SnapUnit(GameObject unit, HexCooridnates coords) { Transform unitTransform = unit.transform; int[] coordinates = HexCoordToArrayindex(coords); Vector3 position; position.x = coordinates[0] * HexWidth - _xOffset; position.y = this.transform.position.y; position.z = (coordinates[1] + (coordinates[0] * 0.5f) - coordinates[0] / 2) * HexHeight - _yOffset; unitTransform.position = position; Vector3 rotation; rotation.x = 270; rotation.y = 0; rotation.z = unitTransform.rotation.eulerAngles.z; unitTransform.rotation = Quaternion.Euler(rotation); }
public DistanceMarker(HexCooridnates hex, float distance) { _hex = hex; _distance = distance; }
//Returns all targetable hexes private List <HexCooridnates> GetHexesInRange(HexCooridnates coords, float maxRange) { return(GetHexesInRange(coords, 0.0f, maxRange)); }
//Selects a cell public void SelectCell(HexCooridnates coords) { int[] index = HexCoordToArrayindex(coords); _hexes[index[0], index[1]].Colour = Color.red; }