MayHeCome/Assets/仪式/C#/BagControl.cs
2024-12-18 17:55:34 +08:00

201 lines
6.2 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 UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
public class BagControl : MonoSingleton<BagControl>
{
public RemainsBag bag;
[Header("空精灵")]
public Sprite Blocksprite;
public Dictionary<string, RemainsMode> remainsModes = new Dictionary<string, RemainsMode>();//借出去的
public Action InteractiveEvent;
public GameObject DragUI;
[HideInInspector]
public GraphicRaycaster graphicRaycaster;
private RectTransform rectTransform;
private Vector2 AwakePos;//背包初始位置
public GameObject Openbtn;
public override void Awake()
{
base.Awake();
graphicRaycaster = transform.parent.GetComponent<GraphicRaycaster>();
rectTransform = GetComponent<RectTransform>();
AwakePos = rectTransform.anchoredPosition;
gameObject.SetActive(false);
}
private void OnEnable()
{
Refresh();
}
private void OnDestroy()
{
}
public void OpenBag(GameObject obj)
{
RectTransform objRectTransform = obj.GetComponent<RectTransform>();
bool isOpening = !gameObject.activeSelf;
gameObject.SetActive(true); // 确保 UI 处于激活状态以显示动画
if (isOpening)
{
// 打开时UI 向下移动obj 向右移动 200 个单位
rectTransform.DOAnchorPos(new Vector2(AwakePos.x, AwakePos.y - 600), 0.5f)
.SetEase(Ease.OutQuad)
.OnComplete(() => gameObject.SetActive(true));
objRectTransform.DOAnchorPos(new Vector2(objRectTransform.anchoredPosition.x, objRectTransform.anchoredPosition.y - 500), 0.5f)
.SetEase(Ease.OutQuad);
}
else
{
// 关闭时UI 和 obj 返回初始位置
rectTransform.DOAnchorPos(AwakePos, 0.5f)
.SetEase(Ease.OutQuad)
.OnComplete(() => gameObject.SetActive(false));
objRectTransform.DOAnchorPos(new Vector2(objRectTransform.anchoredPosition.x, objRectTransform.anchoredPosition.y + 500), 0.5f)
.SetEase(Ease.OutQuad);
}
}
public void CloseBagTwo()
{
YiShiUI.Instance.gameObject.SetActive(false);
YiShiUI.Instance.BagActive(false);
}
public void Refresh()
{
Transform pa = transform.Find("背包/内容/遗物组");
int childIndex = 0;
for (int i = 0; i < pa.childCount; i++, childIndex++)
{
if (bag.Items.Count > childIndex)
{
int RealCount = bag.Items[childIndex].Count;
if (RealCount > 0)//背包有东西
{
if (remainsModes.TryGetValue(bag.Items[childIndex].Rename, out RemainsMode remainsMode))//---借出去的有该类
{
Debug.LogWarning("借出去的有该类");
RealCount -= remainsMode.Count;
if (RealCount == 0)
{
i--;
continue;
}
}
Transform child = pa.GetChild(i);
child.GetComponent<UIItem>().AwkaeItem(bag.Items[childIndex].Rename, RealCount);
}
else i--;
}
else
{
Transform child = pa.GetChild(i);
child.GetComponent<UIItem>().Refresh();
}
}
MiaosuRemains("");
}
/// <summary>
/// 移出背包
/// </summary>
public void RemoveOut(string name, int count = 1)
{
RemainsMode remainsMode;
Debug.LogWarning("移出背包");
if (remainsModes.TryGetValue(name, out remainsMode))
{
remainsMode.Count += count;
remainsModes[name] = remainsMode;
Debug.LogWarning("移出背包" + name);
}
else
{
Debug.LogWarning("移出背包sss" + name);
remainsMode = bag.GetRemainsMode(name);
remainsMode.Count = 1;
remainsModes.Add(name, remainsMode);
}
InteractiveEvent?.Invoke();
}
/// <summary>
/// 移入背包
/// </summary>
public void RemoveIn(string Rename, int count = 1)
{
if (remainsModes.TryGetValue(Rename, out RemainsMode remainsbagmode))
{
remainsbagmode.Count -= count;
if (remainsbagmode.Count <= 0)
{
remainsModes.Remove(remainsbagmode.Rename);
}
else
{
remainsModes[Rename] = remainsbagmode;
}
}
}
public RectTransform ChangeDragUI(string Rename)
{
DragUI.gameObject.SetActive(true);
DragUI.GetComponent<Image>().sprite = bag.GetSprite(Rename);
return DragUI.GetComponent<RectTransform>();
}
/// <summary>
/// 消耗遗物
/// </summary>
public void CutRemains()
{
foreach (KeyValuePair<string, RemainsMode> item in remainsModes)
{
bag.CutItems(item.Key, item.Value.Count);
}
remainsModes.Clear();
Refresh();
}
/// <summary>
/// 增加遗物
/// </summary>
public void AddRemains(RemainsItem remains)
{
bag.AddItems(remains);
Refresh();
}
/// <summary>
/// 描述遗物
/// </summary>
public void MiaosuRemains(string remainsName)
{
if (remainsName == "")
{
transform.Find("遗物描述").GetComponent<TextMeshProUGUI>().text = "";
transform.Find("功能描述").GetComponent<TextMeshProUGUI>().text = "";
transform.Find("内容描述").GetComponent<TextMeshProUGUI>().text = "";
}
else
{
transform.Find("遗物描述").GetComponent<TextMeshProUGUI>().text = bag.GetRemainsItem(remainsName).Miaosu;
transform.Find("功能描述").GetComponent<TextMeshProUGUI>().text = bag.GetRemainsItem(remainsName).BuffMiaosu;
transform.Find("内容描述").GetComponent<TextMeshProUGUI>().text = bag.GetRemainsItem(remainsName).ContectMiaosu;
}
}
}