using Cysharp.Threading.Tasks;
using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class ItemsOnFloor : MonoBehaviour
{
[SerializeField] RectTransform floorUI; //掉落道具显示的父类
[SerializeField] FloorItemCell floorItemCell; //非装备显示组件
[SerializeField] MoneyMoveByPath moneyMoveByPathCell; //掉落物金钱
[SerializeField] RectTransform defaultDropRect; //默认掉落位置
[SerializeField] Text[] expTexts;
FloorItemCell[] floorItemCells = new FloorItemCell[20]; //包含非装备的战利品掉落
MoneyMoveByPath[] moneyMoveByPathArr = new MoneyMoveByPath[20]; //掉落货币,金钱,经验等
void Awake()
{
for (int i = 0; i < floorItemCells.Length; i++)
{
//将预制体实例化到界面中
var inst = Instantiate(floorItemCell, floorUI);
inst.gameObject.name = "floorItemCell" + i;
inst.transform.localPosition = Vector3.zero;
floorItemCells[i] = inst;
}
for (int i = 0; i < moneyMoveByPathArr.Length; i++)
{
var mmbpath = Instantiate(moneyMoveByPathCell, floorUI);
mmbpath.gameObject.name = "moneyMoveByPath" + i;
mmbpath.transform.localPosition = Vector3.zero;
moneyMoveByPathArr[i] = mmbpath;
}
}
//主界面切换模式触发
private void OnEnable()
{
//bug记录:因为断线重连直接销毁界面导致OnEnable二次触发中间没有触发OnDisable,出现多次掉落的现象
// 已经调整断线重连逻辑,后续观察返回登录界面情况
// EquipModel.Instance.OnItemDropEvent -= NotifyPlayItemDrop;
// PackManager.Instance.DeleteItemEvent -= DeleteDropItem;
//主界面打开和显隐都要刷新
Display(true, EquipModel.Instance.lastDropIndexs);
EquipModel.Instance.OnItemDropEvent += NotifyPlayItemDrop;
PackManager.Instance.DeleteItemEvent += DeleteDropItem;
for (int i = 0; i < expTexts.Length; i++)
{
expTexts[i].text = "";
}
}
private void OnDisable()
{
EquipModel.Instance.OnItemDropEvent -= NotifyPlayItemDrop;
PackManager.Instance.DeleteItemEvent -= DeleteDropItem;
}
void NotifyPlayItemDrop(BattleDrops drops)
{
if (drops.dropItemPackIndex.Count > 0)
{
Display(true, drops.dropItemPackIndex, drops.rectTransform);
}
DisplayExp(drops);
}
int expIndex = 0;
void DisplayExp(BattleDrops drops)
{
// startPos = new Vector2(transform.localPosition.x + UnityEngine.Random.Range(-30, 30), transform.localPosition.y + UnityEngine.Random.Range(50, 100));
if (drops.expDrops.Count > 0)
{
var nowIndex = expIndex;
expTexts[nowIndex].text = "E+" + (drops.expDrops[0].Exp + drops.expDrops[0].ExpPoint * Constants.ExpPointValue);
expTexts[nowIndex].transform.position = drops.rectTransform.position;
expTexts[nowIndex].transform.DOLocalMove(expTexts[nowIndex].transform.localPosition + new Vector3(0, 20, 0), 1f).SetEase(Ease.InOutSine).onComplete = () =>
{
expTexts[nowIndex].text = "";
};
expIndex = (expIndex + 1) % 6;
}
}
void DeleteDropItem(PackType packType, string guid, int itemID, int index, int clearType)
{
if (packType != PackType.DropItem)
return;
floorItemCells[index].SetActive(false);
if (clearType == 1 || index >= moneyMoveByPathArr.Length)
{
return;
}
if (!EquipModel.Instance.IsEquip(itemID))
{
return;
}
moneyMoveByPathArr[index].transform.localPosition = floorItemCells[index].transform.localPosition;
moneyMoveByPathArr[index].SetActive(true);
moneyMoveByPathArr[index].PlayAnimation(42, 6);
}
///
/// 掉落过程的表现 界面格子组件顺序和背包格子顺序一致
///
///
///
public void Display(bool isAnimate = false, List indexList = null, RectTransform rect = null)
{
for (int i = 0; i < floorItemCells.Length; i++)
{
var item = floorItemCells[i];
var equipModel = PackManager.Instance.GetItemByIndex(PackType.DropItem, i);
if (equipModel == null)
{
//对空物品进行隐藏防范
item.SetActive(false);
continue;
}
if (!indexList.IsNullOrEmpty() && !indexList.Contains(i))
{
//不干涉其他掉落物品
continue;
}
if (item.isActiveAndEnabled)
{
//防范一直播放掉落动画,某种原因被打断了后续的界面显示,这里直接触发打开界面
// EquipModel.Instance.CalcFloorEquip(i);
continue;
}
item.Display(i, isAnimate, rect == null ? defaultDropRect.position : rect.position).Forget();
}
}
}