public void HandleMouseButtonDown(int mouseId, Vector3 mousePos)
    {
        if (IsRolling())
        {
            return;
        }
        //horizontal plane only
        var   ray   = Camera.main.ScreenPointToRay(mousePos);
        Plane plane = new Plane(transform.up, 0);

        if (plane.Raycast(ray, out float enter))
        {
            var hitpoint = ray.GetPoint(enter);

            CarpetRoller topHitRoller = null;

            float topMost = float.MinValue;

            if (carpetTree.CarpetRollers == null)
            {
                return;
            }

            foreach (var roller in carpetTree.CarpetRollers)
            {
                Vector3 relativeHitPoint = roller.transform.InverseTransformPoint(hitpoint);

                Vector2 hitpoint2 = new Vector3(relativeHitPoint.x, relativeHitPoint.z);

                if (roller.CheckMouseHitPoint(hitpoint2))
                {
                    if (roller.RolledIn)
                    {
                        topHitRoller = roller;

                        if (!topHitRoller.RolledIn)
                        {
                            break;
                        }
                    }
                    if (topMost < roller.transform.position.y)
                    {
                        topMost = roller.transform.position.y;

                        topHitRoller = roller;
                    }
                }
            }
            if (topHitRoller != null)
            {
                if (topHitRoller.RolledIn)
                {
                    topHitRoller.RollOut();
                }
                else
                {
                    RollInFromNode(topHitRoller.carpet.node);
                }
            }
        }
    }
示例#2
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);
    }
    private void Update()
    {
        if (rootNodeToRoll != null)
        {
            if (rollerToRoll == null)
            {
                var nodeToRoll = RollInOnce(rootNodeToRoll);

                if (nodeToRoll == null)
                {
                    rollerToRoll   = null;
                    rootNodeToRoll = null;
                }
                else
                {
                    rollerToRoll = nodeToRoll.obj.carpetRoller;
                }
            }
            else
            {
                if (!rollerToRoll.rollingIn)
                {
                    rollerToRoll = null;
                }
            }
        }

        if (rollTillMatchedTrigger > 0)
        {
            if (rollerToRoll == null)
            {
                if (!carpetTree.IsUnrolledCarpetsInOrder)
                {
                    var nodeToRoll = RollInOnce(carpetTree.CurrentTree);

                    if (nodeToRoll == carpetTree.CurrentTree || nodeToRoll == null)
                    {
                        rollTillMatchedTrigger--;

                        rollerToRoll = null;
                    }
                    else
                    {
                        rollerToRoll = nodeToRoll.obj.carpetRoller;
                    }
                }
                else
                {
                    rollTillMatchedTrigger--;// = false;

                    RollOutNextHint();
                }
            }
            else
            {
                if (!rollerToRoll.rollingIn)
                {
                    rollerToRoll = null;
                }
            }
        }
    }