示例#1
0
 private void InitPlayer()
 {
     // Set player in first cell
     if (m_Maze != null)
     {
         IVector2 startLocation = m_Maze.MazeSettings.StartPoint;
         MazeCell startCell     = m_Maze.GetCell(startLocation);
         m_Player.transform.parent = m_Maze.transform;
         m_Player.SetLocation(startCell);
         m_Player.enabled = true;
     }
 }
示例#2
0
        public void GenerateMazePrefab(string nameMaze)
        {
            MazeJSONData mazeJSONData;
            bool         tryLoadMaze = MazeJSONTool.LoadMazeJSON(nameMaze, out mazeJSONData);

            if (tryLoadMaze)
            {
                GameObject obj = new GameObject(nameMaze);
                obj.layer = LayerMask.NameToLayer("Game");
                MazeBase maze = obj.AddComponent <MazeBase>();

                // Maze settings
                MazeSettings mazeSettings = new MazeSettings(mazeJSONData.Columns, mazeJSONData.Rows);
                mazeSettings.NameMaze = nameMaze;
                //mazeSettings.StartPoint = mazeJSONData.StartLocation;
                //mazeSettings.EndPoint = mazeJSONData.EndLocation;

                maze.InitializeMaze(mazeSettings);

                // Create rooms
                Debug.Log("number rooms: " + mazeJSONData.NumberRooms);
                maze.CreateRooms(mazeJSONData.NumberRooms);

                // Create cells
                Debug.Log("number tiles: " + mazeJSONData.ListTiles.Count);
                for (int i = 0; i < mazeJSONData.ListTiles.Count; i++)
                {
                    IVector2 coordsCell = new IVector2(mazeJSONData.ListTiles[i].Coords.Column, mazeJSONData.ListTiles[i].Coords.Row);

                    // Instance cell
                    GameObject goCell = Instantiate(m_CellPrefab);
                    goCell.layer            = LayerMask.NameToLayer("Game");
                    goCell.name             = "Cell_" + coordsCell.Column + "_" + coordsCell.Row;
                    goCell.transform.parent = obj.transform;
                    // Set position tiles
                    goCell.transform.localPosition = new Vector3(coordsCell.Column * m_TileSize, 0.0f, coordsCell.Row * m_TileSize);
                    // Setup index room
                    MazeCell mCell = goCell.GetComponent <MazeCell>();
                    mCell.Coords = coordsCell;

                    // Set type cell
                    mCell.SetTypeCell(mazeJSONData.ListTiles[i].TileType);
                    if (mazeJSONData.ListTiles[i].TileType == MazeCell.ETypeCell.STARTLOCATION)
                    {
                        mazeSettings.StartPoint = coordsCell;
                    }
                    if (mazeJSONData.ListTiles[i].TileType == MazeCell.ETypeCell.ENDLOCATION)
                    {
                        mazeSettings.EndPoint = coordsCell;
                    }



                    /*bool bVisibleTile = true;
                     * if (mazeJSONData.ListTiles[i].IsVisible == 0)
                     * {
                     *  bVisibleTile = false;
                     * }
                     * if (mCell.IsVisible)
                     * {
                     *
                     *  // Set material for start point
                     *  if ((coordsCell.Column == mazeSettings.StartPoint.Column) && (coordsCell.Row == mazeSettings.StartPoint.Row))
                     *  {
                     *      mCell.SetTypeCell(MazeCell.ETypeCell.STARTLOCATION);
                     *  }
                     *
                     *  // Set material for start point
                     *  else if ((coordsCell.Column == mazeSettings.EndPoint.Column) && (coordsCell.Row == mazeSettings.EndPoint.Row))
                     *  {
                     *      mCell.SetTypeCell(MazeCell.ETypeCell.ENDLOCATION);
                     *
                     *  }
                     *  else
                     *  {
                     *      // Check if the coords are in the list of holes
                     *      bool isHole = false;
                     *      for (int iHole = 0; iHole < mazeJSONData.ListHoles.Count; iHole++)
                     *      {
                     *          if ((coordsCell.Column == mazeJSONData.ListHoles[iHole].Coords.Column) && (coordsCell.Row == mazeJSONData.ListHoles[iHole].Coords.Row))
                     *          {
                     *              mCell.SetTypeCell(MazeCell.ETypeCell.HOLE);
                     *              isHole = true;
                     *
                     *              Debug.LogFormat("HOLE DATA: Coords {0}x{1} TypeHole: {2} ConnectedCoords: {3}x{4} ", mazeJSONData.ListHoles[iHole].Coords.Column, mazeJSONData.ListHoles[iHole].Coords.Row, mazeJSONData.ListHoles[iHole].TypeHole, mazeJSONData.ListHoles[iHole].ConnectedCoords.Column, mazeJSONData.ListHoles[iHole].ConnectedCoords.Row);
                     *
                     *              mazeSettings.AddHole(mazeJSONData.ListHoles[iHole]);
                     *              mCell.SetHole(mazeJSONData.ListHoles[iHole]);
                     *              break;
                     *          }
                     *      }
                     *
                     *      if (!isHole)
                     *      {
                     *          mCell.SetTypeCell(MazeCell.ETypeCell.NONE);
                     *      }
                     *  }
                     *
                     *  // Add obstacles
                     *  for (int iObstacle = 0; iObstacle < mazeJSONData.ListObstacles.Count; iObstacle++)
                     *  {
                     *      if ((mCell.Coords.Column == mazeJSONData.ListObstacles[iObstacle].Coords.Column) && (mCell.Coords.Row == mazeJSONData.ListObstacles[iObstacle].Coords.Row))
                     *      {
                     *          // Instance obstacle
                     *          mCell.SetObstacle(mazeJSONData.ListObstacles[iObstacle]);
                     *          break;
                     *      }
                     *  }
                     *
                     * }*/
                    // Add cell to the maze
                    maze.AddCell(mCell);
                }
                // Segup each edge for each cells
                for (int i = 0; i < mazeJSONData.ListTiles.Count; i++)
                {
                    MazeCell currentCell = maze.GetCell(mazeJSONData.ListTiles[i].Coords);

                    if (currentCell.TypeCell != MazeCell.ETypeCell.INVISIBLE)
                    {
                        // Setup each cell
                        for (int iEdge = 0; iEdge < mazeJSONData.ListTiles[i].Edges.Length; iEdge++)
                        {
                            // Gets connected cell with this edge
                            // Gets direction for this edge
                            MazeDirection dir = (MazeDirection)iEdge;

                            IVector2 coordsConnectedCell = mazeJSONData.ListTiles[i].Coords + dir.ToIntVector2();

                            // Get connected cell (it could be null)
                            MazeCell connectedCell = maze.GetCell(coordsConnectedCell);

                            // Get type edge
                            MazeCellEdge.TypeEdgeEnum tEdge = mazeJSONData.ListTiles[i].Edges[iEdge];

                            currentCell.SetEdge(dir, tEdge, connectedCell);
                        }
                    }
                }

#if UNITY_EDITOR
                // Create prefab
                string fileLocation = "Assets/TheMaze/Prefabs/Mazes/" + nameMaze + ".prefab";
                Object emptyObj     = PrefabUtility.CreateEmptyPrefab(fileLocation);

                PrefabUtility.ReplacePrefab(obj, emptyObj, ReplacePrefabOptions.ConnectToPrefab);
#endif
            }
        }