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; }
public int CreateBFS(GridXY start) { var bfs = new Dictionary <Vector2, Vector2>(); bfsList.Add(bfs); BreadthFirstSearch(new Vector2(start.x, start.y), bfs); return(bfsList.Count - 1); }
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; } }
private void Awake() { if (Main == null) { Main = this; } else { Destroy(this); } Grid = new GridXY <Element>(); }
/// <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); }
public void LoadTerrain() { // Load data from resource. Texture2D terrain = Resources.Load <Texture2D>("Terrain04"); // Process terrain data and create tile objects; m_Tiles = new TileController[terrain.height, terrain.width]; for (int y = 0; y < terrain.height; y++) { for (int x = 0; x < terrain.width; x++) { ProcessTerrainData(terrain, x, y); } } // Initialise path finding model. GridXY.SetSize(terrain.width, terrain.height); PathFinding.GetInstance().LoadMap(terrain); foreach (var mt in MovingTarget.allTargets) { mt.InitBFS(); } }
public GridXYDebug(GridXY grid) { this.grid = grid; gridTextArray = new TextMeshPro[grid.Width, grid.Height]; }
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); }; }
public static void SetUp(int width, int height, bool includeNullElements, bool searchBestPath, GridXY <Element> gridElements) { LevelNavigation.gridElements = gridElements; SetUp(width, height, includeNullElements, searchBestPath); }
public PathNode(GridXY <PathNode> grid, int x, int y) { this.grid = grid; X = x; Y = y; }
public static List <PathNode> FindPath(Vector2Int startCell, Vector2Int endCell, GridXY <Element> gridElements) { LevelNavigation.gridElements = gridElements; return(FindPath(startCell, endCell)); }
/// <summary> /// Load a new level /// </summary> /// <param name="newLevel">New level.</param> public void LoadLevel(GridXY <Element> newLevel) { if (newLevel == null || newLevel.Size == 0) { Debug.LogWarning("The new level grid is not valid"); return; } Debug.Log($"0. Loading level {newLevel.Width}x{newLevel.Height}..."); ClearLevel(); Debug.Log($"1. Initializing level..."); Grid.CreateGridXY(newLevel.Width, newLevel.Height, 1, Vector3.zero, true, Element.NULL, Element.NULL); Debug.Log("Level is not playable!"); LvlState = LevelState.NotPlayable; OnLevelNotPlayable?.Invoke(this, null); Debug.Log("2. Generating level..."); StartCell = Vector2Int.one * -1; EndCell = Vector2Int.one * -1; bool hasStart = false; bool hasEnd = Grid.Size == 1; Element type; for (int x = 0; x < Grid.Width; x++) { for (int y = 0; y < Grid.Height; y++) { type = newLevel.GetTile(x, y); if (showDebugLog) { Debug.Log($"Setted Tile {x},{y} ({type})"); } Grid.SetTile(x, y, type); if (type == Element.Start) { StartCell = new Vector2Int(x, y); hasStart = true; } else { if (type == Element.End) { EndCell = new Vector2Int(x, y); hasEnd = true; } } } } Debug.Log("Level is ready!"); LvlState = LevelState.Ready; OnLevelReady?.Invoke(this, new OnLevelReadyEventArgs { width = Grid.Width, height = Grid.Height }); if (!hasStart || !hasEnd) { Debug.LogWarning($"Level has {(!hasStart ? "NO": "")} Start and {(!hasEnd ? "NO" : "")} End"); return; } LevelNavigation.SetUp(Grid.Width, Grid.Height, false, true, Grid); Debug.Log("Level is playable!"); LvlState = LevelState.Playable; OnLevelPlayable?.Invoke(this, new OnLevelPlayableEventArgs { startX = StartCell.x, startY = StartCell.y, endX = EndCell.x, endY = EndCell.y }); OnLevelStart?.Invoke(); }