Guest User

Untitled

a guest
Sep 28th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Linq;
  6. public class PathfindingMap : MonoBehaviour
  7. {
  8.     public NodeTemplate[] nodeTypes;
  9.     public GameObject NodeMesh;
  10.     public Node[,] mapcords;
  11.     public int width = 10, height = 10;
  12.     public static PathfindingMap Instance;
  13.     PathfindingMap()
  14.     {
  15.         Instance = this;
  16.     }
  17.     public int[,] map =
  18.     {
  19.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  20.         {1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1 },
  21.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  22.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0},
  23.         {0,0,2,2,1,2,2,2,2,0,0,0,2,2,1,2,2,2,2,0 },
  24.         {0,2,2,2,2,2,0,0,0,0,0,2,2,2,2,2,0,0,0,0 },
  25.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
  26.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
  27.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  28.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  29.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  30.         {1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1 },
  31.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  32.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0},
  33.         {0,0,2,2,1,2,2,2,2,0,0,0,2,2,1,2,2,2,2,0 },
  34.         {0,2,2,2,2,2,0,0,0,0,0,2,2,2,2,2,0,0,0,0 },
  35.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
  36.         {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
  37.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  38.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
  39.     };
  40.     public SquareGrid grid = null;
  41.     public Hashtable nodes = new Hashtable();
  42.     void Start()
  43.     {
  44.         grid = new SquareGrid(width, height);
  45.         for (int x = 0; x < width; x++)
  46.         {
  47.             for (int y = 0; y < height; y++)
  48.             {
  49.                 Vector3 pos = new Vector3(x, 0, y);
  50.                 GameObject temp = Instantiate(NodeMesh, pos, NodeMesh.transform.rotation) as GameObject;
  51.                 temp.transform.SetParent(transform);
  52.                 int type = map[x, y];
  53.                 temp.GetComponent<MeshRenderer>().material = nodeTypes[type].mat;
  54.                 grid.nodes[x, y].passable = nodeTypes[type].enterable;
  55.                 /* Node node = ScriptableObject.CreateInstance<Node>();
  56.                  node.cost = nodeTypes[type].Cost;
  57.                  node.cords = pos;
  58.                  node.enterable = nodeTypes[type].enterable;
  59.                  node.inWorld = temp;
  60.                  nodes.Add(pos, node);*/
  61.             }
  62.         }
  63.     }
  64.     public static Node VecToNode(Vector3 vec)
  65.     {
  66.         return (Node)Instance.nodes[vec];
  67.     }
  68.     public static bool UnitCanEnterTile(float x, float y)
  69.     {
  70.         int tx = Mathf.RoundToInt(x);
  71.         int ty = Mathf.RoundToInt(y);
  72.         if (tx > Instance.width || ty > Instance.height)
  73.             return false;
  74.         else
  75.             return Instance.mapcords[tx, ty].enterable;
  76.     }
  77. }
  78. public class Node : ScriptableObject
  79. {
  80.     Node(Vector3 _cords, double _cost)
  81.     {
  82.         cords = _cords;
  83.         cost = _cost;
  84.     }
  85.     public Vector3 cords;
  86.     public GameObject inWorld;
  87.     public double cost;
  88.     public Seeker occupied;
  89.     public bool enterable;
  90.     public List<Node> neighbours = new List<Node>();
  91. }
  92. [System.Serializable]
  93. public class NodeTemplate
  94. {
  95.     public Material mat;
  96.     public double Cost;
  97.     public bool enterable;
  98. }
Add Comment
Please, Sign In to add comment