Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IEnumerable<NodeElement> Neighbors(NodeElement id, Seeker seeker)
- {
- Direction curDir;
- if (seeker._start == id)
- {
- //if this is the first node, get the general direction from it to the goal of the path
- curDir = new Direction(Mathf.Clamp(seeker.LastDestination.x - id.x, -1, 1), Mathf.Clamp(seeker.LastDestination.y - id.y, -1, 1));
- }
- else
- {
- //otherwise, get the direction from the seeker's last node to this node
- curDir = new Direction(Mathf.Clamp(-seeker.camefrom[id].x + id.x, -1, 1), Mathf.Clamp(-seeker.camefrom[id].y + id.y, -1, 1));
- }
- //if the direction is horizontal, add forward, and if the sides are unpassable, add their forward nodes too
- //if the direction is vertical, do the same but with y and x reversed
- //if the direction is diagonal, add the forward, left, right node
- if (Mathf.Abs(curDir.x) == 1 && curDir.y == 0)
- {
- //horizontal
- NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y);
- NodeElement right = GetNodeAt(id.x, id.y + 1);
- NodeElement left = GetNodeAt(id.x, id.y - 1);
- if (middleForward != null)
- {
- if (middleForward.Passable())
- {
- yield return middleForward;
- }
- }
- if (!right.Passable())
- {
- NodeElement rightForward = GetNodeAt(id.x + curDir.x, id.y + 1);
- if (rightForward != null)
- {
- if (rightForward.Passable())
- {
- yield return rightForward;
- }
- }
- }
- if (!left.Passable())
- {
- NodeElement leftForward = GetNodeAt(id.x + curDir.x, id.y - 1);
- if (leftForward != null)
- {
- if (leftForward.Passable())
- {
- yield return leftForward;
- }
- }
- }
- }
- else
- if (Mathf.Abs(curDir.y) == 1 && curDir.x == 0)
- {
- //vertical
- NodeElement middleForward = GetNodeAt(id.x, id.y + curDir.y);
- NodeElement right = GetNodeAt(id.x + 1, id.y);
- NodeElement left = GetNodeAt(id.x - 1, id.y);
- if (middleForward != null)
- {
- if (middleForward.Passable())
- {
- yield return middleForward;
- }
- } if (right != null)
- if (!right.Passable())
- {
- NodeElement rightForward = GetNodeAt(id.x + 1, id.y + curDir.y);
- if (rightForward != null)
- {
- if (rightForward.Passable())
- {
- yield return rightForward;
- }
- }
- }
- if (left != null)
- if (!left.Passable())
- {
- NodeElement leftForward = GetNodeAt(id.x - 1, id.y + curDir.y);
- if (leftForward != null)
- {
- if (leftForward.Passable())
- {
- yield return leftForward;
- }
- }
- }
- }
- else
- if (Mathf.Abs(curDir.x) == Mathf.Abs(curDir.y))
- {
- //diagonal
- NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y + curDir.y);
- if (middleForward != null)
- {
- NodeElement right = GetNodeAt(middleForward.x - curDir.x, middleForward.y);
- NodeElement left = GetNodeAt(middleForward.x, middleForward.y - curDir.y);
- if (middleForward.Passable())
- {
- yield return middleForward;
- }
- if (right != null)
- {
- if (right.Passable())
- {
- yield return right;
- }
- }
- if (left != null)
- {
- if (left.Passable())
- {
- yield return left;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement