Guest User

Untitled

a guest
Nov 7th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using System.Collections.Generic;
  5. public class SquareGrid
  6. {
  7.     // Implementation notes: I made the fields public for convenience,
  8.     // but in a real project you'll probably want to follow standard
  9.     // style and make them private.
  10.     public static readonly Direction[] DIRS = new[]
  11.         {
  12.             new Direction(1, 0),
  13.             new Direction(1, 1),
  14.             new Direction(1, -1),
  15.             new Direction(-1, 1),
  16.             new Direction(-1, -1),
  17.             new Direction(0, -1),
  18.             new Direction(-1, 0),
  19.             new Direction(0, 1)
  20.         };
  21.  
  22.     public int width, height;
  23.     public NodeElement[,] nodes;
  24.     public SquareGrid(int width, int height)
  25.     {
  26.         Instance = this;
  27.         nodes = new NodeElement[width, height];
  28.         for (int x = 0; x < width; x++)
  29.         {
  30.             for (int y = 0; y < height; y++)
  31.             {
  32.                 nodes[x, y] = new NodeElement(x, y, 1, true);
  33.             }
  34.         }
  35.         this.width = width;
  36.         this.height = height;
  37.     }
  38.     public static NodeElement GetNodeAt(int x, int y)
  39.     {
  40.         if (InBounds(x, y))
  41.             return Instance.nodes[x, y];
  42.         else
  43.         {
  44.             return null;
  45.         }
  46.     }
  47.     public static bool InBounds(NodeElement id)
  48.     {
  49.         return 0 <= id.x && id.x < Instance.width
  50.             && 0 <= id.y && id.y < Instance.height;
  51.     }
  52.     public static bool InBounds(int x, int y)
  53.     {
  54.         return 0 <= x && x < Instance.width
  55.             && 0 <= y && y < Instance.height;
  56.     }
  57.  
  58.     public double Cost(NodeElement a, NodeElement b)
  59.     {
  60.         double c = b.item2;
  61.         if (a.x != b.x && a.y != b.y)
  62.             c += 0.1f;
  63.         return c;
  64.     }
  65.     public static SquareGrid Instance;
  66.     public static IEnumerable<NodeElement> Neighbors(NodeElement id, Seeker seeker)
  67.     {
  68.         Direction curDir;
  69.         if (seeker._start == id)
  70.         {
  71.             curDir = new Direction(Mathf.Clamp(seeker.LastDestination.x - id.x, -1, 1), Mathf.Clamp(seeker.LastDestination.y - id.y, -1, 1));
  72.         }
  73.         else
  74.         {
  75.             curDir = new Direction(Mathf.Clamp(-seeker.camefrom[id].x + id.x, -1, 1), Mathf.Clamp(-seeker.camefrom[id].y + id.y, -1, 1));
  76.         }
  77.         if (Mathf.Abs(curDir.x) == 1 && curDir.y == 0)
  78.         {
  79.             //horizontal
  80.             NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y);
  81.             NodeElement right = GetNodeAt(id.x, id.y + 1);
  82.             NodeElement left = GetNodeAt(id.x, id.y - 1);
  83.             if (middleForward != null)
  84.             {
  85.                 if (middleForward.Passable())
  86.                 {
  87.                     yield return middleForward;
  88.                 }
  89.             }
  90.             if (!right.Passable())
  91.             {
  92.                 NodeElement rightForward = GetNodeAt(id.x + curDir.x, id.y + 1);
  93.                 if (rightForward != null)
  94.                 {
  95.                     if (rightForward.Passable())
  96.                     {
  97.                         yield return rightForward;
  98.                     }
  99.                 }
  100.             }
  101.             if (!left.Passable())
  102.             {
  103.                 NodeElement leftForward = GetNodeAt(id.x + curDir.x, id.y - 1);
  104.                 if (leftForward != null)
  105.                 {
  106.                     if (leftForward.Passable())
  107.                     {
  108.                         yield return leftForward;
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.         else
  114.         if (Mathf.Abs(curDir.y) == 1 && curDir.x == 0)
  115.         {
  116.             //vertical
  117.             NodeElement middleForward = GetNodeAt(id.x, id.y + curDir.y);
  118.             NodeElement right = GetNodeAt(id.x + 1, id.y);
  119.             NodeElement left = GetNodeAt(id.x - 1, id.y);
  120.             if (middleForward != null)
  121.             {
  122.                 if (middleForward.Passable())
  123.                 {
  124.                     yield return middleForward;
  125.                 }
  126.             }
  127.             if (!right.Passable())
  128.             {
  129.                 NodeElement rightForward = GetNodeAt(id.x + 1, id.y + curDir.y);
  130.                 if (rightForward != null)
  131.                 {
  132.                     if (rightForward.Passable())
  133.                     {
  134.                         yield return rightForward;
  135.                     }
  136.                 }
  137.             }
  138.             if (!left.Passable())
  139.             {
  140.                 NodeElement leftForward = GetNodeAt(id.x - 1, id.y + curDir.y);
  141.                 if (leftForward != null)
  142.                 {
  143.                     if (leftForward.Passable())
  144.                     {
  145.                         yield return leftForward;
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         else
  151.         if (Mathf.Abs(curDir.x) == Mathf.Abs(curDir.y))
  152.         {
  153.             //diagonal
  154.             NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y + curDir.y);
  155.             if (middleForward == null)
  156.             {
  157.                // Debug.LogError(id.x + " " + curDir.x + " " + id.y + " " + curDir.y + " " + seeker.name);
  158.             }
  159.             else
  160.             {
  161.                 NodeElement right = GetNodeAt(middleForward.x - curDir.x, middleForward.y);
  162.                 NodeElement left = GetNodeAt(middleForward.x, middleForward.y - curDir.y);
  163.                 if (middleForward.Passable())
  164.                 {
  165.                     yield return middleForward;
  166.                 }
  167.                 if (right != null)
  168.                 {
  169.                     if (right.Passable())
  170.                     {
  171.                         yield return right;
  172.                     }
  173.                 }
  174.                 if (left != null)
  175.                 {
  176.                     if (left.Passable())
  177.                     {
  178.                         yield return left;
  179.                     }
  180.  
  181.                 }
  182.             }
  183.         }
  184.     }
  185. }
Add Comment
Please, Sign In to add comment