Beispiel #1
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);
        }