Beispiel #1
0
 public static void SetUp(int width, int height, bool includeNullElements, bool searchBestPath)
 {
     grid = new GridXY <PathNode>();
     grid.CreateGridXY(width, height, 1, Vector3.zero, true, null, (GridXY <PathNode> grid, int x, int y) => new PathNode(grid, x, y));
     LevelNavigation.includeNullElements = includeNullElements;
     LevelNavigation.searchBestPath      = searchBestPath;
 }
Beispiel #2
0
        private IEnumerator GenerateLevel()
        {
            if (lvlWidth <= 0)
            {
                Debug.LogError($"Can't generate a level with {lvlWidth} width");
                Status = LevelGenerationStatus.Abort;
                yield break;
            }

            if (lvlHeight <= 0)
            {
                Debug.LogError($"Can't generate a level with {lvlHeight} height");
                Status = LevelGenerationStatus.Abort;
                yield break;
            }

            Status             = LevelGenerationStatus.Idle;
            genProgressionPrev = -1;
            genProgression     = 0;
            rnd = new System.Random(DateTime.Now.Millisecond);

            Debug.Log($"Generating level {lvlWidth}x{lvlHeight}");

            newLevel = new GridXY <Element>();
            newLevel.CreateGridXY(lvlWidth, lvlHeight, 1, Vector3.zero, false, Element.NULL, Element.NULL);
            LevelNavigation.SetUp(lvlWidth, lvlHeight, true, false);

            Status = LevelGenerationStatus.Generating;
            yield return(null);

            List <Vector2Int> nodes = new List <Vector2Int>();

            Coroutine generatingNodes = StartCoroutine(GenerateNodes(Mathf.RoundToInt(Mathf.Clamp((lvlWidth * lvlHeight) * (nodesPercentage / 100f), 2, newLevel.Size)), (generatedNodes) => { nodes = generatedNodes; }));

            yield return(generatingNodes);

            yield return(null);

            int       pathsNum        = 0;
            Coroutine generatingPaths = StartCoroutine(GeneratePaths(nodes, (generatedPathsNum) => { pathsNum = generatedPathsNum; }));

            yield return(generatingPaths);

            yield return(null);

            if (pathsNum > 0)
            {
                Status = LevelGenerationStatus.Completed;
            }
            else
            {
                Status = LevelGenerationStatus.Abort;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Restart the current level.
        /// </summary>
        public void Restart()
        {
            GridXY <Element> newLevel = new GridXY <Element>();

            newLevel.CreateGridXY(Grid.Width, Grid.Height, Grid.CellSize, Grid.OriginPosition, false, Element.NULL, Element.NULL);
            for (int x = 0; x < Grid.Width; x++)
            {
                for (int y = 0; y < Grid.Height; y++)
                {
                    newLevel.SetTile(x, y, Grid.GetTile(x, y));
                }
            }
            LoadLevel(newLevel);
        }
Beispiel #4
0
        private void Awake()
        {
            tilemap = GetComponent <Tilemap>();
            grid    = new GridXY <TileVisibility>();

            grid.OnTileChanged += (sender, args) => {
                if (showDebugLog.HasFlag(LevelFogDebug.Setting_Tile))
                {
                    Debug.Log($"Setting Visibility Tile {args.x},{args.y} ({args.value})");
                }

                switch (args.value)
                {
                case TileVisibility.Invisible:
                    HiddenTile?.Invoke(this, new GridCellEventArgs {
                        x = args.x, y = args.y, cell = new Vector2Int(args.x, args.y)
                    });
                    break;

                case TileVisibility.Visible:
                    DiscoveredTile?.Invoke(this, new GridCellEventArgs {
                        x = args.x, y = args.y, cell = new Vector2Int(args.x, args.y)
                    });
                    break;
                }
                tilemap.SetTile(new Vector3Int(args.x, args.y, 0), args.value != TileVisibility.Visible ? visual : null);
                CheckTilesVisibilityAround(args.x, args.y);
            };

            LevelManager.Main.Grid.OnGridCreated += (sender, args) => {
                Debug.Log("Clearing Fog Tilemap");
                tilemap.ClearAllTiles();
                if (LeanTween.isTweening(clusterDiscoveryTweenId))
                {
                    LeanTween.cancel(clusterDiscoveryTweenId, false);
                }
                grid.CreateGridXY(args.width, args.height, 1, Vector3.zero, false, TileVisibility.NULL, TileVisibility.NULL);
            };
            LevelManager.Main.Grid.OnTileChanged += (sender, args) => {
                HideTile(args.x, args.y);
            };

            LevelManager.OnLevelReady += (sender, args) => {
                CheckNullTiles();
            };

            LevelManager.OnLevelPlayable += (sender, args) => {
                DiscoverTile(args.endX, args.endY);
                DiscoverTile(args.endX + 1, args.endY);
                DiscoverTile(args.endX - 1, args.endY);
                DiscoverTile(args.endX, args.endY + 1);
                DiscoverTile(args.endX, args.endY - 1);
            };

            Player.MovedStatic += (sender, args) => {
                DiscoverTile(args.x, args.y);
            };
            Player.StoppedMoveStatic += (sender, args) => {
                DiscoverTile(args.x + 1, args.y);
                DiscoverTile(args.x - 1, args.y);
                DiscoverTile(args.x, args.y + 1);
                DiscoverTile(args.x, args.y - 1);
            };
        }