CORE dashboard + a lot of changes
This commit is contained in:
@@ -11,14 +11,26 @@ using UnityEngine;
|
||||
namespace RPGCore.BackpackEquipment.ObjectModules.Content
|
||||
{
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(BaseObject))]
|
||||
[ObjectModule(
|
||||
name: "[Inventory] Content",
|
||||
description: "Simple content managing attached to any implementation of <b>BaseObject</b>. " +
|
||||
"By itself allows only: taking, dropping and deciding who manages items. " +
|
||||
"Attach implementation of <b>"+nameof(IContentOwner)+"</b> to manage content's items."
|
||||
)]
|
||||
public sealed class ContentModule : ObjectModule<BaseObject>
|
||||
{
|
||||
// TODO: tutaj jakiś domyślny owner?
|
||||
|
||||
// RUNTIME
|
||||
private GameObject _contentGameObject;
|
||||
private Dictionary<ItemObject, IContentOwner> _items = new();
|
||||
|
||||
public bool Add(ItemObject item)
|
||||
private void Awake()
|
||||
{
|
||||
_contentGameObject = new GameObject("-- CONTENT --");
|
||||
_contentGameObject.transform.SetParent(transform);
|
||||
_contentGameObject.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
||||
}
|
||||
|
||||
public bool Take(ItemObject item)
|
||||
{
|
||||
if (item.data.Get<ItemObjectData>().carriedBy) return false;
|
||||
if (_items.ContainsKey(item)) return false;
|
||||
@@ -27,26 +39,33 @@ namespace RPGCore.BackpackEquipment.ObjectModules.Content
|
||||
item.data.Get<ItemObjectData>().carriedBy = parent;
|
||||
parent.events.Invoke(new ContentAddEvent{obj = parent, item = item});
|
||||
item.events.Register<RemoveEvent>(OnItemRemove);
|
||||
|
||||
item.gameObject.SetActive(false);
|
||||
item.transform.SetParent(_contentGameObject.transform);
|
||||
item.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(ItemObject item)
|
||||
public bool Drop(ItemObject item)
|
||||
{
|
||||
if (!_items.ContainsKey(item)) return false;
|
||||
|
||||
_items[item]?.Remove_Internal(item);
|
||||
_items.Remove(item);
|
||||
item.data.Get<ItemObjectData>().carriedBy = null;
|
||||
parent.events.Invoke(new ContentAddEvent{obj = parent, item = item});
|
||||
parent.events.Invoke(new ContentRemoveEvent { obj = parent, item = item });
|
||||
item.events.Unregister<RemoveEvent>(OnItemRemove);
|
||||
|
||||
item.gameObject.SetActive(true);
|
||||
item.transform.SetParent(null);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TransferTo(IContentOwner newOwner, ItemObject item, int index = -1)
|
||||
{
|
||||
if (parent.data.Get<ItemObjectData>().carriedBy != parent) return false;
|
||||
if (item.data.Get<ItemObjectData>().carriedBy != parent) return false;
|
||||
|
||||
if (!newOwner.CanAdd(item, index)) return false;
|
||||
|
||||
@@ -58,9 +77,14 @@ namespace RPGCore.BackpackEquipment.ObjectModules.Content
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TransferFrom(ItemObject item)
|
||||
{
|
||||
return _items[item]?.Remove_Internal(item) ?? false;
|
||||
}
|
||||
|
||||
private void OnItemRemove(RemoveEvent removeEvent)
|
||||
{
|
||||
Remove(removeEvent.obj as ItemObject);
|
||||
Drop(removeEvent.obj as ItemObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user