Страница 1 из 1

Синхронизация лабиринта через Photon

СообщениеДобавлено: 16 мар 2020, 16:29
Dnishe
Всем привет
Столкнулся с такой проблемой мне нужно синхронизировать лабиринт через фотон, лабиринт создаётся случайным образом (рандомно). Мне нужно его синхронизировать между игроками. Кто может подсказать как это сделать?
Скрипт Лабиринта (Он не мой)
Синтаксис:
Используется csharp
public class MazeGeneratorCell{
        public int X;
        public int Y;
        public bool WallLeft = true;
        public bool WallBottom = true;
        public bool SpawnPoint = true;
        public bool Visited = false;
}

public class MazeGenerator
{
        public int Width = 23;
        public int Height = 12;
       
        public MazeGeneratorCell[,] GenerateMaze(){
                MazeGeneratorCell[,] maze = new MazeGeneratorCell[Width, Height];
                for(int x = 0; x < maze.GetLength(0); x++){
                        for(int y = 0; y < maze.GetLength(1); y++){
                                maze[x, y] = new MazeGeneratorCell {X=x, Y=y};
                        }
                } //выключения стен верхних и боковых
                for (int x = 0; x < maze.GetLength(0); x++){
            maze[x, Height - 1].WallLeft = false;
                        maze[x, Height - 1].SpawnPoint = false;
        }
        for (int y = 0; y < maze.GetLength(1); y++){
            maze[Width - 1, y].WallBottom = false;
                        maze[Width - 1, y].SpawnPoint = false;
        }

                RemoveWallsWithBacktracker(maze);
                return maze;
        }
       
        private void RemoveWallsWithBacktracker(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[0, 0];
        current.Visited = true;

        Stack<MazeGeneratorCell> stack = new Stack<MazeGeneratorCell>();
        do
        {
            List<MazeGeneratorCell> unvisitedNeighbours = new List<MazeGeneratorCell>();

            int x = current.X;
            int y = current.Y;

            if (x > 0 && !maze[x - 1, y].Visited) unvisitedNeighbours.Add(maze[x - 1, y]);
            if (y > 0 && !maze[x, y - 1].Visited) unvisitedNeighbours.Add(maze[x, y - 1]);
            if (x < Width - 2 && !maze[x + 1, y].Visited) unvisitedNeighbours.Add(maze[x + 1, y]);
            if (y < Height - 2 && !maze[x, y + 1].Visited) unvisitedNeighbours.Add(maze[x, y + 1]);

            if (unvisitedNeighbours.Count > 0)
            {
                MazeGeneratorCell chosen = unvisitedNeighbours[UnityEngine.Random.Range(0, unvisitedNeighbours.Count)];
                RemoveWall(current, chosen);

                chosen.Visited = true;
                stack.Push(chosen);
                current = chosen;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }

    private void RemoveWall(MazeGeneratorCell a, MazeGeneratorCell b)
    {
        if (a.X == b.X)
        {
            if (a.Y > b.Y) a.WallBottom = false;
            else b.WallBottom = false;
        }
        else
        {
            if (a.X > b.X) a.WallLeft = false;
            else b.WallLeft = false;
        }
    }
}

Через другой скрипт он устанавливается на сцену
Синтаксис:
Используется csharp
public class MazeSpawner : MonoBehaviour
{
        public GameObject CellPrefab;
        public Vector2 CellSize = new Vector2(1,1);
        private GameObject[] MazeCell;
       
    private void CreateMaze(){
                MazeGenerator generator = new MazeGenerator();
                MazeGeneratorCell[,] maze = generator.GenerateMaze();
               
                for(int x = 0; x < maze.GetLength(0); x++){
                        for(int y = 0; y < maze.GetLength(1); y++){
                                Cell c = Instantiate(CellPrefab, new Vector2(x * CellSize.x,y * CellSize.y), Quaternion.identity).GetComponent<Cell>();
                                c.WallLeft.SetActive(maze[x,y].WallLeft);
                                c.WallBottom.SetActive(maze[x,y].WallBottom);
                                c.SpawnPoint.SetActive(maze[x,y].SpawnPoint);
                        }
                }
        }
}

Помогите кто может!

Re: Синхронизация лабиринта через Photon

СообщениеДобавлено: 16 мар 2020, 17:02
Jarico
Генерируешь зерно (seed - сид) для лабиринта, скидаешь всем игрокам и готово...