Fix issue with UI container update
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -6,7 +7,7 @@ public class ContainerItem : MonoBehaviour
|
|||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private ContainerSO _containerSO;
|
private ContainerSO _containerSO;
|
||||||
|
public event EventHandler OnItemsChange;
|
||||||
|
|
||||||
private List<BaseItemSO> _items = new List<BaseItemSO>();
|
private List<BaseItemSO> _items = new List<BaseItemSO>();
|
||||||
|
|
||||||
@@ -21,12 +22,27 @@ public class ContainerItem : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Debug.Log($"Player put to container a {item.ItemName}");
|
Debug.Log($"Player put to container a {item.ItemName}");
|
||||||
_items.Add(item);
|
_items.Add(item);
|
||||||
|
OnItemsChange?.Invoke(this,EventArgs.Empty);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Log("Container is full");
|
Debug.Log("Container is full");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Remove(BaseItemSO item)
|
||||||
|
{
|
||||||
|
if (_items.Count>0)
|
||||||
|
{
|
||||||
|
Debug.Log($"Player remove {item.ItemName} from container");
|
||||||
|
_items.Remove(item);
|
||||||
|
OnItemsChange?.Invoke(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Log("Container is emplty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<BaseItemSO> GetItems()
|
public List<BaseItemSO> GetItems()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using Assets.Scripts.Interfaces;
|
using Assets.Scripts.Interfaces;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Player : BaseCharacter
|
public class Player : BaseCharacter
|
||||||
{
|
{
|
||||||
|
public event EventHandler<bool> OnContainerChanged;
|
||||||
public static Player Instance { get; private set; }
|
public static Player Instance { get; private set; }
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
@@ -118,16 +120,21 @@ public class Player : BaseCharacter
|
|||||||
containerItem.transform.parent = _holdPoint;
|
containerItem.transform.parent = _holdPoint;
|
||||||
containerItem.transform.localPosition = Vector3.zero;
|
containerItem.transform.localPosition = Vector3.zero;
|
||||||
_containerItem = containerItem;
|
_containerItem = containerItem;
|
||||||
|
OnContainerChanged.Invoke(this,true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClearContainerItem()
|
||||||
|
{
|
||||||
|
OnContainerChanged.Invoke(this, false);
|
||||||
|
Destroy(_containerItem.gameObject);
|
||||||
|
_containerItem = null;
|
||||||
|
}
|
||||||
|
|
||||||
public ContainerItem GetContainerItem()
|
public ContainerItem GetContainerItem()
|
||||||
{
|
{
|
||||||
return _containerItem;
|
return _containerItem;
|
||||||
}
|
}
|
||||||
public void ClearContainerItem()
|
|
||||||
{
|
|
||||||
Destroy(_containerItem.gameObject);
|
|
||||||
_containerItem = null;
|
|
||||||
}
|
|
||||||
public bool IsHoldContainerItem()
|
public bool IsHoldContainerItem()
|
||||||
{
|
{
|
||||||
return _containerItem != null;
|
return _containerItem != null;
|
||||||
|
|||||||
@@ -10,40 +10,47 @@ public class ContainerItemsUI : MonoBehaviour
|
|||||||
[SerializeField]
|
[SerializeField]
|
||||||
private Transform _itemUITemplate;
|
private Transform _itemUITemplate;
|
||||||
|
|
||||||
|
private ContainerItem _playerContainer;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
_container.gameObject.SetActive(false);
|
_container.gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Start()
|
||||||
{
|
{
|
||||||
if (Player.Instance.IsHoldContainerItem())
|
Player.Instance.OnContainerChanged += OnContainerChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnContainerChanged(object sender, bool e)
|
||||||
|
{
|
||||||
|
if (e)
|
||||||
{
|
{
|
||||||
var playerContainer = Player.Instance.GetContainerItem();
|
|
||||||
_container.gameObject.SetActive(true);
|
_container.gameObject.SetActive(true);
|
||||||
|
_playerContainer = Player.Instance.GetContainerItem();
|
||||||
if (playerContainer.IsSalebleItems())
|
_playerContainer.OnItemsChange += OnItemsChange;
|
||||||
{
|
|
||||||
ClearObject();
|
|
||||||
|
|
||||||
var playerItemsList = playerContainer.GetItems();
|
|
||||||
for (int count = 0; count < playerItemsList.Count; count++)
|
|
||||||
{
|
|
||||||
var itemUI = Instantiate(_itemUITemplate, _itemsList);
|
|
||||||
itemUI.gameObject.SetActive(true);
|
|
||||||
itemUI.GetComponent<ItemDescriptionUI>().SetItem((SellableItemSO)playerItemsList[count]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ClearObject();
|
|
||||||
_container.gameObject.SetActive(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ClearObject();
|
|
||||||
_container.gameObject.SetActive(false);
|
_container.gameObject.SetActive(false);
|
||||||
|
_playerContainer.OnItemsChange -= OnItemsChange;
|
||||||
|
_playerContainer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnItemsChange(object sender, System.EventArgs e)
|
||||||
|
{
|
||||||
|
if (_playerContainer.IsSalebleItems())
|
||||||
|
{
|
||||||
|
ClearObject();
|
||||||
|
|
||||||
|
var playerItemsList = _playerContainer.GetItems();
|
||||||
|
for (int count = 0; count < playerItemsList.Count; count++)
|
||||||
|
{
|
||||||
|
var itemUI = Instantiate(_itemUITemplate, _itemsList);
|
||||||
|
itemUI.gameObject.SetActive(true);
|
||||||
|
itemUI.GetComponent<ItemDescriptionUI>().SetItem((SellableItemSO)playerItemsList[count]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user