게임클라이언트 프로그래밍/Guns N Rachel's
UIDice 수정(인벤토리 용량, 소지골드 체크 상호작용)
Game Client Lee Hwanguk
2023. 5. 3. 03:45
* 기존에 만든 Dice 시스템을 수정했다. 인벤토리의 용량과 소지골드를 체크하고 bool 타입을 조건으로
로직을 제어했다.
플레이어의 상태를 체크하는 시점을 먼저 생각했다. 'GO!' 버튼을 누르면 플레이어는 겜블을 시도하는데
이때가 체크하는 시점으로 보였다. 기존의 로직과 조금 달라진 점은 골드부족이나 용량 부족으로 인해 겜블이 불가능하다면 버튼을 비활성화 (0.5f초) 한 후 다시 활성화 하는 로직으로 변경 했다.
*수정된 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.U2D;
using UnityEngine.UI;
public class UIDice : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public Button closeBtn;
public GameObject btnGo;
private Button rollBtn;
public DiceScript dice;
public Button dim;
public System.Action onClosing;
public UIdiceResultPopup resultPopup;
private bool isEnugh;
private bool isFull;
private List<string> curList;
private int inventCellCount;
public void Init()
{
this.dice.Init();
this.rollBtn = this.btnGo.GetComponent<Button>();
this.closeBtn.onClick.AddListener(() => {
this.onClosing();
});
this.dim.onClick.AddListener(() => {
this.onClosing();
});
this.rollBtn.onClick.AddListener(() => { //돌고 있는 중이면 또 눌리면 안됨
////데이터 테이블 참조해서 금액 넣음 됩니다.
this. isEnugh = InfoManager.instance.DecreaseDungeonGold(1000000);
this. curList = default(List<string>);
EventDispatcher.Instance.Dispatch<List<string>>(EventDispatcher.EventName.UICurrentInventoryList,
out curList);
this. inventCellCount = InfoManager.instance.inventoryInfo.InventoryCount;
Debug.LogFormat("현재 인벤토리 갯수 : {0}, 현재 들고 있는 아이템 리스트 갯수 : {1}", inventCellCount, curList.Count);
this.isFull = curList.Count < inventCellCount;
Debug.Log(InfoManager.instance.possessionAmountInfo.dungeonGoldAmount);
if (isEnugh && isFull)
{
this.dice.RollDice();
StartCoroutine(this.RollDiceBtnSetting());
this.dice.DiceResultImgInit();
}
else if (!isEnugh || !isFull)
{
StartCoroutine(this.CantDiceBtnSetting());
}
});
this.resultPopup.Init();
}
//ResultPopup
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("PointerDown");
this.resultPopup.gameObject.SetActive(true);
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("PointUp");
this.resultPopup.gameObject.SetActive(false);
}
//DiceResult
private IEnumerator RollDiceBtnSetting()
{
while (true)
{
yield return new WaitForSeconds(0.2f);
this.closeBtn.interactable = false;
this.dim.interactable = false;
this.BtnGoColorDown();
this.rollBtn.interactable = false;
if (this.dice.rb.velocity.magnitude == 0)
{
this.dice.StopDice();
this.rollBtn.interactable = true;
this.BtnGoColorUp();
this.dice.DiceResultImg();
this.closeBtn.interactable = true;
this.dim.interactable = true;
//Percentage
this.dice.GradePercentage();
this.dice.TypePercentage();
yield break;
}
}
}
private IEnumerator CantDiceBtnSetting()
{
var btnTxt = this.btnGo.transform.GetChild(1).GetComponent<Text>();
this.BtnGoColorDown();
this.rollBtn.interactable = false;
Debug.LogFormat("{0}/{1}", isEnugh, isFull);
if (!isEnugh)
{
btnTxt.text = "Gold부족";
}
else if (!isFull)
{
btnTxt.text = "용량부족";
}
yield return new WaitForSeconds(1f);
this.BtnGoColorUp();
this.rollBtn.interactable = true;
btnTxt.text = "Go!";
this.isEnugh = true;
this.isFull= true;
}
//ButtonSetting
private void BtnGoColorDown() //알파값 down
{
var imgBoxGo=this.btnGo.transform.Find("imgBox").GetComponent<Image>();
var txtSelectGo = this.btnGo.transform.Find("txtSelect").GetComponent<Text>();
Color imgBoxGoColor = imgBoxGo.color;
imgBoxGoColor.a = 0.5f;
imgBoxGo.color= imgBoxGoColor;
Color txtSelectGoColor= txtSelectGo.color;
txtSelectGoColor.a = 0.5f;
txtSelectGo.color= txtSelectGoColor;
}
private void BtnGoColorUp() //알파값 up
{
var imgBoxGo = this.btnGo.transform.Find("imgBox").GetComponent<Image>();
var txtSelectGo = this.btnGo.transform.Find("txtSelect").GetComponent<Text>();
Color imgBoxGoColor = imgBoxGo.color;
imgBoxGoColor.a = 1f;
imgBoxGo.color = imgBoxGoColor;
Color txtSelectGoColor = txtSelectGo.color;
txtSelectGoColor.a = 1f;
txtSelectGo.color = txtSelectGoColor;
}
}
# 결과 아이템이 인벤토리에 2개씩 들어오는 치명적인 버그가있다. 내일 반드시 수정해야한다.
아마 Instantaite 가 두번되는 것 같다. 많이 어려운 문제는 아닐 것으로 예상된다.