public void Awake() { pathMapDict = new Dictionary <BlockingType, PathMap>(); //for each list of blocking layers (assumes BlockingType enum starts at 0) PathMap pMap; for (int t = 0; t < tilemapMatrix.lists.Length; t++) { if (tilemapMatrix.lists[t].tileMaps.Length != 0) { //generate a PathMap pMap = MapConverter.TilemapArrToPathMap(tilemapMatrix[(BlockingType)t].tileMaps, background.cellBounds); //add to dictionary, with the current BlockingType as a key pathMapDict.Add((BlockingType)t, pMap); } } }
/* * ==== DESCRIPTION ==== * * A Navigator instance is used by any object that wishes to use pathfinding. * The Navigator acts as a mediator between Tilemaps in a scene and pathfinding algorithms, * and makes extensive use of algorithms in the static MapConverter class to perform conversions. * * The Navigator is used by calling "GetWorldPath". * This provides a suggested path, each entry being a NodeGrid node's world coordinates. * Objects trying to navigate should pass in their current location and their destination, * then attempt to *directly* move to each node in turn (likely moving on to the next node * once arriving within a certain distance of the current node). * Calls should be made roughly every time an object's target destination changes. * This may be very often if chasing a moving object, so effort should be made to limit calls. * * The Navigator is handy because its inputs and outputs are world coordinates, so objects * have an easy time communicating with it. * * The Navigator also generates and stores all important pathfinding data (PathMaps and their contents), * so that each object/actor using pathfinding does not need its own copy, which saves memory. * */ /// <summary> /// Get a world location to walk towards, in order to reach the desired destination. /// </summary> /// <param name="pointA">Entity's current world location.</param></param> /// <param name="PointB">World location of final destination.</param> public Vector3[] GetWorldPath(BlockingType bType, Vector3 pointA, Vector3 pointB) { PathMap pMap = pathMapDict[bType]; Vector3Int pA_vec = MapConverter.WorldToNode(pMap, pointA); Vector3Int pB_vec = MapConverter.WorldToNode(pMap, pointB); Point pA = new Point(pA_vec.x, pA_vec.y); Point pB = new Point(pB_vec.x, pB_vec.y); List <Point> pointList = Pathfinder.FindPath(pMap.nodeGrid, pA, pB); Vector3[] coordsList = new Vector3[pointList.Count]; int i = 0; pointList.ForEach(delegate(Point p){ coordsList[i++] = MapConverter.NodeToWorld(pMap, p.x, p.y); }); return(coordsList); }
/* * ==== INITIALIZATION ==== * * A Navigator must generate nodes and grids based on the tilemaps in a scene. * */ //regenerates pathMap, accounting for changes in blocking tilemaps. //probably unnecessary public void RefreshPathMap(BlockingType t) { pathMapDict[t] = MapConverter.TilemapArrToPathMap(tilemapMatrix[t].tileMaps, background.cellBounds); }