コード例 #1
0
    public void GenerateTree(LevelSO levelSO)
    {
        int n = levelSO.carpetSOs.Count;

        Carpets = new Carpet[n];

        CarpetRollers = new CarpetRoller[n];

        for (int i = 0; i < n; i++)
        {
            Carpets[i] = Instantiate(carpetPrefab, transform).GetComponent <Carpet>().Init(levelSO.carpetSOs[i]);
            Carpets[i].carpetRoller.RollStateChanged += HandleCarpetRollStateChanged;
            Carpets[i].carpetRoller.BeforeRollIn     += HandleBeforeRollIn;
            Carpets[i].carpetRoller.BeforeRollOut    += HandleBeforeRollOut;
            Carpets[i].InGameIndex = -1;
            Carpets[i].carpetMeshCreator.MeshRebuiltCallback += Carpets[i].carpetRoller.HandleCarpetMeshRebuilt;
            Carpets[i].carpetMeshCreator.RebuildMesh();

            CarpetRollers[i] = Carpets[i].carpetRoller;
        }

        RolledOutCounter = 0;

        var rootCarpet = Carpet.CreateRootNode();

        SolutionTree = Carpet.BuildRefTree(rootCarpet.obj, Carpets);

        CurrentTree = rootCarpet;

        float left = float.MaxValue, right = float.MinValue, top = float.MinValue, bottom = float.MaxValue;

        for (int i = 0; i < n; i++)
        {
            var c = Carpets[i];

            c.SetY(c.GetCorrectHeight());

            if (c.carpetMeshCreator.Bounds.x < left)
            {
                left = c.carpetMeshCreator.Bounds.x;
            }
            if (c.carpetMeshCreator.Bounds.y > right)
            {
                right = c.carpetMeshCreator.Bounds.y;
            }
            if (c.carpetMeshCreator.Bounds.z > top)
            {
                top = c.carpetMeshCreator.Bounds.z;
            }
            if (c.carpetMeshCreator.Bounds.w < bottom)
            {
                bottom = c.carpetMeshCreator.Bounds.w;
            }
        }
        Bounds = new Bounds()
        {
            center = new Vector3((left + right) / 2, 0, (top + bottom) / 2),
            size   = new Vector3((right - left), 0, (top - bottom))
        };

        //Carpet.DisplayRefTree(SolutionTree);

        CarpetTreeController.Reset();

        Vector3 origin = /*new Vector3(levelSO.Position.x, 0, levelSO.Position.y)- */ -Bounds.center;

        transform.localPosition = origin + new Vector3(0, transform.localPosition.y, 0);
    }