Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class SquareGrid
- {
- // Implementation notes: I made the fields public for convenience,
- // but in a real project you'll probably want to follow standard
- // style and make them private.
- public static readonly Direction[] DIRS = new[]
- {
- new Direction(1, 0),
- new Direction(1, 1),
- new Direction(1, -1),
- new Direction(-1, 1),
- new Direction(-1, -1),
- new Direction(0, -1),
- new Direction(-1, 0),
- new Direction(0, 1)
- };
- public int width, height;
- public NodeElement[,] nodes;
- public SquareGrid(int width, int height)
- {
- Instance = this;
- nodes = new NodeElement[width, height];
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- nodes[x, y] = new NodeElement(x, y, 1, true);
- }
- }
- this.width = width;
- this.height = height;
- }
- public static NodeElement GetNodeAt(int x, int y)
- {
- if (InBounds(x, y))
- return Instance.nodes[x, y];
- else
- {
- return null;
- }
- }
- public static bool InBounds(NodeElement id)
- {
- return 0 <= id.x && id.x < Instance.width
- && 0 <= id.y && id.y < Instance.height;
- }
- public static bool InBounds(int x, int y)
- {
- return 0 <= x && x < Instance.width
- && 0 <= y && y < Instance.height;
- }
- public double Cost(NodeElement a, NodeElement b)
- {
- double c = b.item2;
- if (a.x != b.x && a.y != b.y)
- c += 0.1f;
- return c;
- }
- public static SquareGrid Instance;
- public static IEnumerable<NodeElement> Neighbors(NodeElement id, Seeker seeker)
- {
- Direction curDir;
- if (seeker._start == id)
- {
- curDir = new Direction(Mathf.Clamp(seeker.LastDestination.x - id.x, -1, 1), Mathf.Clamp(seeker.LastDestination.y - id.y, -1, 1));
- }
- else
- {
- curDir = new Direction(Mathf.Clamp(-seeker.camefrom[id].x + id.x, -1, 1), Mathf.Clamp(-seeker.camefrom[id].y + id.y, -1, 1));
- }
- 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.Passable())
- {
- NodeElement rightForward = GetNodeAt(id.x + 1, id.y + curDir.y);
- if (rightForward != null)
- {
- if (rightForward.Passable())
- {
- yield return rightForward;
- }
- }
- }
- 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)
- {
- Debug.LogError(id.x + " " + curDir.x + " " + id.y + " " + curDir.y + " " + seeker.name);
- }
- NodeElement right = GetNodeAt(middleForward.x - curDir.x, middleForward.y);
- NodeElement left = GetNodeAt(middleForward.x, middleForward.y - curDir.y);
- if (middleForward != null)
- {
- if (middleForward.Passable())
- {
- yield return middleForward;
- }
- if (right != null)
- {
- if (right.Passable())
- {
- yield return right;
- }
- }
- if (left != null)
- {
- if (left.Passable())
- {
- yield return left;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment