Пример #1
0
 private void GetAllCubesAffectedByPivot(CubeRotationAxis pivot, int index, ref List <IndividualCubeController> cubesToReturn)
 {
     if (pivot == CubeRotationAxis.XAxis)
     {
         cubesToReturn.AddRange(this.allCubes.FindAll(item => { return(item.currentCoordinates.x == index); }));
     }
     else if (pivot == CubeRotationAxis.YAxis)
     {
         cubesToReturn.AddRange(this.allCubes.FindAll(item => { return(item.currentCoordinates.y == index); }));
     }
     else if (pivot == CubeRotationAxis.ZAxis)
     {
         cubesToReturn.AddRange(this.allCubes.FindAll(item => { return(item.currentCoordinates.z == index); }));
     }
 }
Пример #2
0
    public PivotRotationActions RotatePivot(CubeRotationAxis chosenAxis, int pivotIndex, bool directionUpOrLeft)
    {
        List <IndividualCubeController> cubesToRotate = new List <IndividualCubeController>(gameSize * gameSize);
        Vector3    rotationCentre;
        Quaternion targetQuaternion;
        float      rotationDirection = directionUpOrLeft ? 1f : -1f;

        Vector3 axis = Vector3.zero;

        switch (chosenAxis)
        {
        case CubeRotationAxis.XAxis:
            axis = new Vector3(1, 0, 0);
            break;

        case CubeRotationAxis.YAxis:
            axis = new Vector3(0, 1, 0);
            break;

        case CubeRotationAxis.ZAxis:
            axis = new Vector3(0, 0, 1);
            break;

        default:
            Debug.LogError("Invalid axis");
            break;
        }

        GetAllCubesAffectedByPivot(chosenAxis, pivotIndex, ref cubesToRotate);
        rotationCentre   = -pivotIndex * individualCubeSize * axis;
        targetQuaternion = Quaternion.AngleAxis(rotationDirection * 90, axis);

        // I will now parent all the cubes to be rotated into a new temporary GameObject
        // So they can be rotated together

        // You can do this with pure math
        // but this way makes the code simpler for not much overhead
        // and allows us to animate all the cubes at once with minimal effort
        var rotationalAidGO = new GameObject();

        rotationalAidGO.transform.position = rotationCentre;

        foreach (var cube in cubesToRotate)
        {
            cube.transform.SetParent(rotationalAidGO.transform, true);
        }


        Quaternion startRotation = rotationalAidGO.transform.rotation;

        //You can do really cool things here in the pivoting action
        //to animate the rotation, all that matters is that evertything
        //is in the right place for the final action
        return(new PivotRotationActions()
        {
            startAction = () =>
            {
            },
            pivotingAction =
                time =>
            {
                var currQ = Quaternion.Slerp(startRotation, startRotation * targetQuaternion, time);

                rotationalAidGO.transform.rotation = currQ;
            },

            finalAction = () =>
            {
                //Update the cube's current coordinates
                foreach (var cube in cubesToRotate)
                {
                    cube.transform.SetParent(this.cubesParent, true);
                    cube.currentCoordinates = WorldCoordinatesToCube(cube.transform.position);
                    cube.transform.localRotation = cube.transform.localRotation.RoundAngle(90);
                }
                GameObject.Destroy(rotationalAidGO);
            }
        });
    }