Example #1
0
        /// <summary>
        /// Interface method to generate additional tile data for the baked grid.
        /// </summary>
        public void GenerateAdditionalTileData(NavTileSurfaceData inData)
        {
            DisplayProgressBar(0f);

            foreach (var tile in inData.Tiles)
            {
                tile.Value.AdditionalData = new AdditionalJPSPlusData();

                IdentifyJumpPoint(tile, inData);
            }

            // Do vertical first.
            DisplayProgressBar(1f / 5f);
            ProcessGridWestwards(inData);
            DisplayProgressBar(2f / 5f);
            ProcessGridEastwards(inData);

            // Horizontal next so the vertical data is available.
            DisplayProgressBar(3f / 5f);
            ProcessGridNorthwards(inData);
            DisplayProgressBar(4f / 5f);
            ProcessGridSouthwards(inData);

#if UNITY_EDITOR
            EditorUtility.ClearProgressBar();
#endif
        }
Example #2
0
        /// <summary>
        /// Processes the grid southwards and assigns all distances per tile in the southward direction.
        /// </summary>
        private void ProcessGridSouthwards(NavTileSurfaceData inData)
        {
            int currentDistance = 0;

            RectInt gridBounds = inData.GridBounds.AsRect();

            Vector2Int coordinate = new Vector2Int();

            for (coordinate.x = gridBounds.xMin; coordinate.x <= gridBounds.xMax; coordinate.x++)
            {
                for (coordinate.y = gridBounds.yMin; coordinate.y <= gridBounds.yMax; coordinate.y++)
                {
                    TileData tile = inData.GetTileData(coordinate);

                    if (tile == null)
                    {
                        continue;
                    }

                    AdditionalJPSPlusData addedData = tile.AdditionalData;

                    if (coordinate.y == gridBounds.yMin ||
                        !inData.IsTileWalkable(coordinate + new Vector2Int(0, -1), 1))
                    {
                        addedData.SetJumpDistance(JumpPointDirection.South, 0);
                    }
                    else if (inData.IsTileWalkable(coordinate + new Vector2Int(0, -1), 1) &&
                             (inData.GetTileData(coordinate + new Vector2Int(0, -1))?.AdditionalData.GetJumpDistance(JumpPointDirection.West) > 0 ||
                              inData.GetTileData(coordinate + new Vector2Int(0, -1))?.AdditionalData.GetJumpDistance(JumpPointDirection.East) > 0))
                    {
                        addedData.SetJumpDistance(JumpPointDirection.South, 1);
                    }
                    else
                    {
                        currentDistance = inData.GetTileData(coordinate + new Vector2Int(0, -1)).AdditionalData.GetJumpDistance(JumpPointDirection.South);

                        if (currentDistance > 0)
                        {
                            addedData.SetJumpDistance(JumpPointDirection.South, currentDistance + 1);
                        }
                        else
                        {
                            addedData.SetJumpDistance(JumpPointDirection.South, currentDistance - 1);
                        }
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Gets the data associated with the scene in the resources folder next to the scene.
        /// </summary>
        /// <param name="inCreateNewIfNotPresent">Creates a new data asset if it was not found.</param>
        /// <returns>The data asset associated with the scene. Can be null if not found and not created.</returns>
        private NavTileSurfaceData GetData(bool inCreateNewIfNotPresent)
        {
            string sceneName = SceneManager.GetActiveScene().name;

            NavTileSurfaceData data = Resources.Load <NavTileSurfaceData>($"NavData-{sceneName}");

            if (data != null)
            {
                return(data);
            }
#if UNITY_EDITOR
            else if (inCreateNewIfNotPresent)
            {
                return(CreateNewDataForCurrentScene());
            }
#endif
            else
            {
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new NavTile data asset for the current scene.
        /// This asset will be located in the resources folder next to the scene in its corresponding folder.
        /// </summary>
        /// <returns>The newly created data asset.</returns>
        private NavTileSurfaceData CreateNewDataForCurrentScene()
        {
            Scene  currentScene = SceneManager.GetActiveScene();
            string sceneName    = currentScene.name;

            string sceneFolderPath     = currentScene.path.Replace($"/{sceneName}.unity", "");
            string resourcesFolderPath = sceneFolderPath + "/Resources";
            string assetPath           = resourcesFolderPath + $"/NavData-{sceneName}.asset";

            if (!AssetDatabase.IsValidFolder(resourcesFolderPath))
            {
                AssetDatabase.CreateFolder(sceneFolderPath, "Resources");
            }

            NavTileSurfaceData data = ScriptableObject.CreateInstance <NavTileSurfaceData>();

            AssetDatabase.CreateAsset(data, assetPath);

            Debug.Log("NavTile data asset succesfully created.", data);

            return(data);
        }
Example #5
0
        /// <summary>
        /// Processes the grid westwards and assigns all distances per tile in the westward direction.
        /// </summary>
        private void ProcessGridWestwards(NavTileSurfaceData inData)
        {
            int      currentDistance = 0;
            TileData relevantTile    = null;

            RectInt gridBounds = inData.GridBounds.AsRect();

            Vector2Int coordinate = new Vector2Int();

            for (coordinate.x = gridBounds.xMin, coordinate.y = gridBounds.yMin; coordinate.x < gridBounds.xMax && coordinate.y < gridBounds.yMax; coordinate.y++)
            {
                for (; coordinate.x < gridBounds.xMax; coordinate.x++)
                {
                    TileData currentTile = inData.GetTileData(coordinate);

                    SetDistance(JumpPointDirection.West, currentTile, ref relevantTile, ref currentDistance);
                }

                coordinate.x    = gridBounds.xMin;
                relevantTile    = null;
                currentDistance = 0;
            }
        }
Example #6
0
        /// <summary>
        /// Checks the grid and marks any jump point.
        /// </summary>
        private void IdentifyJumpPoint(KeyValuePair <Vector2Int, TileData> inTile, NavTileSurfaceData inData)
        {
            JumpPointDirection jumpPointFlag = JumpPointDirection.None;

            if (inTile.Value == null || !inTile.Value.IsWalkable(1))
            {
                return;
            }

            // Hard coded 0 to 3 as there are only 4 cardinal directions.
            for (int i = 0; i <= 3; i++)
            {
                JumpPointDirection parentDirection = (JumpPointDirection)(1 << i);
                Vector2Int         parentPosition  = GetNextPositionInDirection(inTile.Key, parentDirection);

                TileData parentTile = inData.GetTileData(parentPosition);

                if (parentTile == null || !parentTile.IsWalkable(1))
                {
                    continue;
                }

                JumpPointDirection firstPerpendicularDirection;
                JumpPointDirection secondPerpendicularDirection;

                GetPerpendicularDirections(parentDirection, out firstPerpendicularDirection, out secondPerpendicularDirection);

                if ((HasAdjacentTraverseNode(inTile.Key, firstPerpendicularDirection, inData) && !HasAdjacentTraverseNode(parentPosition, firstPerpendicularDirection, inData)) ||
                    HasAdjacentTraverseNode(inTile.Key, secondPerpendicularDirection, inData) && !HasAdjacentTraverseNode(parentPosition, secondPerpendicularDirection, inData))
                {
                    jumpPointFlag |= GetOppositeDirection(parentDirection);
                }
            }

            (inTile.Value.AdditionalData as AdditionalJPSPlusData).JumpPointDirections = jumpPointFlag;
        }
Example #7
0
        /// <summary>
        /// Checks whether the given position has a traversable node in the given direction next to it.
        /// </summary>
        private bool HasAdjacentTraverseNode(Vector2Int inPosition, JumpPointDirection inDirection, NavTileSurfaceData inData)
        {
            Vector2Int adjacentCoordinate = GetNextPositionInDirection(inPosition, inDirection);

            return(inData.IsTileWalkable(adjacentCoordinate, 1));
        }
Example #8
0
 private void OnSceneChanged(Scene inPreviousScene, Scene inNextScene)
 {
     _gridInfo = null;
     _data     = GetData(false);
 }
Example #9
0
 /// <summary>
 /// This function is used to switch data and grid references on scene changes.
 /// </summary>
 private void OnSceneChanged(Scene inScene, OpenSceneMode inMode)
 {
     _gridInfo = null;
     _data     = GetData(false);
 }
Example #10
0
 /// <summary>
 /// Gets the NavTile data associated with the scene or creates if not found.
 /// </summary>
 public void InitDataForCurrentScene()
 {
     _data = GetData(true);
 }