using System; using Lean.Pool; using Michsky.UI.Heat; using UFS3; using UnityEngine; public class UI_FishingSlotsController : MonoBehaviour { public PlayerProfile PlayerProfile; public ScriptableEventItemType OnItemTypeAssign; public ScriptableEventItemData OnAssignedItem; public ScriptableEventItemData OnRemovedItemFromSlot; public ScriptableEventItemData OnItemAssignDirect; [SerializeField] private PanelManager _CategoriesPanelManager; [SerializeField] private UI_SlotItemAssignButton _SlotTemplate; private UI_SlotItemAssignButton _RodSlot; private UI_SlotItemAssignButton _ReelSlot; private UI_SlotItemAssignButton _LineSlot; private UI_SlotItemAssignButton _BobberSlot; private UI_SlotItemAssignButton _HookSlot; private UI_SlotItemAssignButton _BaitSlot; private UI_SlotItemAssignButton _WeightSlot; private UI_SlotItemAssignButton _LeaderSlot; private UI_SlotItemAssignButton _LureSlot; public RectTransform layoutGroup; public static int CurrentInventorySlotNumber; public static event Action OnInventorySlotNumberChange; private void Awake() { CurrentInventorySlotNumber = 0; } public void SetSlot(int slotNumber) { CurrentInventorySlotNumber = slotNumber; KillAllSlots(); InitializeSlots(); UI_FishingSlotsController.OnInventorySlotNumberChange?.Invoke(); } private void OnEnable() { OnItemTypeAssign.OnRaised += TypeAssignItemType; OnAssignedItem.OnRaised += OnAssignedItemOn; OnRemovedItemFromSlot.OnRaised += OnRemovedItemFromSlotOnOnRaised; OnItemAssignDirect.OnRaised += OnItemAssignDirect_OnRaised; InitializeSlots(); } private void OnDisable() { OnItemTypeAssign.OnRaised -= TypeAssignItemType; OnAssignedItem.OnRaised -= OnAssignedItemOn; OnRemovedItemFromSlot.OnRaised -= OnRemovedItemFromSlotOnOnRaised; OnItemAssignDirect.OnRaised -= OnItemAssignDirect_OnRaised; KillAllSlots(); } private void KillAllSlots() { UI_SlotItemAssignButton[] componentsInChildren = GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { LeanPool.Despawn(componentsInChildren[i]); } _RodSlot = null; _ReelSlot = null; _LineSlot = null; _BobberSlot = null; _HookSlot = null; _BaitSlot = null; _WeightSlot = null; _LeaderSlot = null; _LureSlot = null; } private bool CheckIfItemIsInFishingSet(BaseItemData item) { FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; return ((IItemType)item).ItemType switch { ItemType.Rod => fishingSetData.Rod == item, ItemType.Reel => fishingSetData.Reel == item, ItemType.Line => fishingSetData.Line == item, ItemType.Bobber => fishingSetData.Bobber == item, ItemType.Lure => fishingSetData.Lure == item, ItemType.Hook => fishingSetData.Hook == item, ItemType.Bait => fishingSetData.Bait == item, _ => false, }; } private void PutBackToInventory(BaseItemData item) { if (!(item == null)) { PlayerProfile.AddItem(item, overrideAutoSave: true); ItemType itemType = ((IItemType)item).ItemType; FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; switch (itemType) { case ItemType.Rod: fishingSetData.Rod = null; break; case ItemType.Reel: fishingSetData.Reel = null; break; case ItemType.Line: fishingSetData.Line = null; break; case ItemType.Bobber: fishingSetData.Bobber = null; break; case ItemType.Hook: fishingSetData.Hook = null; break; case ItemType.Lure: fishingSetData.Lure = null; break; case ItemType.Bait: fishingSetData.Bait = null; break; default: throw new ArgumentOutOfRangeException(); } } } private void OnRemovedItemFromSlotOnOnRaised(BaseItemData obj) { if (CheckIfItemIsInFishingSet(obj)) { ItemType itemType = ((IItemType)obj).ItemType; FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; KillAllSlots(); switch (itemType) { case ItemType.Rod: PutBackToInventory(fishingSetData.Reel); PutBackToInventory(fishingSetData.Line); PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Rod); break; case ItemType.Reel: PutBackToInventory(fishingSetData.Line); PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Reel); break; case ItemType.Line: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Line); break; case ItemType.Bobber: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Lure); break; case ItemType.Hook: PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Hook); break; case ItemType.Lure: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Lure); break; case ItemType.Bait: PutBackToInventory(fishingSetData.Bait); break; default: throw new ArgumentOutOfRangeException(); } PlayerProfile.SaveData(); InitializeSlots(); } } private void OnItemAssignDirect_OnRaised(BaseItemData obj) { if (!CheckIfItemIsInFishingSet(obj)) { ItemType itemType = ((IItemType)obj).ItemType; FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; KillAllSlots(); switch (itemType) { case ItemType.Rod: PutBackToInventory(fishingSetData.Reel); PutBackToInventory(fishingSetData.Line); PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Rod); fishingSetData.Rod = (RodData)obj; break; case ItemType.Reel: PutBackToInventory(fishingSetData.Line); PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Reel); fishingSetData.Reel = (ReelData)obj; break; case ItemType.Line: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Lure); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Line); fishingSetData.Line = (LineData)obj; break; case ItemType.Bobber: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Lure); fishingSetData.Bobber = (BobberData)obj; fishingSetData.SetType = FishingMethodType.Float; break; case ItemType.Hook: PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Hook); fishingSetData.Hook = (HookData)obj; break; case ItemType.Lure: PutBackToInventory(fishingSetData.Bobber); PutBackToInventory(fishingSetData.Hook); PutBackToInventory(fishingSetData.Bait); PutBackToInventory(fishingSetData.Lure); fishingSetData.Lure = (LureData)obj; fishingSetData.SetType = FishingMethodType.Spinning; break; case ItemType.Bait: fishingSetData.Bait = (BaitData)obj; break; default: throw new ArgumentOutOfRangeException(); } PlayerProfile.SaveData(); InitializeSlots(); } } private void InitializeSlots() { FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; if ((object)_RodSlot == null) { _RodSlot = CreateSlotItemAssignButton(ItemType.Rod); } if ((bool)fishingSetData.Rod) { OnAssignedItemOnOnRaised(fishingSetData.Rod); } if ((bool)fishingSetData.Reel) { OnAssignedItemOnOnRaised(fishingSetData.Reel); } if ((bool)fishingSetData.Line) { OnAssignedItemOnOnRaised(fishingSetData.Line); } if ((bool)fishingSetData.Bobber) { OnAssignedItemOnOnRaised(fishingSetData.Bobber); } if ((bool)fishingSetData.Lure) { OnAssignedItemOnOnRaised(fishingSetData.Lure); } if ((bool)fishingSetData.Hook) { OnAssignedItemOnOnRaised(fishingSetData.Hook); } if ((bool)fishingSetData.Bait) { OnAssignedItemOnOnRaised(fishingSetData.Bait); } if ((bool)fishingSetData.Weight) { OnAssignedItemOnOnRaised(fishingSetData.Weight); } if ((bool)fishingSetData.Rod) { _RodSlot.SetData(fishingSetData.Rod); } if ((bool)fishingSetData.Reel) { _ReelSlot.SetData(fishingSetData.Reel); } if ((bool)fishingSetData.Line) { _LineSlot.SetData(fishingSetData.Line); } if ((bool)fishingSetData.Bobber) { _BobberSlot.SetData(fishingSetData.Bobber); } if ((bool)fishingSetData.Lure) { _LureSlot.SetData(fishingSetData.Lure); } if ((bool)fishingSetData.Hook) { _HookSlot.SetData(fishingSetData.Hook); } if ((bool)fishingSetData.Bait) { _BaitSlot.SetData(fishingSetData.Bait); } if ((bool)fishingSetData.Weight) { _WeightSlot.SetData(fishingSetData.Weight); } } private void OnAssignedItemOn(BaseItemData item) { KillAllSlots(); PlayerProfile.SaveData(); InitializeSlots(); } private void CheckAssignedItemOnOnRaised(BaseItemData item) { _ = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; } private void OnAssignedItemOnOnRaised(BaseItemData obj) { ItemType itemType = ((IItemType)obj).ItemType; FishingSetData fishingSetData = PlayerProfile.FishingSets[CurrentInventorySlotNumber]; switch (itemType) { case ItemType.Rod: _ReelSlot = CreateSlotItemAssignButton(ItemType.Reel); break; case ItemType.Reel: _LineSlot = CreateSlotItemAssignButton(ItemType.Line); break; case ItemType.Line: if (fishingSetData.Lure == null) { _BobberSlot = CreateSlotItemAssignButton(ItemType.Bobber); } if (fishingSetData.Bobber == null) { _LureSlot = CreateSlotItemAssignButton(ItemType.Lure); } break; case ItemType.Bobber: _HookSlot = CreateSlotItemAssignButton(ItemType.Hook); break; case ItemType.Hook: _BaitSlot = CreateSlotItemAssignButton(ItemType.Bait); break; default: throw new ArgumentOutOfRangeException(); case ItemType.Lure: case ItemType.Bait: break; } } private UI_SlotItemAssignButton CreateSlotItemAssignButton(ItemType type) { UI_SlotItemAssignButton uI_SlotItemAssignButton = LeanPool.Spawn(_SlotTemplate, layoutGroup); uI_SlotItemAssignButton.transform.SetAsLastSibling(); uI_SlotItemAssignButton.Type = type; uI_SlotItemAssignButton.SetData(null); uI_SlotItemAssignButton.gameObject.SetActive(value: true); return uI_SlotItemAssignButton; } public void TypeAssignItemType(ItemType slot) { KillAllSlots(); _CategoriesPanelManager.OpenPanelByIndex((int)slot); InitializeSlots(); } }