コード例 #1
0
        public void AddGrid(int width = 10, int height = 10)
        {
            SAP_GridSource newGrid = new SAP_GridSource(width, height);

            grids.Add(newGrid);
            newGrid.GridName += " " + grids.IndexOf(newGrid);
        }
コード例 #2
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);
        }
コード例 #3
0
 public SAP_TileData(int x, int y, SAP_GridSource grid)
 {
     gridX         = x;
     gridY         = y;
     gridContainer = grid;
     isWalkable    = true;
 }
コード例 #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);
        }
コード例 #5
0
        public void SwapGrids(int indexA, int indexB)
        {
            SAP_GridSource gridA = grids[indexA];
            SAP_GridSource gridB = grids[indexB];

            grids[indexA] = gridB;
            grids[indexB] = gridA;
        }
コード例 #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));
        }
コード例 #7
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);
        }
コード例 #8
0
        private void Update()
        {
            if (Target != null)
            {
                grid      = pathfinder.GetGrid(Config.GridIndex);
                posInGrid = grid.GetTileDataAtWorldPosition(transform.position).WorldPosition;

                if (transform.hasChanged)
                {
                    isMoving             = true;
                    transform.hasChanged = false;
                }
                else
                {
                    isMoving = false;
                }

                if (GetNextPointDistance < 0)
                {
                    GetNextPointDistance = 0.0001f;
                }

                if (MouseControl)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        Target.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    }
                }

                if (!CanSearch && changeSearch)
                {
                    StopAllCoroutines();
                    path = null;

                    changeSearch = false;
                }
                else if (CanSearch && !changeSearch)
                {
                    StartCoroutine(FindPath());

                    changeSearch = true;
                }

                if (CanMove)
                {
                    Move();
                }
            }
        }
コード例 #9
0
 public void RemoveGrid(SAP_GridSource grid)
 {
     grids.Remove(grid);
 }