Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

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

public class PanelCreater : MonoBehaviour
{
    public int maxArrow;
    public int waitTime;

    public int maxColumn;
    public int maxRow;

    public int maxWaveCount;
    private int currentWaveCount = 0;

    //private float timer;
    public int oneWaveTimeLimit;
    private float waveTimer;

    public AnimalPanelItem animalPanelItemPrefab;
    public Transform animalPanelGenerateTran;

    public PanelItem panelItemPrefab;
    public Transform panelGenerateTran;

    public List<PanelItem> panelItemList = new List<PanelItem>();
    public List<AnimalPanelItem> animalPanelList = new List<AnimalPanelItem>();

    public PlayerController playerController;
    public UIController uIController;

    public enum GameState {
        Wait,
        Play,
        Game_Up,
    }
    public GameState gameState;

    IEnumerator Start()
    {
        yield return new WaitForSeconds(0.5f);

        gameState = GameState.Wait;

        StartCoroutine(NextWave());
    }

    void Update()
    {
        if (gameState == GameState.Game_Up) {
            return;
        }

        if (gameState == GameState.Wait) {
            return;
        }

        waveTimer -= Time.deltaTime;
        uIController.txtWaveTimer.text = waveTimer.ToString("F0");

        if (waveTimer <= 0) {
            if (currentWaveCount >= maxWaveCount) {
                gameState = GameState.Game_Up;
                uIController.DisplayaGameUp();
                return;
            }
            gameState = GameState.Wait;
            StartCoroutine(NextWave());
        }
    }

    public void NextPanels() {
        DestroyPanels();
        GenerateArrowPanels();
    }

    /// <summary>
    /// PanelItem¤ÎÇË´þ
    /// </summary>
    private void DestroyPanels() {
        if (panelItemList.Count <= 0) {
            return;
        }

        for (int i = 0; i < panelItemList.Count; i++) {
            Destroy(panelItemList[i].gameObject);
        }
        panelItemList.Clear();
    }

    /// <summary>
    /// PanelItem¤ÎÀ¸À®
    /// </summary>
    private void GenerateArrowPanels() {
        for (int i = 0; i < maxArrow; i++) {
            PanelItem panelItem = Instantiate(panelItemPrefab, panelGenerateTran, false);
            panelItem.SetupPanel(playerController, this);
            panelItemList.Add(panelItem);
        }
    }

    private IEnumerator NextWave() {
        if (currentWaveCount != 0) {
            DestroyAnimalPanels();

            DestroyPanels();

            playerController.ResetPosition();
        }
        uIController.ActivateReloadButton(false);

        yield return StartCoroutine(GenerateAnimalPanaels());

        waveTimer = oneWaveTimeLimit;
        currentWaveCount++;

        StartCoroutine(uIController.DisplayWaveStart(currentWaveCount));

        yield return new WaitForSeconds(1.5f);

        GenerateArrowPanels();

        uIController.ActivateReloadButton(true);

        gameState = GameState.Play;
    }

    /// <summary>
    /// AnimalPanelItem¤ÎÇË´þ
    /// </summary>
    private void DestroyAnimalPanels() {
        for (int i = 0; i < animalPanelList.Count; i++) {
            Destroy(animalPanelList[i].gameObject);
        }
        animalPanelList.Clear();
    }

    /// <summary>
    /// AnimalPanelItem¤ÎÀ¸À®
    /// </summary>
    private IEnumerator GenerateAnimalPanaels() {
        for (int x = 0; x < maxColumn; x++) {
            for (int y = 0; y < maxRow; y++) {
                AnimalPanelItem animalPanelItem = Instantiate(animalPanelItemPrefab, animalPanelGenerateTran, false);
                animalPanelItem.SetUpAnimalPanel(Random.Range(0,7), 100);
                animalPanelList.Add(animalPanelItem);
                yield return new WaitForSeconds(0.2f);
            }
        }
    }
}

using UnityEngine;
using UnityEngine.UI;

public class PanelItem : MonoBehaviour
{
    public Button btnPanel;
    public Image imgPanel;
    public ArrowDirectionType ArrowDirectionType;
    
    private bool isClickable;
    private PlayerController playerController;
    private PanelCreater panelCreater;

    public void SetupPanel(PlayerController playerController, PanelCreater panelCreater) {
        isClickable = true;

        this.playerController = playerController;
        this.panelCreater = panelCreater;
        
        FetchArrowDirection();

        btnPanel.onClick.AddListener(OnClickPanel);

        isClickable = false;
    }

    /// <summary>
    /// Ìð¤ÎÊý¸þ¾ðÊó¤È¥¤¥á¡¼¥¸¤ÎÊý¸þ¤ò°ìÃפµ¤»¤ë
    /// </summary>
    private void FetchArrowDirection() {
        // Ìð¤ÎÊý¸þ¤ò¥é¥ó¥À¥à¤Ç·èÄê
        int randomDirection = Random.Range(0, (int)ArrowDirectionType.Count);

        // Ìð¤Î¥¤¥á¡¼¥¸¤ÈÊý¸þ¤Î¾ðÊó¤ò°ìÃ×
        imgPanel.transform.rotation = Quaternion.Euler(0, 0, randomDirection * 45);
        ArrowDirectionType = (ArrowDirectionType)randomDirection;
    }

    private void OnClickPanel() {
        if (isClickable) {
            return;
        }
        isClickable = true;
        btnPanel.interactable = false;
        playerController.JudgeMove(ArrowDirectionType);
        panelCreater.NextPanels();
    }
}

public enum ArrowDirectionType {
    Right_Middle,     // 0 Åì
    Right_Top,        // 1 ËÌÅì
    Center_Top,       // 2 ËÌ
    Left_Top,         // 3 ËÌÀ¾
    Left_Middle,      // 4 À¾
    Left_Bottom,      // 5 ÆîÀ¾
    Center_Bottom,    // 6 Æî
    Right_Bottom,     // 7 ÆîÅì
    Count
}

using UnityEngine;
using UnityEngine.UI;

public class AnimalPanelItem : MonoBehaviour
{
    public Image imgAnimal;
    public int animalNo;
    public int score;
    public bool isGetPanel;
    public BoxCollider2D boxCollider2D;

    public void SetUpAnimalPanel(int no, int score)
    {
        animalNo = no;
        this.score = score;
        imgAnimal.sprite = Resources.Load<Sprite>("animal_" + animalNo);
    }

    public void GetPanel() {
        imgAnimal.color = new Color(1, 1, 1, 0.25f);
        isGetPanel = true;
        boxCollider2D.enabled = false;
    }
}

using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private int currentAnimalNo = 0;     // ¸½ºß¤ÎAnimalPanel¤ÎNo¡£¥³¥ó¥ÜȽÄê¤Ë»ÈÍѤ¹¤ë
    private int comboCount;              // ¸½ºß¤Î¥³¥ó¥Ü¿ô¡£°Û¤Ê¤ëAnimalNo¤ÎAnimalPanel¤òÄ̲᤹¤ë¤È¥¢¥Ã¥×¡£Æ±¤¸No¤òÄ̲᤹¤ë¤È¥ê¥»¥Ã¥È
    private int totalScore;              // ¸½ºß¤Î¥È¡¼¥¿¥ë¥¹¥³¥¢¡£Wave¶¦ÄÌ
    private Vector3 startPos;            // Player¤Î¥¹¥¿¡¼¥ÈÃÏÅÀÅÐÏ¿ÍÑ¡£Wave¤¬ÀÚ¤êÂؤï¤ëÅ٤ˤ³¤ÎÃÏÅÀ¤ËÌá¤ë
    private float moveAmount;            // Player¤Î¥Ñ¥Í¥ë¤Î°ÜÆ°ÎÌ

    public BoxCollider2D boxCol;         // Player¤ÎBoxCollier2D¤ò¥¢¥µ¥¤¥ó¤¹¤ë
    public UIController uIController;    // ¥Ò¥¨¥é¥ë¥­¡¼¾å¤Ë¤¢¤ëUIControler¤ò¥¢¥µ¥¤¥ó¤¹¤ë
    public LayerMask wallLayer;          // Wall¥ì¥¤¥ä¡¼¤ò¥¢¥µ¥¤¥ó¤¹¤ë

    Dictionary<ArrowDirectionType, Vector3> moveDirection;   // Dictionary¤ÎÀë¸À

    void Start() {
        // ¥¹¥¿¡¼¥ÈÃÏÅÀ¤òÅÐÏ¿
        startPos = transform.position;

        // Player¤Î°ÜÆ°Î̤òPlayer¥ª¥Ö¥¸¥§¥¯¥È¤ÎÂ礭¤µ¤«¤é¼èÆÀ
        moveAmount = GetComponent<RectTransform>().sizeDelta.x;

        // °ÜưȽÄêÍѤÎDictionay¤òÍÑ°Õ
        SetUpMoveDirection();
    }

    /// <summary>
    /// ¸þ¤­¤ÎEnum¤È¡¢¤½¤ì¤ËÂбþ¤¹¤ëÊý¸þ¾ðÊó¤ò¥»¥Ã¥È¤·¤ÆDictionay¤Ë½ç¼¡ÅÐÏ¿
    /// </summary>
    private void SetUpMoveDirection() {
        // ½é´ü²½
        moveDirection = new Dictionary<ArrowDirectionType, Vector3>();

        // ¸þ¤­¤ÎEnum¤ÈÊý¸þ¾ðÊó¤ò£±¥»¥Ã¥Èñ°Ì¤ÇÅÐÏ¿
        moveDirection.Add(ArrowDirectionType.Right_Middle, new Vector3(moveAmount, 0, 0));
        moveDirection.Add(ArrowDirectionType.Right_Top, new Vector3(moveAmount, moveAmount, 0));
        moveDirection.Add(ArrowDirectionType.Center_Top, new Vector3(0, moveAmount, 0));
        moveDirection.Add(ArrowDirectionType.Left_Top, new Vector3(-moveAmount, moveAmount, 0));
        moveDirection.Add(ArrowDirectionType.Left_Middle, new Vector3(-moveAmount, 0, 0));
        moveDirection.Add(ArrowDirectionType.Left_Bottom, new Vector3(-moveAmount, -moveAmount, 0));
        moveDirection.Add(ArrowDirectionType.Center_Bottom, new Vector3(0, -moveAmount, 0));
        moveDirection.Add(ArrowDirectionType.Right_Bottom, new Vector3(moveAmount, -moveAmount, 0));
    }

    /// <summary>
    /// WaveÀÚ¤êÂؤ¨»þ¤ËPlayer¤ò¥¹¥¿¡¼¥ÈÃÏÅÀ¤ËÌ᤹
    /// </summary>
    public void ResetPosition() {
        transform.position = startPos;
    }

    /// <summary>
    /// Ìð°õ¥Ñ¥Í¥ë¤ÎÊý¸þ¤Ë°ÜÆ°²Äǽ¤«¤É¤¦¤«¤òȽÄê
    /// </summary>
    /// <param name="arrowDirectionType"></param>
    public void JudgeMove(ArrowDirectionType arrowDirectionType) {

        // Ìá¤êÃͤòÍøÍѤ·¤Æ¡¢ºîÀ®¤·¤¿Dictionary¤ÎÃ椫¤é¡¢¸þ¤­¤ÎEnum¤«¤éÊý¸þ¾ðÊó¤ò¼èÆÀ
        Vector3 judgeDirection = SearchDirectionFromDictionary(arrowDirectionType);

        // Player¤Î°ÌÃÖ¤ÈÊý¸þ¤ò¼èÆÀ
        Vector3 playerPos = transform.position;

        // Ray¤ò»È¤Ã¤Æ°ÜÆ°Àè¤ËÊɤ¬¤¢¤ë¤«È½Äê
        RaycastHit2D raycastHit2D = Physics2D.Raycast(playerPos, judgeDirection, 1, wallLayer);

        // Ray¤òScene¥×¥ì¥Ó¥å¡¼Æâ¤Ë²Ä»ë²½
        Debug.DrawRay(playerPos, judgeDirection, Color.red, 1);

        // Wall¤Ç¤¢¤Ã¤¿¾ì¹ç¡¢Debug¤È¤·¤ÆÆâÍƤòɽ¼¨¤¹¤ë
        Debug.Log(raycastHit2D.collider);

        // °ÜÆ°À褬Êɤξì¹ç¡¢°ÜÆ°¼ºÇÔ¤ÎȽÄê¤ò¤¹¤ë¡Êraycast2DÊÑ¿ôÆâ¤Ë¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤¬³ÊǼ¤µ¤ì¤ë¤Î¤Ç¡¢°ÜÆ°½èÍý¤ò¤·¤Ê¤¤¡Ë
        if (raycastHit2D.collider != null) {
            // °ÜÆ°¼ºÇÔ¤ÎSEÌĤ餹
            return;
        }

        // °ÜưȽÄê¤ËÀ®¸ù¤·¤¿¤Î¤ÇPlayer¤ò°ÜÆ°
        ActionMove(judgeDirection);
    }

    /// <summary>
    /// Dicitonay¤Ç¤¢¤ëmoveDirection¤ò¸¡º÷¤·¤Æ¡¢¸þ¤­¤ÎEnum¤«¤éVector3¤ÎÊý¸þ¾ðÊó¤ò¼èÆÀ¤·¤ÆÌ᤹
    /// </summary>
    /// <param name="arrowDirectionType"></param>
    /// <returns></returns>
    private Vector3 SearchDirectionFromDictionary(ArrowDirectionType arrowDirectionType) {
        // ÊÑ¿ô¤òÍÑ°Õ
        Vector3 direction = new Vector3(0, 0, 0);

        // Dicitonay¤ò¸¡º÷¤·¡¢°ú¿ô¤ÎEnum¤ÈKey¤ÎEnum¤¬°ìÃפ·¤¿¤é¡¢¤½¤ÎÊý¸þ¾ðÊó¡ÊValue¡Ë¤ò¼èÆÀ
        foreach (KeyValuePair<ArrowDirectionType, Vector3> item in moveDirection) {
            if (item.Key == arrowDirectionType) {
                // ÃͤòÂåÆþ¤·¤ÆÌ᤹¡Ê¤³¤³¤Ç½èÍý¤Ï½ªÎ»¡Ë
                return direction = item.Value;
            }
        }
        return direction;
    }

    /// <summary>
    /// Player¤ò°ÜÆ°¤¹¤ë
    /// </summary>
    /// <param name="currentMoveDirection"></param>
    private void ActionMove(Vector3 currentMoveDirection) {
        // Player¤ò°ÜÆ°
        transform.localPosition += currentMoveDirection;
        // Player¤ÎÅö¤¿¤êȽÄê¤ò¥ª¥ó¤Ë¤¹¤ë(°ÜÆ°¸å¤Ë¥ª¥ó¤Ë¤¹¤ë¤³¤È¤Ç¡¢¤½¤Î´Ö¤ÏÅö¤¿¤êȽÄê¤ò¼è¤é¤Ê¤¤¤è¤¦¤Ë¤¹¤ë)
        boxCol.enabled = true;
    }

    private void OnTriggerEnter2D(Collider2D col) {
        // Åö¤¿¤êȽÄê¤ËÆþ¤Ã¤¿°ÜÆ°Àè¤Î¥Ñ¥Í¥ë¤¬AnimalPanelItem¤ò»ý¤Ã¤Æ¤¤¤ë¤«³Îǧ¤·¤ÆÊÑ¿ô¤ËÂåÆþ
        if (col.gameObject.TryGetComponent(out AnimalPanelItem animalPanelItem)) {

            // AnimalPanelItem¥¯¥é¥¹¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¥Ñ¥Í¥ë¤Ê¤é½èÍý¤ò»ß¤á¤ë
            if (animalPanelItem.isGetPanel) {            
                return;
            }

            // ¥³¥ó¥ÜȽÄê¤ò¹Ô¤¦¡£º£¤Þ¤Ç¤¤¤¿¥Ñ¥Í¥ë¤ÎAnimalNo¤È¿·¤·¤¯°ÜÆ°¤·¤¿Àè¤Î¥Ñ¥Í¥ë¤ÎAnimalNo¤òÈæ¤Ù¤Æ°ã¤¦¾ì¹ç¤Ë¤Ï¥³¥ó¥ÜÀ®¸ù
            if (currentAnimalNo != animalPanelItem.animalNo) {                
                comboCount++;
            } else {
                // Ʊ¤¸AnimalNo¤Ê¤é¥³¥ó¥Ü¼ºÇÔ¡£¼¡¤Î¥³¥ó¥Ü¤Ë¸þ¤±¤Æ½é´ü²½¤¹¤ë
                currentAnimalNo = animalPanelItem.animalNo;
                comboCount = 1;
            }

            // ¥¹¥³¥¢¤ò¹¹¿·(TODO¡¡¥¢¥Ë¥á½èÍý¤¹¤ë)
            totalScore += animalPanelItem.score * comboCount;

            // ¥¹¥³¥¢¤Î²èÌÌɽ¼¨¤ò¹¹¿·
            uIController.UpdateDisplayScore(totalScore);

            // Ä̲ᤷ¤¿¥Ñ¥Í¥ë¤Ï½ÅÊ£¤·¤ÆÄ̲á¤Ç¤­¤Ê¤¤¤è¤¦¤Ë½èÍý¤ò¤¹¤ë
            animalPanelItem.GetPanel();
        }

        // Player¤ÎÅö¤¿¤êȽÄê¤ò¥ª¥Õ
        boxCol.enabled = false;
    }
}

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIController : MonoBehaviour
{
    public Text txtScore;
    public Text txtWaveCount;
    public Text txtWaveTimer;
    public Text txtInfo;

    public Button btnReload;
    public PanelCreater panelCreater;

    public bool isClickable;
    private float waitTime = 0.5f;

    private Sequence sequence;

    void Start() {
        sequence = DOTween.Sequence();
        btnReload.onClick.AddListener(() => StartCoroutine(OnClickReloadArrowPanels(waitTime)));
    }

    public void DisplayaGameUp() {
        txtInfo.text = "Game Up!";
        sequence.Append(txtInfo.transform.DOLocalMoveX(0, 1.0f)).SetEase(Ease.Linear).SetRelative();
    }

    public IEnumerator DisplayWaveStart(int waveCount) {
        txtWaveCount.text = waveCount.ToString();
        txtInfo.text = "Wave : " + waveCount + " Start!";
        sequence.Append(txtInfo.transform.DOLocalMoveX(0, 1.0f)).SetEase(Ease.Linear).SetRelative();
        yield return new WaitForSeconds(2.0f);
        sequence.Append(txtInfo.transform.DOLocalMoveX(700, 1.0f)).SetEase(Ease.Linear).SetRelative();
        yield return new WaitForSeconds(1.0f);
        txtInfo.text = "";
        sequence.Append(txtInfo.transform.DOLocalMoveX(-700, 1.0f)).SetEase(Ease.Linear).SetRelative();
    }

    public void UpdateDisplayScore(int score) {
        // ¥¢¥Ë¥á¤µ¤»¤ë
        txtScore.text = score.ToString();
    }

    private IEnumerator OnClickReloadArrowPanels(float waitTime) {
        if (isClickable) {
            yield break;
        }
        isClickable = true;
        ActivateReloadButton(false);
        panelCreater.NextPanels();
        yield return new WaitForSeconds(waitTime);
        isClickable = false;
        ActivateReloadButton(true);
    }

    public void ActivateReloadButton(bool isSwitch) {
        btnReload.interactable = isSwitch;
    }
}

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu



´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹