private void _InstantiateHexagon(Vector3Int coords) { Vector3 worldSpacePos = HexMath.HexCoordsToWorldSpace(coords); GameObject HexagonGO = Instantiate(HexagonPrefab, worldSpacePos, Quaternion.identity, gameObject.transform); HexagonGO.name = "Hex " + coords.ToString(); Hexagon hexagon = HexagonGO.AddComponent <Hexagon>(); hexagon.Initialise(coords, 1); HexDict.Add(coords, hexagon); }
//This is hard to make self documenting because of the ordering. //If a hex is not a part of the map, it should be destroyed. //However, you can't remove the hexagon from the HexDict while //iterating through the HexDict, it breaks the foreach loop //So there's an extra list to add the key to, in order to afterwards //remove that key from the hex dict /// <summary> /// Go through every hex in the HexDict and delete if it not part of the map /// </summary> private void _DeleteUnusedHexagons() { List <Vector3Int> destroyedHexes = new List <Vector3Int>(); foreach (KeyValuePair <Vector3Int, IHexagon> hex in HexDict) { if (_ShouldDeleteThisHex(hex.Value)) { _DeleteHex(hex.Value); destroyedHexes.Add(hex.Key); } } foreach (Vector3Int coords in destroyedHexes) { HexDict.Remove(coords); } }
//---------------------------------------------------------------------------- // UpdateHexNeighbours //---------------------------------------------------------------------------- #region UpdateHexNeighbours /// <summary> /// After all hexes have been instantiated and validated /// check in each direction for a valid neighbour /// </summary> public void UpdateHexNeighbours() { List <Vector3Int> directions = GetDirections(); foreach (KeyValuePair <Vector3Int, IHexagon> hex in HexDict) { HashSet <IHexagon> neighbours = new HashSet <IHexagon>(); foreach (Vector3Int direction in directions) { Vector3Int neighbourCoords = hex.Value.MyHexMap.CoOrds + direction; if (HexDict.ContainsKey(neighbourCoords)) { neighbours.Add(HexDict[neighbourCoords]); } } ((IHexagonRestricted)hex.Value).MyHexMapRestricted.SetNeighbours(neighbours); } }