Ejemplo n.º 1
0
        public List <SAP_TileData> GetNeighborTiles(SAP_TileData tile, bool cutCorners, int radius = 1)
        {
            List <SAP_TileData> neighbors = new List <SAP_TileData>();

            for (int x = -radius; x <= radius; x++)
            {
                for (int y = -radius; y <= radius; y++)
                {
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }
                    int currX = tile.x + x;
                    int currY = tile.y + y;
                    if (currX >= 0 && currY >= 0 && currX < width && currY < height)
                    {
                        SAP_TileData currTile = GetTileDataAt(currX, currY);
                        if (cutCorners == false)
                        {
                            if (CheckCorner(tile, currTile) == true)
                            {
                                continue;
                            }
                        }
                        neighbors.Add(currTile);
                    }
                }
            }
            return(neighbors);
        }
Ejemplo n.º 2
0
 public void CalculateColliders(SAP_TileData startTile = null, SAP_TileData endTile = null)
 {
     foreach (SAP_GridSource grid in grids)
     {
         grid.CalculateColliders(startTile, endTile);
     }
 }
Ejemplo n.º 3
0
        public void CalculateObstacle(Bounds bounds)
        {
            if (Config == null || pathfinder == null)
            {
                return;
            }

            grid = pathfinder.GetGrid(Config.GridIndex);

            float tileRadius = grid.TileDiameter / 2;

            Vector3 minPos = new Vector3
                             (
                bounds.min.x - tileRadius,
                bounds.min.y - tileRadius,
                bounds.min.z
                             );
            Vector3 maxPos = new Vector3
                             (
                bounds.max.x + tileRadius,
                bounds.max.y + tileRadius,
                bounds.max.z
                             );

            SAP_TileData minTile = grid.GetTileDataAtWorldPosition(minPos);
            SAP_TileData maxTile = grid.GetTileDataAtWorldPosition(maxPos);

            grid.CalculateColliders(minTile, maxTile);
        }
Ejemplo n.º 4
0
        public static void WriteData(SAP_GridSource grid, SAP_TileData tileData)
        {
            string filePath = SAP2DPathfinder.singleton.EditorSettings.UserDataFolder;

            if (grid.UserGridData == null)
            {
                //create new user grid data
                string folderPath = filePath.Substring(0, filePath.LastIndexOf('/'));
                string folderName = filePath.Remove(0, folderPath.Length + 1);

                if (!AssetDatabase.IsValidFolder(filePath))
                {
                    AssetDatabase.CreateFolder(folderPath, folderName);
                }
                string fileName = "UserGridData (" + grid.GridName + ")";
                string path     = AssetDatabase.GenerateUniqueAssetPath(filePath + "/" + fileName + ".asset");

                SAP_UserData data = ScriptableObject.CreateInstance <SAP_UserData>();
                AssetDatabase.CreateAsset(data, path);

                grid.UserGridData = AssetDatabase.LoadAssetAtPath(path, typeof(SAP_UserData)) as SAP_UserData;
                EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
            }
            //write data
            grid.UserGridData.AddData(tileData);
            EditorUtility.SetDirty(grid.UserGridData);
        }
Ejemplo n.º 5
0
        public void SetParentTile(SAP_TileData parent, SAP_TileData target)
        {
            if (parent == null || target == null)
            {
                return;
            }
            parentTile = parent;

            CalculateValues(parent, target);
        }
Ejemplo n.º 6
0
        public Vector2[] FindPath(Vector2 from, Vector2 to, SAP2DPathfindingConfig config)
        {
            SAP_GridSource grid        = GetGrid(config.GridIndex);
            SAP_TileData   startTile   = grid.GetTileDataAtWorldPosition(from);
            SAP_TileData   targetTile  = grid.GetTileDataAtWorldPosition(to);
            SAP_TileData   currentTile = startTile;

            if (targetTile.isWalkable == false)
            {
                return(null);
            }

            List <SAP_TileData> openList   = new List <SAP_TileData>();
            List <SAP_TileData> closedList = new List <SAP_TileData>();

            while (closedList.Contains(targetTile) == false)
            {
                List <SAP_TileData> neighbors = grid.GetNeighborTiles(currentTile, config.CutCorners);

                foreach (SAP_TileData neighbor in neighbors)
                {
                    if (neighbor.isWalkable == false || closedList.Contains(neighbor) == true)
                    {
                        continue;
                    }
                    if (openList.Contains(neighbor) == true)
                    {
                        SAP_TileData oldParent = neighbor.ParentTile;
                        int          oldG      = neighbor.G;
                        neighbor.SetParentTile(currentTile, targetTile);
                        if (neighbor.G >= oldG)
                        {
                            neighbor.SetParentTile(oldParent, targetTile);
                        }
                    }
                    else
                    {
                        neighbor.SetParentTile(currentTile, targetTile);
                        openList.Add(neighbor);
                    }
                }
                openList.Remove(currentTile);
                closedList.Add(currentTile);

                currentTile = FindNextTile(openList);

                if (currentTile == null)
                {
                    Debug.LogError("Path not found");
                    return(null);
                }
            }
            return(PathRecovery(startTile, targetTile));
        }
Ejemplo n.º 7
0
 public void AddData(SAP_TileData tile)
 {
     for (int i = 0; i < UnwalkableTiles.Count; i++)
     {
         if (UnwalkableTiles[i].x == tile.x && UnwalkableTiles[i].y == tile.y)
         {
             return;
         }
     }
     UnwalkableTiles.Add(tile);
 }
Ejemplo n.º 8
0
 public void DeleteData(SAP_TileData tile)
 {
     for (int i = 0; i < UnwalkableTiles.Count; i++)
     {
         if (UnwalkableTiles[i].x == tile.x && UnwalkableTiles[i].y == tile.y)
         {
             UnwalkableTiles.RemoveAt(i);
             break;
         }
     }
 }
Ejemplo n.º 9
0
        public static void DeleteData(SAP_GridSource grid, SAP_TileData tileData)
        {
            if (grid.UserGridData == null)
            {
                return;
            }

            //delete data
            grid.UserGridData.DeleteData(tileData);
            EditorUtility.SetDirty(grid.UserGridData);
        }
Ejemplo n.º 10
0
        private Vector2[] PathRecovery(SAP_TileData startTile, SAP_TileData targetTile)
        {
            SAP_TileData current = targetTile;

            List <Vector2> path = new List <Vector2>();

            while (current != startTile)
            {
                path.Add(current.WorldPosition);
                current = current.ParentTile;
            }
            path.Reverse();
            return(path.ToArray());
        }
Ejemplo n.º 11
0
 private void ReadUserData()
 {
     if (UserGridData == null)
     {
         return;
     }
     foreach (SAP_TileData t in UserGridData.UnwalkableTiles)
     {
         int          x    = t.x;
         int          y    = t.y;
         SAP_TileData tile = GetTileDataAt(x, y);
         tile.isWalkable   = false;
         tile.walkableLock = true;
     }
 }
Ejemplo n.º 12
0
        public void CreateGrid(int width, int height)
        {
            width       = Mathf.Clamp(width, 1, width);
            height      = Mathf.Clamp(height, 1, height);
            this.width  = width;
            this.height = height;
            tiles       = new SAP_TileData[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    tiles[x, y] = new SAP_TileData(x, y, this);
                }
            }
            SetGridSize();
            SetCenterPosAccordingToPivot();
        }
Ejemplo n.º 13
0
        private SAP_TileData FindNextTile(List <SAP_TileData> openList)
        {
            if (openList.Count == 0)
            {
                return(null);
            }

            SAP_TileData result = openList[0];

            foreach (SAP_TileData tile in openList)
            {
                if (tile.F < result.F)
                {
                    result = tile;
                }
            }
            return(result);
        }
Ejemplo n.º 14
0
        public void CalculateColliders(SAP_TileData startTile = null, SAP_TileData endTile = null)
        {
            if (startTile == null)
            {
                startTile = GetTileDataAt(0, 0);
            }
            if (endTile == null)
            {
                endTile = GetTileDataAt(Width - 1, Height - 1);
            }
            for (int x = startTile.x; x <= endTile.x; x++)
            {
                for (int y = startTile.y; y <= endTile.y; y++)
                {
                    SAP_TileData tile = GetTileDataAt(x, y);

                    Collider2D[] colls = Physics2D.OverlapCircleAll(tile.WorldPosition, tileDiameter / 4, ObstaclesLayer);

                    if (tile.walkableLock == true)
                    {
                        continue;
                    }

                    if (UsePhysics2D == true)
                    {
                        if (colls.Length > 0)
                        {
                            tile.isWalkable = GetWalkableState(colls);
                        }
                        else
                        {
                            tile.isWalkable = true;
                        }
                    }
                    else
                    {
                        tile.isWalkable = true;
                    }
                }
            }
        }
Ejemplo n.º 15
0
 private bool CheckCorner(SAP_TileData centerTile, SAP_TileData conerTile)
 {
     if (centerTile.x != conerTile.x && centerTile.y != conerTile.y)
     {
         int mixX = Math.Min(centerTile.x, conerTile.x);
         int maxX = Math.Max(centerTile.x, conerTile.x);
         int minY = Math.Min(centerTile.y, conerTile.y);
         int maxY = Math.Max(centerTile.y, conerTile.y);
         for (int x = mixX; x <= maxX; x++)
         {
             for (int y = minY; y <= maxY; y++)
             {
                 SAP_TileData tile = GetTileDataAt(x, y);
                 if (tile.isWalkable == false)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Ejemplo n.º 16
0
 private void CalculateValues(SAP_TileData parent, SAP_TileData target)
 {
     g = (parent.x != gridX && parent.y != gridY) ? parent.G + 14 : parent.G + 10;
     h = (Mathf.Abs(gridX - target.x) + Mathf.Abs(gridY - target.y)) * 10;
     f = g + h;
 }