Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.Linq;
- public class PathfindingMap : MonoBehaviour
- {
- public NodeTemplate[] nodeTypes;
- public GameObject NodeMesh;
- public Node[,] mapcords;
- public int width = 10, height = 10;
- public static PathfindingMap Instance;
- PathfindingMap()
- {
- Instance = this;
- }
- public int[,] map =
- {
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0},
- {0,0,2,2,1,2,2,2,2,0,0,0,2,2,1,2,2,2,2,0 },
- {0,2,2,2,2,2,0,0,0,0,0,2,2,2,2,2,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0},
- {0,0,2,2,1,2,2,2,2,0,0,0,2,2,1,2,2,2,2,0 },
- {0,2,2,2,2,2,0,0,0,0,0,2,2,2,2,2,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
- {0,0,2,2,2,2,0,0,0,0,0,0,2,2,2,2,0,0,0,0 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
- };
- public SquareGrid grid = null;
- public Hashtable nodes = new Hashtable();
- void Start()
- {
- grid = new SquareGrid(width, height);
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Vector3 pos = new Vector3(x, 0, y);
- GameObject temp = Instantiate(NodeMesh, pos, NodeMesh.transform.rotation) as GameObject;
- temp.transform.SetParent(transform);
- int type = map[x, y];
- temp.GetComponent<MeshRenderer>().material = nodeTypes[type].mat;
- Node node = ScriptableObject.CreateInstance<Node>();
- node.cost = nodeTypes[type].Cost;
- node.cords = pos;
- node.enterable = nodeTypes[type].enterable;
- node.inWorld = temp;
- if (node.enterable == false)
- {
- grid.walls.Add(pos);
- }
- nodes.Add(pos, node);
- }
- }
- }
- public static Node VecToNode(Vector3 vec)
- {
- return (Node)Instance.nodes[vec];
- }
- public static bool UnitCanEnterTile(float x, float y)
- {
- int tx = Mathf.RoundToInt(x);
- int ty = Mathf.RoundToInt(y);
- if (tx > Instance.width || ty > Instance.height)
- return false;
- else
- return Instance.mapcords[tx, ty].enterable;
- }
- }
- public class Node : ScriptableObject
- {
- Node(Vector3 _cords, double _cost)
- {
- cords = _cords;
- cost = _cost;
- }
- public Vector3 cords;
- public GameObject inWorld;
- public double cost;
- public Seeker occupied;
- public bool enterable;
- public List<Node> neighbours = new List<Node>();
- }
- [System.Serializable]
- public class NodeTemplate
- {
- public Material mat;
- public double Cost;
- public bool enterable;
- }
Add Comment
Please, Sign In to add comment