Unity魔方拓展脚本(魔方打乱和复原功能)

接上文,因为懒得自己手动打乱魔方,所以写了一个打乱魔方的脚本,还有魔方复原方法,以下是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CubePlayer : MonoBehaviour {

     private enum Model {
        Puzzle = 1,
        Recover = 2
    }

    public Button PuzzleButton;
    public Button RecoverButton;
    
    private const int STEPS = 20;
    private const float CD_TIME = 0.5f;
    private int index = 0;
    private Dictionary StepsDict = new Dictionary();
    private Stack StepsStack = new Stack();
    private List StepsList = new List();
    private bool isInCDTime = false;
    private float timer = 0f;
    private Model model;

    void Start () {
        PuzzleButton.onClick.AddListener(OnClickPuzzleButton);
        RecoverButton.onClick.AddListener(OnClickRecoverButton);

        StepsDict.Add("U","U'");
        StepsDict.Add("U'", "U");
        StepsDict.Add("D", "D'");
        StepsDict.Add("D'", "D");
        StepsDict.Add("F", "F'");
        StepsDict.Add("F'", "F");
        StepsDict.Add("B", "B'");
        StepsDict.Add("B'", "B");
        StepsDict.Add("L", "L'");
        StepsDict.Add("L'", "L");
        StepsDict.Add("R", "R'");
        StepsDict.Add("R'", "R");

        StepsList.Add("U");
        StepsList.Add("U'");
        StepsList.Add("D");
        StepsList.Add("D'");
        StepsList.Add("F");
        StepsList.Add("F'");
        StepsList.Add("B");
        StepsList.Add("B'");
        StepsList.Add("L");
        StepsList.Add("L'");
        StepsList.Add("R");
        StepsList.Add("R'");

    }

    void Update()
    {
        if (isInCDTime) {
            timer -= Time.deltaTime;
        }
        if (timer < 0.01f) {
            isInCDTime = false;
            switch (model)
            {
                case Model.Puzzle:OnClickPuzzleButton();break;
                case Model.Recover:OnClickRecoverButton();break;
                default:
                    break;
            }
        }

    }

    void OnClickPuzzleButton() {
        model = Model.Puzzle;

        int step = Random.Range(0, 12);
        string stepStr = StepsList[step];
        if (index >= STEPS)
            return;
        index++;
        //Debug.Log("index:"+index);
        RotateCube(stepStr);
        StepsStack.Push(StepsDict[stepStr]);
        timer = CD_TIME;
        isInCDTime = true;
    }

    void OnClickRecoverButton()
    {
        model = Model.Recover;

        if (StepsStack.Count <= 0)
            return;
        index--;
        //Debug.Log("index:" + index);
        string stepStr = StepsStack.Pop();
        RotateCube(stepStr);
        timer = CD_TIME;
        isInCDTime = true;
    }

    void RotateCube(string rotateType) {
        switch (rotateType)
        {
            case "U": GameController.Instance.OnClickUBtn();break;
            case "U'": GameController.Instance.OnClickUAntiBtn(); break;
            case "D": GameController.Instance.OnClickDBtn(); break;
            case "D'": GameController.Instance.OnClickDAntiBtn(); break;
            case "F": GameController.Instance.OnClickFBtn(); break;
            case "F'": GameController.Instance.OnClickFAntiBtn(); break;
            case "B": GameController.Instance.OnClickBBtn(); break;
            case "B'": GameController.Instance.OnClickBAntiBtn(); break;
            case "L": GameController.Instance.OnClickLBtn(); break;
            case "L'": GameController.Instance.OnClickLAntiBtn(); break;
            case "R": GameController.Instance.OnClickRBtn(); break;
            case "R'": GameController.Instance.OnClickRAntiBtn(); break;
            default:
                break;
        }
    }

}

 

你可能感兴趣的:(Unity)