Files
MRFishingMaster/Assets/_FishingMaster/Scripts/Item/Shop.cs

132 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using DragonLi.Core;
using IngameDebugConsole;
using TMPro;
using UnityEngine;
public class Shop : MonoBehaviour
{
public GameObject shopUI;
public GameObject tipUI;
public ShopItem[] shopItems;
public TMP_Text playerCoinText;
public GameObject[] tipObjs;
public EventSystemHandler eventSystem;
private int currentShowCoin;
private Tween coinTween;
private void Awake()
{
eventSystem.embeddedEventSystem=GameInit.Ins.eventSystem.gameObject;
}
private void Start()
{
shopUI.transform.localScale = Vector3.zero;
foreach (var item in tipObjs)
{
item.transform.localScale = Vector3.zero;
}
for (int i = 0; i < shopItems.Length; i++)
{
shopItems[i].Init(GameManager.Ins.ShopDataDic[i + 1]);
}
foreach (GameObject item in tipObjs)
{
item.transform.localScale = Vector3.zero;
}
currentShowCoin = GameInit.Ins.self.GetCoinCount();
UpdateCoinText(currentShowCoin);
EventDispatcher.AddEventListener<int>("RefreshPlayerCoin", RefreshPlayerCoin);
EventDispatcher.AddEventListener<int>("ShowTip", ShowTip);
}
public void ShowShopUI()
{
shopUI.transform.DOScale(0.007f, 0.5f).SetEase(Ease.OutBounce);
GameInit.Ins.self.UserHand();
}
public void HideShopUI()
{
shopUI.transform.DOScale(0f, 0.5f).SetEase(Ease.OutBounce);
GameInit.Ins.self.UserGun();
}
public void ShowTipUI(int tipId)
{
tipObjs[tipId].transform.DOScale(1f, 0.5f).SetEase(Ease.OutBounce);
}
public void HideTipUI(int tipId)
{
tipObjs[tipId].transform.DOScale(0f, 0.5f).SetEase(Ease.OutBounce);
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
ShowShopUI();
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
HideShopUI();
}
}
public void RefreshPlayerCoin(int targetCoin)
{
// 如果有旧的 tween先停掉
if (coinTween != null && coinTween.IsActive())
coinTween.Kill();
int startCoin = currentShowCoin;
coinTween = DOTween.To(
() => startCoin,
value =>
{
startCoin = value;
currentShowCoin = value;
UpdateCoinText(value);
},
targetCoin,
0.5f // ⏱ 动画时长,可调
)
.SetEase(Ease.OutCubic);
}
public void ShowTip(int tipId)
{
ShowTipUI(tipId);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
HideTipUI(tipId);
}, 2f);
}
void UpdateCoinText(int coin)
{
playerCoinText.text = coin.ToString();
}
public void CheckGunBtn(int id)
{
foreach (var item in shopItems)
{
item.CheckGunBuyBtn(id);
}
}
}