해당 Stage 진행도를 나타내주는 UI

 

Stage 진행도를 나타내주는 기능은 아무래도 동적으로 값이 계속 변하다보니 MVC 패턴을 이용하여 UIManager에 등록하고 사용하는 방식으로 진행 하였다., 

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

//Model : GameManager의 처치한 Enemy Count가 Model이됨
//View : UIStageProgressBarView
//Controller : UIStageProgressBarController

public class UIStageProgressBar : MonoBehaviour
{
    public string UIKey;

    private UIStageProgressBarModel model;
    [SerializeField]private UIStageProgressBarView view;
    private UIStageProgressBarController controller;

    private void Start()
    {
        model = new UIStageProgressBarModel();
        GameManager.Instance.StageProgressModel = model;
        controller = new UIStageProgressBarController();
        controller.Initialize(view, model);

        UIManager.Instance.RegisterController(UIKey, controller);
    }

}

해당 기능의 Model,View,Controller를 멤버변수로 가지고있어 초기화및 관리를 하고있다. 오브젝트에 컴포넌트로 적용하여 사용하는 클래스이다. 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using DG.Tweening;

//Model : GameManager의 처치한 Enemy Count가 Model이됨
//View : UIStageProgressBarView
//Controller : UIStageProgressBarController

public class UIStageProgressBarView : MonoBehaviour, IUIBase
{
    [SerializeField] private RectTransform curStageProgress;

    public void Initialize()
    {
        if (curStageProgress == null)
        {
            curStageProgress = GetComponent<RectTransform>();
        }

        curStageProgress.localScale = new Vector3(0, 1, 1);

    }

    public void ShowUI()
    {
        //Boss 몬스터 등장 조건이 되지 않으면 UI 출력
        gameObject.SetActive(true);
    }

    public void HideUI()
    {
        //Boss 몬스터 등장 조건이 되면 사라지기
        Utils.StartFadeOut(this.GetComponent<CanvasGroup>(), Ease.OutBounce, 1.0f);
        gameObject.SetActive(false);
    }

    public void UpdateUI()
    {

    }

    public void UpdateUIProgree(float resultProgress)
    {
        curStageProgress.localScale = new Vector3(resultProgress, 1, 1);
    }
}

Controller로부터 데이터를 받아 Scene에 출력(업데이트)하는 클래스 영역이다. 

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

//Model : GameManager의 처치한 Enemy Count가 Model이됨
//View : UIStageProgressBarView
//Controller : UIStageProgressBarController

[System.Serializable]
public class UIStageProgressBarModel : UIModel
{
    public event Action OnEventCurEnemyAddCount;
    private int curEnemySlayerCount; //처지한 Enemy 카운트
    private int bossTriggerEnemySlayerCount; //Boss가 등장에 필요한 쓰러트린 Enemy 카운트

    public int CurEnemySlayerCount { get => curEnemySlayerCount; private set => curEnemySlayerCount = value; }
    public int BossTriggerEnemySlayerCount { get => bossTriggerEnemySlayerCount; private set => bossTriggerEnemySlayerCount = value; }

    public void Initialize(int bossTriggerCount)
    {
        bossTriggerEnemySlayerCount = bossTriggerCount;
        curEnemySlayerCount = 0;
    }

    public void CurCountDataClear()
    {
        curEnemySlayerCount = 0;
    }

    public void AddCurEnemyCount(int slayEnemyCount)
    {
        curEnemySlayerCount += slayEnemyCount;
        Debug.Log($"현재 처치한 적의 갯수 : {curEnemySlayerCount} \" {bossTriggerEnemySlayerCount}");
        OnEventCurEnemyAddCount?.Invoke();
    }


}

Data(Enemy처치 갯수)를 관리하는 곳으로 적을 처치할때마다 EnemyCount가 올라간다. 

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

//Model : GameManager의 처치한 Enemy Count가 Model이됨
//View : UIStageProgressBarView
//Controller : UIStageProgressBarController

public class UIStageProgressBarController : UIController
{
    private UIStageProgressBarModel stageProgressBarModel;
    private UIStageProgressBarView stageProgressBarView;

    public override void Initialize(IUIBase view, UIModel model)
    {
        base.Initialize(view, model);

        stageProgressBarModel = model as UIStageProgressBarModel;
        stageProgressBarView = view as UIStageProgressBarView;

        stageProgressBarModel.OnEventCurEnemyAddCount += UpdateView;
        stageProgressBarModel.OnEventCurEnemyAddCount += BossTriggerCheck;
    }

    public override void OnShow()
    {
        view.ShowUI();
        UpdateView();   // 초기 View 갱신
    }

    public override void OnHide()
    {
        view.HideUI();
    }

    public override void UpdateView()
    {
        float resultProgress = (stageProgressBarModel.CurEnemySlayerCount / (float)stageProgressBarModel.BossTriggerEnemySlayerCount);
        resultProgress = Mathf.Min(1,resultProgress);

        // Model 데이터를 기반으로 View 갱신
        //view.UpdateUI();
        stageProgressBarView.UpdateUIProgree(resultProgress);
    }

    public void BossTriggerCheck()
    {
        if (stageProgressBarModel.CurEnemySlayerCount >= stageProgressBarModel.BossTriggerEnemySlayerCount)
        {
            Debug.Log($"보스 등장 조건을 만족 합니다.");
            GameManager.Instance.isTryBoss = true;
            OnHide();
        }
    }
}

Controller로 Data값이 등록되면 해당 Controller에 알려주고 Controller는 해당 데이터가 들어옴에 따라 로직대로 동작하게된다. 해당 로직은 현재 잡은 Enemy수 / 목표 Enemy 처치수를 하여 백분율로 나타내고 해당 데이터를 이용하여 Boss 등장 조건을 체크하는 Class 이다. 

+ Recent posts