using DG.Tweening; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; public class GoodsSet { public byte setCount; public ItemType type; } public class GoodsHandler : MonoBehaviour { [SerializeField] private GoodsInputPlatform currentGoodsPlacer; [SerializeField] private GoodsInputPlatform nextGoodsPlacer; [SerializeField] private byte minGoods = 2; [SerializeField] private byte minGoodsInSet = 2; [SerializeField] private byte maxGoodsInSet = 3; [SerializeField] private byte totalGoodsAvail = 10; private PopupManager popupManager; private ItemType[] goodsType; private List lastUpdatedGoodsSet = new List(); public GoodsInputPlatform CurrentGoodsPlacer => currentGoodsPlacer; public GoodsInputPlatform NextGoodsPlacer => nextGoodsPlacer; public bool CanClearGoodsUsingPowerup { get; private set; } public void InitGoodsInfo(int goodTypeCount) { InitGoodsTypes(goodTypeCount); InitCurrentAndNextGoods(); } private void InitGoodsTypes(int goodTypeCount) { // Debug.Log($"Goods type count: {goodTypeCount}"); // // int maxElements = (int)ItemType.MAX; goodsType = new ItemType[goodTypeCount]; for (int index = 0; index < goodTypeCount; index++) goodsType[index] = (ItemType)index; } public void OnRefreshInputNodes() { currentGoodsPlacer.SendObjectsBackToPool(); nextGoodsPlacer.SendObjectsBackToPool(); InitCurrentAndNextGoods(); } public void InitCurrentAndNextGoods() { InitGoods(); currentGoodsPlacer.InitGoodsView(new List(lastUpdatedGoodsSet)); currentGoodsPlacer.PlaceGoods(); InitGoods(); nextGoodsPlacer.InitGoodsView(new List(lastUpdatedGoodsSet)); nextGoodsPlacer.PlaceGoods(); } public void SetClearPowerupState(bool state) { CanClearGoodsUsingPowerup = state; popupManager = popupManager == null ? InterfaceManager.Instance?.GetInterfaceInstance() : popupManager; if (CanClearGoodsUsingPowerup) { // initialize the ftue text over here popupManager.GetPopup(PopupType.FeedbackPopup).SetFeedbackText("SELECT ANY NODE TO CLEAR IT"); popupManager.ShowPopup(PopupType.FeedbackPopup); } } private void InitGoods() { lastUpdatedGoodsSet.Clear(); CreateGoodsSet(); //CreateGoodsSetTest(); } private void CreateGoodsSetTest() { testGoodTypeIndex = 0; for (int i=0; i<3; i++) { var goodsSetObj = new GoodsSet(); UseTestData(ref goodsSetObj); lastUpdatedGoodsSet.Add(goodsSetObj); } } private void CreateGoodsSet() { int remCountInSet = (byte)UnityEngine.Random.Range(minGoods, totalGoodsAvail); while (remCountInSet >= minGoods) { if (lastUpdatedGoodsSet.Count == maxGoodsInSet) break; var goodsSetObj = new GoodsSet(); goodsSetObj.type = GenerateRandomGoodsType(); goodsSetObj.setCount = GenerateRandomSetCount(remCountInSet); lastUpdatedGoodsSet.Add(goodsSetObj); remCountInSet -= goodsSetObj.setCount; } } private int testGoodTypeIndex = 0; public void UseTestData(ref GoodsSet goodsSetObj) { goodsSetObj.type = goodsType[testGoodTypeIndex]; goodsSetObj.setCount = 3; // check 2 and 3 testGoodTypeIndex++; // testGoodTypeIndex = testGoodTypeIndex == 0 ? 1 : 0; } public void UpdateGoodsInputPlatform() { currentGoodsPlacer.InitGoodsView(new List(nextGoodsPlacer.GetGoodsDataSet())); currentGoodsPlacer.SetBaseObjects(new List(nextGoodsPlacer.GetBaseObjects())); TweenNextObjectsToCurrentInputPlatform(); } public void SwapInputPlatformsData() { var storedNextGoodsSet = new List(nextGoodsPlacer.GetGoodsDataSet()); // var storedNextBaseObjects = new List(nextGoodsPlacer.GetBaseObjects()); // int count = currentGoodsPlacer.GetBaseObjectsCount(); Tween inputPlatformTween1 = null, inputPlatformTween2 = null; ItemBase itemBase = null; for (int index = 0; index < count; index++) { itemBase = currentGoodsPlacer.GetItemBasedOnIndex(index); inputPlatformTween1 = itemBase.transform.DOMove( nextGoodsPlacer.GetSpawnPointTransform(index).position, 1f ); } nextGoodsPlacer.InitGoodsView(new List(currentGoodsPlacer.GetGoodsDataSet())); nextGoodsPlacer.SetBaseObjects(new List(currentGoodsPlacer.GetBaseObjects())); count = storedNextBaseObjects.Count; for (int index = 0; index < count; index++) { itemBase = storedNextBaseObjects[index]; inputPlatformTween2 = itemBase.transform.DOMove( currentGoodsPlacer.GetSpawnPointTransform(index).position, 1f ); } currentGoodsPlacer.InitGoodsView(new List(storedNextGoodsSet)); currentGoodsPlacer.SetBaseObjects(new List(storedNextBaseObjects)); } private void UpdateNextInputGoods() { InitGoods(); nextGoodsPlacer.InitGoodsView(new List(lastUpdatedGoodsSet)); nextGoodsPlacer.PlaceGoods(); } public void TweenNextObjectsToCurrentInputPlatform() { int count = currentGoodsPlacer.GetBaseObjectsCount(); Tween inputPlatformTween = null; ItemBase itemBase = null; for (int index = 0; index < count; index++) { itemBase = currentGoodsPlacer.GetItemBasedOnIndex(index); inputPlatformTween = itemBase.transform.DOMove( currentGoodsPlacer.GetSpawnPointTransform(index).position, 1f ); } inputPlatformTween?.OnComplete(() => UpdateNextInputGoods()); } private ItemType GenerateRandomGoodsType() { return goodsType[UnityEngine.Random.Range(0, goodsType.Length)]; } private byte GenerateRandomSetCount(int availCountInSet) { return (byte)UnityEngine.Random.Range(minGoods, availCountInSet); } public void ClearGoodsInputPlatforms() { goodsType = null; lastUpdatedGoodsSet.Clear(); currentGoodsPlacer.ClearGoodsSetAndBaseObjects(); nextGoodsPlacer.ClearGoodsSetAndBaseObjects(); } }