Guest User

map

a guest
Sep 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 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.                 Node node = ScriptableObject.CreateInstance<Node>();
  55.                 node.cost = nodeTypes[type].Cost;
  56.                 node.cords = pos;
  57.                 node.enterable = nodeTypes[type].enterable;
  58.                 node.inWorld = temp;
  59.                 if (node.enterable == false)
  60.                 {
  61.                     grid.walls.Add(pos);
  62.                 }
  63.                 nodes.Add(pos, node);
  64.             }
  65.         }
  66.     }
  67.     public static Node VecToNode(Vector3 vec)
  68.     {
  69.         return (Node)Instance.nodes[vec];
  70.     }
  71.     public static bool UnitCanEnterTile(float x, float y)
  72.     {
  73.         int tx = Mathf.RoundToInt(x);
  74.         int ty = Mathf.RoundToInt(y);
  75.         if (tx > Instance.width || ty > Instance.height)
  76.             return false;
  77.         else
  78.             return Instance.mapcords[tx, ty].enterable;
  79.     }
  80. }
  81. public class Node : ScriptableObject
  82. {
  83.     Node(Vector3 _cords, double _cost)
  84.     {
  85.         cords = _cords;
  86.         cost = _cost;
  87.     }
  88.     public Vector3 cords;
  89.     public GameObject inWorld;
  90.     public double cost;
  91.     public Seeker occupied;
  92.     public bool enterable;
  93.     public List<Node> neighbours = new List<Node>();
  94. }
  95. [System.Serializable]
  96. public class NodeTemplate
  97. {
  98.     public Material mat;
  99.     public double Cost;
  100.     public bool enterable;
  101. }
Add Comment
Please, Sign In to add comment