Skip to content
Snippets Groups Projects
BlockHandler.cs 3.08 KiB
Newer Older
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
using TMPro;

public class BlockHandler : MonoBehaviour
{
    public GameObject nblock_input;
    public GameObject block_object;
    public AnimateCircle animateCircle;
    public int max_num_blocks { get; private set; }

    TMP_Dropdown dropdown;
    List<GameObject> block_objects = new List<GameObject>();
    GameObject running_block;
    int current_block_index = 0;

    private Vector3 block_home_position = new Vector3(-104f, -345f, 0f);
    // Start is called before the first frame update
    void Start()
    {
        dropdown = this.GetComponent<TMP_Dropdown>();
        GameObject first_block = GetBlockObjectPrefab();
        block_objects.Add(first_block);
        block_objects[0].SetActive(true);
        running_block = block_objects[0];

        UpdateOptions();
        SetRunningBlockIndex();
        UpdateRunningBlock();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void UpdateOptions()
    {
        UpdateMaxNumBlocks();
        dropdown.ClearOptions();
        for (int i = 1; i < max_num_blocks + 1; i++)
        {
            dropdown.AddOptions(new List<string> { i.ToString() });
        }
    }

    public void UpdateMaxNumBlocks()
    { 
        TMP_InputField input_field;
        input_field = nblock_input.GetComponent<TMP_InputField>();
        max_num_blocks = Int32.Parse(input_field.text);
    }

    public void SetRunningBlockIndex()
    {
        current_block_index = dropdown.value;
        Debug.Log("current block index: " + current_block_index);
    }

    public void UpdateBlockObjects()
    {
        bool is_empty = !block_objects.Any();
        int num_block_objects = 0;
        if (!is_empty)
        {
            num_block_objects = block_objects.Count;
        }
        if (max_num_blocks > num_block_objects)
        {
            for (int i = num_block_objects; i < max_num_blocks; i++)
            {
                block_objects.Add(GetBlockObjectPrefab());
                block_objects[i].SetActive(false);
            }
        }
        else if (max_num_blocks < num_block_objects)
        {
            for (int i = num_block_objects; i > max_num_blocks; i--)
            {
                block_objects.RemoveAt(i);
            }
        }
        current_block_index = 0;
        SetRunningBlock();
        UpdateRunningBlock();
    }

    public void SetRunningBlock()
    {
        running_block.SetActive(false);
        running_block = block_objects[current_block_index];
        running_block.SetActive(true);
    }

    public void UpdateRunningBlock()
    {
        animateCircle.block_obj = running_block;
    }

    GameObject GetBlockObjectPrefab()
    {
        GameObject block = Instantiate(
            block_object,
            new Vector3(0f, 0f, 0f),
            Quaternion.identity
            );
        block.transform.parent = this.transform;
        block.transform.localPosition = block_home_position;
        block.transform.localScale = new Vector3(1f, 1f, 0f);
        return block;
    }
}