Turntable Game Realization

Posted by mhalloran on Mon, 07 Oct 2019 08:03:40 +0200

Effect

thinking

1. Disk movement

Method 1:
At most 3 + 1 = 4 rows of elements appear on a three-line turntable at the same time
At first, I moved four elements in each column down at the same time. When an element moved less than the lower bound, I moved it to the top where it was invisible, so as to achieve circular movement.
This method is feasible, but when encountering elements of different sizes, the rendering hierarchy is problematic.

Therefore, method 2:
Move the nodes of the whole column down, move the standard height of an element, restore it, and pass sprite from its child elements down, and the top element passes in new data.

2. Stop turning

Take the result of a column from the bottom to the top of an array of results, and mark that it has been taken out. Stop moving when there is no value to take out.

Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Game : MonoBehaviour
{
    public Transform root;//Root node
    private int[] itemColStates=new int[6] {0,0,0,0,0,0 };//A column of elements whose state 0 is stationary 1 is rotational 2 is ready to stop
    public Sprite[] itemSprites;//Elemental Picture Collection
    private int[] result = new int[]{0, 0, 7, 2, 3, 4, 5, 0, 7, 2, 3, 4, 5, 0, 7, 2, 3, 4, 5 ,0,7,2,3,4,5};//The first element 0 of the stop result is not used
    public Transform[] itemNodes0;//Column node
    public Transform[] itemNodes1;
    public Transform[] itemNodes2;
    public Transform[] itemNodes3;
    public Transform[] itemNodes4;
    public Transform[] itemNodes5;
    public Transform[] itemColNodes;//Column node
    private Transform[][] itemnodes;//Total node
    public Button start;
    public Button stop;
    private int row = 5;//Row number
    private int col = 6;//Column number
    private float colPosYStart = -235f;//Starting position of each column
    private float colPosYEnd = -352f;//The position of each column when the image is refreshed
    private float speed;//Coordinate values of each frame of element y axis moving when the turntable rotates
    private int[] stopDelay = new int[6];
    // Start is called before the first frame update
    void Start()
    {
        itemnodes = new Transform[][] { itemNodes0, itemNodes1, itemNodes2, itemNodes3, itemNodes4, itemNodes5 };
        speed = (colPosYEnd - colPosYStart) / 3;
        start.onClick.AddListener(() => {
            startGame();
            start.gameObject.SetActive(false);
            stop.gameObject.SetActive(true);
        });
        stop.onClick.AddListener(()=> {
            stopGame(50);
            start.gameObject.SetActive(true);
            stop.gameObject.SetActive(false);
        });
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; j++)
            {
                itemnodes[i][j].GetComponent<Image>().sprite = itemSprites[Random.Range(0, 14)];//Setting Random Pictures
                itemnodes[i][j].GetComponent<Image>().SetNativeSize();//Set the picture to its original size
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        //Traveling through each column
        for (int i = 0; i < col; i++)
        {
            //A column is in a rotating state
            if (itemColStates[i]==1)
            {
                //Move the column down
                itemColNodes[i].localPosition += new Vector3(0,speed);
                //Has reached the bottom
                if (itemColNodes[i].localPosition.y<=colPosYEnd)
                {
                    //Restore the original position of the column
                    itemColNodes[i].localPosition = new Vector3(itemColNodes[i].localPosition.x,colPosYStart);
                    //Replace the picture from bottom to top except the first line
                    for (int j = row-1; j <= 1; j--)
                    {
                        itemnodes[i][j].GetComponent<Image>().sprite = itemnodes[i][j - 1].GetComponent<Image>().sprite;
                        itemnodes[i][j].GetComponent<Image>().SetNativeSize();
                    }
                    itemnodes[i][0].GetComponent<Image>().sprite = itemSprites[Random.Range(0, 14)];//Setting Random Pictures
                    itemnodes[i][0].GetComponent<Image>().SetNativeSize();//Set the picture to its original size
                }
                //Stop Delay Calculation for a Column
                if (stopDelay[i]>0)
                {
                    stopDelay[i]--;
                    //A column should stop spinning
                    if (stopDelay[i]<=0)
                    {
                        itemColStates[i] = 2;//Ready to stop
                    }
                }
            }
            //A column is ready to stop
            else if (itemColStates[i] == 2)
            {
                //Move the column down
                itemColNodes[i].localPosition += new Vector3(0,speed);
                //Has reached the bottom
                if (itemColNodes[i].localPosition.y <= colPosYEnd)
                {
                    //Restore the original position of the column
                    itemColNodes[i].localPosition = new Vector3(itemColNodes[i].localPosition.x, colPosYStart);
                    //Replace the picture from bottom to top except the first line
                    for (int j = row - 1; j <= 1; j--)
                    {
                        //sprite passes down
                        itemnodes[i][j].GetComponent<Image>().sprite = itemnodes[i][j - 1].GetComponent<Image>().sprite;
                        itemnodes[i][j].GetComponent<Image>().SetNativeSize();
                    }
                    int spriteIndex = GetColResult(i);//Get results
                    //The result values are all taken out.
                    if (spriteIndex==-1)
                    {
                        itemColStates[i] = 0;
                    }
                    else
                    {
                        itemnodes[i][0].GetComponent<Image>().sprite = itemSprites[result[spriteIndex]];//Set pictures
                        result[spriteIndex] = -1;//Marked Value
                        itemnodes[i][0].GetComponent<Image>().SetNativeSize();//Set the picture to its original size
                    }
                }
            }
        }
    }
    /// <summary>
    /// open
    /// </summary>
    private void startGame() {
        for (int i = 0; i < col; i++)
        {
            itemColStates[i] = 1;
        }
    }
    /// <summary>
    /// stop
    /// </summary>
    /// <param name="delay"></param>
    private void stopGame(int delay) {
        for (int i = 0; i < col; i++)
        {
            stopDelay[i] = delay + i * 25;//Set stop delay for each column
        }
    }
    /// <summary>
    /// Get a bottom-up result index for a column
    /// </summary>
    /// <param name="col">a column </param>
    /// <returns> Returns the index of the extracted element </returns>.
    private int GetColResult(int col) {
        //The column stops when the last element to the first element is nil, otherwise the value is taken out and the corresponding pattern is displayed on the element.
        int resultCount = this.col * (row - 1);
        int k = resultCount - this.col + col;
        while (k>=1)
        {
            if (result[k]!=-1)
            {
                return k;
            }
            else
            {
                k -= this.col;
            }
        }
        return -1;
    }
}

Topics: less