Esempio n. 1
0
        public void InstantiateMazeTiles(Grid grid, bool colorByDistance)
        {
            for (int i = _liveMazeTiles.Count - 1; i >= 0; --i)
            {
                Destroy(_liveMazeTiles[i].gameObject);
            }

            _liveMazeTiles.Clear();
            //var djikstraDistances = Maze.CalculateDistancesFromRoot(grid, grid.Cells[0]);
            //var path = Maze.CalculateLongestPath(grid);
            //var path = Maze.CalculatePath(grid, grid.Cells[0], grid.Cells[grid.Cells.Length - 1]);

            var middleCell  = grid.GetCell(grid.Columns / 2, grid.Rows / 2);
            var distances   = Maze.CalculateDistancesFromRoot(grid, middleCell);
            var maxDistance = distances.Values.Max();

            foreach (var cell in grid.Cells)
            {
                if (cell == null)
                {
                    continue;
                }

                MazeTile tile     = GetTilePrefab(cell);
                Vector3  position = new Vector3(cell.Column * 2, 0, cell.Row * 2);
                MazeTile mazeTile = Instantiate(tile, position, tile.transform.rotation);
                _liveMazeTiles.Add(mazeTile);

                if (colorByDistance)
                {
                    var   tileMaterialPropertyBlock = new MaterialPropertyBlock();
                    var   distance = distances[cell];
                    float h, s, v;
                    Color.RGBToHSV(_mazeColor, out h, out s, out v);
                    s = 1 - ((maxDistance - distance) / (float)maxDistance);

                    tileMaterialPropertyBlock.SetColor("_Color", Color.HSVToRGB(h, s, v));

                    var tileRenderer = mazeTile.GetComponent <MeshRenderer>();
                    tileRenderer.SetPropertyBlock(tileMaterialPropertyBlock);
                }
            }
        }
Esempio n. 2
0
        IEnumerator GenerateMaze()
        {
            navMeshGenerator.InitSurfaces(1);
            navMeshGenerator.AddSurface(floor);
            //for(int i=0; i<floorObjects.Count; i++)
            //{
            //    navMeshGenerator.AddSurface(floorObjects[i]);
            //}

            navMeshGenerator.Bake(); // Dynamically bake the floor navmesh (walls are obstacles)

            List <Portal> portals = new List <Portal>();

            tiles = new MazeTile[imageSize.x, imageSize.y];

            int pacDots = 0;

            for (int y = 0; y < imageSize.y; y++)
            {
                for (int x = 0; x < imageSize.x; x++)
                {
                    MazeTile tile = Instantiate(mazeTile);//CreateTile();

                    tile.name = "(" + x + "," + y + ")";

                    //Debug.Log(tile.name);

                    // Move tile to its position
                    tile.transform.localPosition = new Vector3(x, 0, y) * tile.transform.localScale.x;

                    Color c = pixels[x, y];
                    if (CompareColors(c, imageColors[0]))
                    {
                        tile.SetTileType(MazeTile.TileType.PacDot);
                        pacDots++;
                        // Floor/PacDot
                    }
                    else if (CompareColors(c, imageColors[1]))
                    {
                        //Debug.Log("Wall: " + tile.name);
                        tile.SetIsWall(true);
                        // Wall
                    }
                    else if (CompareColors(c, imageColors[2]))
                    {
                        ghostSpawns.Add(new Vector3(x, 0, y));
                        // EnemySpawn
                    }
                    else if (CompareColors(c, imageColors[3]))
                    {
                        //Debug.Log("Spawn Player: " + tile.name);
                        playerSpawn = new Vector3(x, 0, y);
                        //SpawnPlayer(x, y);
                        // PlayerSpawn
                    }
                    else if (CompareColors(c, imageColors[4]))
                    {
                        //Debug.Log("Portal");
                        Portal p = tile.GetComponent <Portal>();
                        if (p != null)
                        {
                            p.enabled = true;
                            tile.GetComponent <Collider>().enabled = true;
                            tile.tag = "Portal";
                            portals.Add(p);
                        }
                        // Portal
                    }
                    else if (CompareColors(c, imageColors[5]))
                    {
                        tile.SetTileType(MazeTile.TileType.PowerPellet);

                        // Power Pellet
                    }
                    else
                    {
                        // Not a color we expect.  Do nothing (treat it like an empty floor)
                        //Debug.Log("Odd color: " + c.r + " "+ c.g + " " + c.b + " at " + tile.name);
                    }
                    //tile.ToggleWall(false);

                    tiles[x, y] = tile;

                    tile.transform.parent = transform;

                    //Debug.Log(tile.name + " Color: " + c);
                }
            }

            // Enable the walls
            // They are smaller than the squares, so to make walls contiguous there are N/S/E/W half-walls
            for (int x = 0; x < imageSize.x; x++)
            {
                for (int y = 0; y < imageSize.y; y++)
                {
                    if (tiles[x, y].IsWall())
                    {
                        MazeTile t = tiles[x, y];
                        t.ToggleWall(MazeTile.WallTypes.Center, true);
                        if (x != 0 && tiles[x - 1, y].IsWall())
                        {
                            t.ToggleWall(MazeTile.WallTypes.West, true);
                        }
                        if (x != imageSize.x - 1 && tiles[x + 1, y].IsWall())
                        {
                            t.ToggleWall(MazeTile.WallTypes.East, true);
                        }
                        if (y != 0 && tiles[x, y - 1].IsWall())
                        {
                            t.ToggleWall(MazeTile.WallTypes.South, true);
                        }
                        if (y != imageSize.y - 1 && tiles[x, y + 1].IsWall())
                        {
                            t.ToggleWall(MazeTile.WallTypes.North, true);
                        }
                        //yield return null;
                    }
                }
            }

            // Spawn the player wherever the tile was
            SpawnPlayer(playerSpawn.x, playerSpawn.z);

            List <Ghost.Controller> ghosts = new List <Ghost.Controller>();


            // Make the ghosts and update the game manager
            GameManager gm = FindObjectOfType <GameManager>();

            gm.SetTotalPacDots(pacDots);
            gm.InitGhosts();
            for (int i = 0; i < ghostSpawns.Count; i++)
            {
                GameObject ghost = Instantiate(ghostPrefab);


                ghost.transform.position = ghostSpawns[i];

                int x = Random.Range(0, sizeof(Ghost.Controller.GhostType));

                Ghost.Controller gc = ghost.GetComponent <Ghost.Controller>();

                // Pick a random ghost
                gc.SetGhostType((Ghost.Controller.GhostType)x);
                gc.area = new Vector2(imageSize.x, imageSize.y);
                gc.SetSpawn(ghost.transform.position);

                ghosts.Add(gc);

                //Debug.Log("Adding ghost: " + gc.name);

                //Debug.Log(gm.name);
                gm.AddGhost(gc);

                ghost.GetComponent <NavMeshAgent>().enabled = true;

                gc.SetRandomDestination();
            }

            // Give all portals access to all portals
            for (int i = 0; i < portals.Count; i++)
            {
                portals[i].AddPortals(portals);
            }

            yield return(null);
        }