diff --git a/Assets/Scripts/Events/EiramEvents.cs b/Assets/Scripts/Events/EiramEvents.cs index c40ddd0..c3b7541 100644 --- a/Assets/Scripts/Events/EiramEvents.cs +++ b/Assets/Scripts/Events/EiramEvents.cs @@ -29,18 +29,11 @@ public static void OnPlayerToggleInventory(PlayerInventory playerInventory) PlayerToggleInventoryEvent?.Invoke(playerInventory); } - public static event Action PlayerInventoryIsDirtyEvent; + public static event Action SelectedSlotChangedEvent; - public static void OnPlayerInventoryIsDirty(PlayerInventory playerInventory) + public static void SelectedSlotChanged(int slotIndex) { - PlayerInventoryIsDirtyEvent?.Invoke(playerInventory); - } - - public static event Action SelectedSlotChangedEvent; - - public static void SelectedSlotChanged(PlayerInventory playerInventory, int slotIndex) - { - SelectedSlotChangedEvent?.Invoke(playerInventory, slotIndex); + SelectedSlotChangedEvent?.Invoke(slotIndex); } } diff --git a/Assets/Scripts/Inventories/PlayerInventory.cs b/Assets/Scripts/Inventories/PlayerInventory.cs index 3ea3d42..f2c280e 100644 --- a/Assets/Scripts/Inventories/PlayerInventory.cs +++ b/Assets/Scripts/Inventories/PlayerInventory.cs @@ -24,14 +24,14 @@ public void SelectNext() { selectedSlot++; if (selectedSlot >= hotbarSlotsCount) selectedSlot = 0; - EiramEvents.SelectedSlotChanged(this, selectedSlot); + EiramEvents.SelectedSlotChanged(selectedSlot); } public void SelectPrevious() { selectedSlot--; if (selectedSlot < 0) selectedSlot = hotbarSlotsCount - 1; - EiramEvents.SelectedSlotChanged(this, selectedSlot); + EiramEvents.SelectedSlotChanged(selectedSlot); } public ItemStack PopSelectedItem() diff --git a/Assets/Scripts/Inventories/PlayerInventoryUI.cs b/Assets/Scripts/Inventories/PlayerInventoryUI.cs index f6cdda5..ce812ac 100644 --- a/Assets/Scripts/Inventories/PlayerInventoryUI.cs +++ b/Assets/Scripts/Inventories/PlayerInventoryUI.cs @@ -1,5 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Events; +using Players; +using Registers; using UnityEngine; using UnityEngine.UI; @@ -14,15 +17,13 @@ public class PlayerInventoryUI : MonoBehaviour [SerializeField] private GameObject slotPointer = null; private Canvas canvas; - private List itemSlots = new List(PlayerInventory.Slots); - + private PlayerInventory playerInventory; private bool toggled = false; private void Awake() { EiramEvents.PlayerToggleInventoryEvent += OnPlayerToggleInventoryEvent; - EiramEvents.PlayerInventoryIsDirtyEvent += OnPlayerInventoryIsDirty; EiramEvents.SelectedSlotChangedEvent += OnSelectedSlotChanged; canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent(); @@ -30,10 +31,23 @@ private void Awake() GenerateUI(); } + private void Start() + { + playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent().playerInventory; + } + + private void Update() + { + if (playerInventory.IsDirty) + { + playerInventory.IsDirty = false; + Refresh(); + } + } + private void OnDestroy() { EiramEvents.PlayerToggleInventoryEvent -= OnPlayerToggleInventoryEvent; - EiramEvents.PlayerInventoryIsDirtyEvent -= OnPlayerInventoryIsDirty; EiramEvents.SelectedSlotChangedEvent -= OnSelectedSlotChanged; } @@ -56,21 +70,17 @@ private void MovePointer(int slotIndex) private void OnPlayerToggleInventoryEvent(PlayerInventory playerInventory) { Debug.Assert(PlayerInventory.Slots == itemSlots.Count); - if(toggled) CloseInventory(); else OpenInventory(playerInventory); + if(toggled) CloseInventory(); else OpenInventory(); toggled = !toggled; + Refresh(); } - private void OnPlayerInventoryIsDirty(PlayerInventory playerInventory) - { - Refresh(playerInventory); - } - - private void OnSelectedSlotChanged(PlayerInventory playerInventory, int slotIndex) + private void OnSelectedSlotChanged(int slotIndex) { MovePointer(slotIndex); } - private void Refresh(PlayerInventory playerInventory) + private void Refresh() { for(int i = 0; i < playerInventory.ItemStacks.Count; i++) { @@ -88,10 +98,10 @@ private void Refresh(PlayerInventory playerInventory) } } - private void OpenInventory(PlayerInventory playerInventory) + private void OpenInventory() { LeanTween.moveY(gameObject, transform.position.y - 460.0f, 0.4f); - Refresh(playerInventory); + Refresh(); } private void CloseInventory() diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index 9b49127..d896f03 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -13,12 +13,13 @@ namespace Players //[RequireComponent(typeof(Animator))] public class Player : MonoBehaviour { + public PlayerInventory playerInventory; + [SerializeField] private float jumpForce = 400f; [SerializeField] private float movementSpeed = 10f; private Camera mainCamera = null; private CharacterController controller = null; - private PlayerInventory playerInventory = new PlayerInventory(); // private Animator animator = null; private bool isPlayerIdle = true; @@ -40,13 +41,6 @@ void Update() CheckForMouseInput(); CheckPlayerJump(); CheckPlayerIdle(); - - // TODO: the player should not need to handle this - if (playerInventory.IsDirty) - { - EiramEvents.OnPlayerInventoryIsDirty(playerInventory); - playerInventory.IsDirty = false; - } } public void ApplyPlayerData(PlayerData playerData) diff --git a/Assets/Scripts/Worlds/World.cs b/Assets/Scripts/Worlds/World.cs index 91072df..f5e3ca4 100644 --- a/Assets/Scripts/Worlds/World.cs +++ b/Assets/Scripts/Worlds/World.cs @@ -32,12 +32,12 @@ private void Awake() playerObject = GameObject.FindGameObjectWithTag("Player"); player = playerObject.GetComponent(); Save = Filesystem.CreateSave("DEBUG_SAVE"); + LoadWorld(); } void Start() { InvokeRepeating(nameof(ChunkRefresh), 0.0f, 1.0f); - LoadWorld(); } private void OnDestroy()