Skip to content

Commit

Permalink
Refactor: player UI now depends in player inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdelahunt committed Nov 12, 2021
1 parent f8ee3b7 commit 6885402
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
13 changes: 3 additions & 10 deletions Assets/Scripts/Events/EiramEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@ public static void OnPlayerToggleInventory(PlayerInventory playerInventory)
PlayerToggleInventoryEvent?.Invoke(playerInventory);
}

public static event Action<PlayerInventory> PlayerInventoryIsDirtyEvent;
public static event Action<int> SelectedSlotChangedEvent;

public static void OnPlayerInventoryIsDirty(PlayerInventory playerInventory)
public static void SelectedSlotChanged(int slotIndex)
{
PlayerInventoryIsDirtyEvent?.Invoke(playerInventory);
}

public static event Action<PlayerInventory, int> SelectedSlotChangedEvent;

public static void SelectedSlotChanged(PlayerInventory playerInventory, int slotIndex)
{
SelectedSlotChangedEvent?.Invoke(playerInventory, slotIndex);
SelectedSlotChangedEvent?.Invoke(slotIndex);
}

}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Inventories/PlayerInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 25 additions & 15 deletions Assets/Scripts/Inventories/PlayerInventoryUI.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,26 +17,37 @@ public class PlayerInventoryUI : MonoBehaviour
[SerializeField] private GameObject slotPointer = null;

private Canvas canvas;

private List<ItemSlot> itemSlots = new List<ItemSlot>(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<Canvas>();

GenerateUI();
}

private void Start()
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().playerInventory;
}

private void Update()
{
if (playerInventory.IsDirty)
{
playerInventory.IsDirty = false;
Refresh();
}
}

private void OnDestroy()
{
EiramEvents.PlayerToggleInventoryEvent -= OnPlayerToggleInventoryEvent;
EiramEvents.PlayerInventoryIsDirtyEvent -= OnPlayerInventoryIsDirty;
EiramEvents.SelectedSlotChangedEvent -= OnSelectedSlotChanged;
}

Expand All @@ -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++)
{
Expand All @@ -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()
Expand Down
10 changes: 2 additions & 8 deletions Assets/Scripts/Players/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Worlds/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ private void Awake()
playerObject = GameObject.FindGameObjectWithTag("Player");
player = playerObject.GetComponent<Player>();
Save = Filesystem.CreateSave("DEBUG_SAVE");
LoadWorld();
}

void Start()
{
InvokeRepeating(nameof(ChunkRefresh), 0.0f, 1.0f);
LoadWorld();
}

private void OnDestroy()
Expand Down

0 comments on commit 6885402

Please sign in to comment.