/// <summary> /// Retrieves the specified neighbour of this node. /// </summary> /// <param name="node"></param> /// <param name="neighbour"></param> /// <returns>The neighbour or null, if no neighbour is found.</returns> public IHexagonNetNode <T> GetNeighbourFor(IHexagonNetNode <T> node, HexagonNetEnums.Neighbours neighbour) { if (node == null) { throw new ArgumentException("Cannot find the neigbour for the node that is null!"); } if (node.Position == null) { throw new ArgumentException("The node has to be positioned within the HexagonNet!"); } IHexagonNetNode <T> neighbourNode = null; try { var row = net[node.Position.x]; if (row.Shifted) { neighbourNode = GetNeighbourForShiftedRowNode(node, neighbour); } else { neighbourNode = GetNeighbourForNotShiftedRowNode(node, neighbour); } } catch (Exception ex) { Type exType = ex.GetType(); if (exType == typeof(IndexOutOfRangeException) || exType == typeof(KeyNotFoundException)) { // Means we are at the border or the row above/below is not yet added // Simply leave the neighbourNode == null; } else { throw; } } return(neighbourNode); }
private IHexagonNetNode <T> GetNeighbourForNotShiftedRowNode(IHexagonNetNode <T> node, HexagonNetEnums.Neighbours neighbour) { IHexagonNetNode <T> neighbourNode = null; var nodePosition = node.Position; switch (neighbour) { case HexagonNetEnums.Neighbours.UpperLeft: neighbourNode = net[nodePosition.x - 1].Nodes[nodePosition.y - 1]; break; case HexagonNetEnums.Neighbours.UpperRight: neighbourNode = net[nodePosition.x - 1].Nodes[nodePosition.y]; break; case HexagonNetEnums.Neighbours.Left: neighbourNode = net[nodePosition.x].Nodes[nodePosition.y - 1]; break; case HexagonNetEnums.Neighbours.Right: neighbourNode = net[nodePosition.x].Nodes[nodePosition.y + 1]; break; case HexagonNetEnums.Neighbours.LowerLeft: neighbourNode = net[nodePosition.x + 1].Nodes[nodePosition.y - 1]; break; case HexagonNetEnums.Neighbours.LowerRight: neighbourNode = net[nodePosition.x + 1].Nodes[nodePosition.y]; break; } return(neighbourNode); }