Panel refactoring - player equipment

This commit is contained in:
kabix09 2022-11-24 03:03:30 +01:00
parent 7f4aec84b3
commit bccbc439e7
59 changed files with 1863 additions and 1910 deletions

View File

@ -23,4 +23,4 @@ MonoBehaviour:
InteligenceBonus: 5
VitalityBonus: 0
isStackable: 0
EquipmentType: 4
EquipmentType: 5

View File

@ -23,4 +23,4 @@ MonoBehaviour:
InteligenceBonus: 0
VitalityBonus: 0
isStackable: 0
EquipmentType: 4
EquipmentType: 5

View File

@ -150,7 +150,7 @@ GameObject:
- component: {fileID: 102800839174329912}
- component: {fileID: 2957687053509197962}
m_Layer: 5
m_Name: equipment_slot
m_Name: EquipmentBox
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d220ca711ae86664d8bb4f9c1622b13f
guid: eca52e174922c014ba37e2e8c0884dd8
PrefabImporter:
externalObjects: {}
userData:

View File

@ -1,269 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class EquipmentManager : BaseWarehouseController
{
public static int MAX_ITEMS = 0;
[SerializeField] public EquippableItem _helmet;
[SerializeField] public EquippableItem _chest;
[SerializeField] public EquippableItem _boots;
[SerializeField] public EquippableItem _weapon;
[SerializeField] public EquippableItem _potion_one;
[SerializeField] public EquippableItem _potion_two;
[SerializeField] public EquippableItem _potion_three;
[SerializeField] public EquippableItem _potion_four;
[SerializeField] public EquippableItem _potion_five;
public static EquipmentManager Instance;
Dictionary<int, Item> equipment;
private void Awake()
{
if(Instance == null)
{
Instance = this;
equipment = new Dictionary<int, Item>() {
{ 0, _helmet },
{ 1, _chest },
{ 2, _boots },
{ 3, _weapon },
{ 4, _potion_one },
{ 5, _potion_two },
{ 6, _potion_three },
{ 7, _potion_four },
{ 8, _potion_five }
};
}else if (Instance != this)
{
Destroy(gameObject);
}
}
protected override void SetupPanel()
{
if(this.dynamicPanel)
{
this.dynamicPanel.GetComponent<EquipmentPanelController>().Setup(gameObject, equipment);
}
}
// Handle in Manager items local list
public override void SetItemOnPosition(int _keyPosition, Item _item)
{
// in qeuippment panel its case where we drop drop on empty slot
if(_item == null) // if we move eg. form chest to eq Drop work on Eq Panel where dropitemSlot.Item is Null
return;
_keyPosition = MapItemTypeToSlotNumber((EquippableItem)_item);
this.equipment[_keyPosition] = _item;
this.ApplyEquipmentObject(true, (EquippableItem)_item);
base.SetItemOnPosition(_keyPosition, _item);
}
// Remove from Manager items local list
public override void RemoveItemFromPosition(int _keyPosition)
{
this.equipment.Remove(_keyPosition);
this.ApplyEquipmentObject(_keyPosition);
base.RemoveItemFromPosition(_keyPosition);
}
/*
* Function decide in which slot item should be handled
* Mach item to dictionary key based on items equippment type
* return: dictionary key
*/
private int MapItemTypeToSlotNumber(EquippableItem _item)
{
int key=-1;
if(_item == null)
{
Debug.LogError("Ten item w ogóle nie powinien być rozpatrywany jako dodany do ekwipunku - nigdy nie był typu EquippableItem");
}
switch(_item.EquipmentType)
{
case EquipmentTypeEnum.Helmet:
{
key = 0;
break;
}
case EquipmentTypeEnum.Chest:
{
key = 1;
break;
}
case EquipmentTypeEnum.Boots:
{
key = 2;
break;
}
case EquipmentTypeEnum.Weapon:
{
key = 3;
break;
}
case EquipmentTypeEnum.Potion:
{
key = 4;
break;
}
case EquipmentTypeEnum.Bracelet:
{
key = 5;
break;
}
case EquipmentTypeEnum.Necklet:
{
key = 6;
break;
}
case EquipmentTypeEnum.Ring:
{
if(!equipment.ContainsKey(7) || equipment[7] == null)
{
key = 7;
}
if(!equipment.ContainsKey(8) || equipment[8] == null)
{
key = 8;
}
break;
}
default:
{
Debug.Log("Can't mach number to item type");
break;
}
}
return key;
}
/*
* applu EquippableItem to be able to see actual equipment status - mapped with 'qeuipment' Dictioanry which is not showed in amnager panel :/
*/
public void ApplyEquipmentObject(bool put, EquippableItem _item = null)
{
switch(_item.EquipmentType)
{
case EquipmentTypeEnum.Helmet:
{
_helmet = put ? _item : null;
break;
}
case EquipmentTypeEnum.Chest:
{
_chest = put ? _item : null;
break;
}
case EquipmentTypeEnum.Boots:
{
_boots = put ? _item : null;
break;
}
case EquipmentTypeEnum.Weapon:
{
_weapon = put ? _item : null;
break;
}
case EquipmentTypeEnum.Potion:
{
_potion_one = put ? _item : null;
break;
}
case EquipmentTypeEnum.Bracelet:
{
_potion_two = put ? _item : null;
break;
}
case EquipmentTypeEnum.Necklet:
{
_potion_three = put ? _item : null;
break;
}
case EquipmentTypeEnum.Ring:
{
if(!equipment.ContainsKey(7) || equipment[7] == null)
{
_potion_four = put ? _item : null;
}
if(!equipment.ContainsKey(8) || equipment[8] == null)
{
_potion_five = put ? _item : null;
}
break;
}
default:
{
Debug.Log("Can't mach number to item type");
break;
}
}
}
public void ApplyEquipmentObject(int _keyPosition)
{
switch(_keyPosition)
{
case 0:
{
_helmet = null;
break;
}
case 1:
{
_chest = null;
break;
}
case 2:
{
_boots = null;
break;
}
case 3:
{
_weapon = null;
break;
}
case 4:
{
_potion_one = null;
break;
}
case 5:
{
_potion_two = null;
break;
}
case 6:
{
_potion_three = null;
break;
}
case 7:
{
_potion_four = null;
break;
}
case 9:
{
_potion_five = null;
break;
}
}
}
}

View File

@ -1,65 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentPanelController : BasePanelController
{
[Header("Slots List")]
[SerializeField] public const int MAX_SLOT_CUNT = 0;
[SerializeField] public EquipmentSlot _helmet;
[SerializeField] public EquipmentSlot _chest;
[SerializeField] public EquipmentSlot _boots;
[SerializeField] public EquipmentSlot _weapon;
[SerializeField] public EquipmentSlot _potion_one;
[SerializeField] public EquipmentSlot _potion_two;
[SerializeField] public EquipmentSlot _potion_three;
[SerializeField] public EquipmentSlot _potion_four;
[SerializeField] public EquipmentSlot _potion_five;
Dictionary<int, EquipmentSlot> equipment;
// overrwrite this metoh in in parent beacuse we dont have to init any slots
void Awake()
{
base.MAX_SLOT_CUNT = 9;
equipment = new Dictionary<int, EquipmentSlot>() {
{ 0, _helmet },
{ 1, _chest },
{ 2, _boots },
{ 3, _weapon },
{ 4, _potion_one },
{ 5, _potion_two },
{ 6, _potion_three },
{ 7, _potion_four },
{ 8, _potion_five }
};
base.Awake();
}
public override void CloseOnClick()
{
Destroy(gameObject); // destroy panel
if(_instance)
{
_instance.GetComponent<EquipmentManager>().ClosePanel();
}
}
public void Setup(GameObject _equipment, Dictionary<int, Item> _equipmentItems)
{
_instance = _equipment;
base.Setup(_equipmentItems);
}
protected override ISlot SetupSlot(int key, GameObject _parent)
{
ISlot tmp = equipment[key];
tmp.SetupSlot(key, null, this);
return tmp;
}
}

View File

@ -39,7 +39,7 @@ public class PickableController : MonoBehaviour
{
if (Input.GetKeyDown(KeyCode.E))
{
if(InventoryUIManager.Instance.IsFull())
if(!InventoryUIManager.Instance.IsFull())
{
InventoryUIManager.Instance.Add(this.item);
isPicked = 1;

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 73c868471f0c43479d69afc446536d8b
timeCreated: 1660171705

View File

@ -105,13 +105,13 @@ public class SceneChestDataLoader : SceneBaseDataLoader<ChestPrefabAsset>
Debug.Log(SaveModelSystem.Path);
/* try
{*/
try
{
SaveModelSystem.SaveModelList(_elements);
return true;
/* }
catch (Exception e) { Debug.LogError(e.Message); }*/
}
catch (Exception e) { Debug.LogError(e.Message); }
return false;
}

View File

@ -22,7 +22,7 @@ public class SceneChestDataManager : SceneBaseDataManager<ChestPrefabAsset>
// tmp var
bool NewGame = false;
bool NewGame = true;
public override void Awake()
{

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 4e66e807c2717ee45923ec12b22309d0
guid: d0d5e0b950ca6424a96910337a7f6325
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class EquipmentDataListManager : DataListManager<IndexValuePair<int, EquippableItem>>
{
public new DataListManager<IndexValuePair<int, EquippableItem>> SetUiManager(ref EquipmentUIManager _uiManager)
{
uiManager = _uiManager;
return this;
}
/// <summary>
/// Function to init equipment list
/// We should init this before each operation on equipment
/// </summary>
/// <returns></returns>
public List<IndexValuePair<int, EquippableItem>> InitEquipment()
{
List<IndexValuePair<int, EquippableItem>> convertedList = new List<IndexValuePair<int, EquippableItem>>();
foreach (EquipmentPanelSlotsTypeEnum emptyElement in Enum.GetValues(typeof(EquipmentPanelSlotsTypeEnum))) { convertedList.Add(new IndexValuePair<int, EquippableItem>((int)emptyElement, null)); }
return convertedList;
}
public override void AddElementToList(IndexValuePair<int, EquippableItem> newElement)
{
Elements
.Where(equipment => equipment.Key == newElement.Key)
.ToList()
.ForEach(equipment => equipment.Value = newElement.Value);
}
public override void RemoveElementFromList(IndexValuePair<int, EquippableItem> element)
{
Elements
.Where(equipment => equipment.Key == element.Key)
.ToList()
.ForEach(equipment => equipment.Value = null);
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 0ba28abf0f2ebba45a0f7743b05d08d9
guid: 9d53e77851b9a5e47bdc923549ae2993
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class EquipmentDataLoader : SceneBaseDataLoader<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>
{
public EquipmentDataLoader(string _objectListName, string _objectFolderName)
{
SaveModelSystem = new SaveEquipmentManager();
SaveModelSystem.ObjectFolderName = _objectFolderName;
SaveModelSystem.ObjectListName = _objectListName;
}
protected override List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> LoadGenericData()
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
return SaveModelSystem.LoadModelList();
}
protected override bool SaveGenericData(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> _elements)
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
try
{
SaveModelSystem.SaveModelList(_elements);
return true;
}
catch (Exception e) { Debug.LogError(e.Message); }
return false;
}
protected override List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> LoadDynamicData()
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
return SaveModelSystem.LoadModelList();
}
protected override List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> LoadStaticData(){ throw new System.NotImplementedException(); }
protected override bool SaveDynamicData(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> _elements)
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
try
{
SaveModelSystem.SaveModelList(_elements);
return true;
}
catch (Exception e) { Debug.LogError(e.Message); }
return false;
}
protected override bool SaveStaticData(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> _elements) { throw new System.NotImplementedException(); }
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 15919bbecc01e444bbe2a22c7840a518
guid: 25443d561c199cd4ba9ee3b0aeea1014
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class EquipmentDataManager : SceneBaseDataManager<IndexValuePair<int, EquippableItem>>
{
protected override string OBJECT_FOLDER_NAME { get { return "Equipment"; } }
protected override string OBJECT_LIST_NAME { get { return "EquipmentList"; } }
protected new SceneBaseDataLoader<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> DataLoader { get; set; }
bool NewGame = false;
public override void Awake()
{
if (Instance == null)
{
Debug.Log("Create: " + gameObject);
Instance = this;
}
else
{
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
}
}
private void Start()
{
Debug.Log("Start SceneEquipmentData manager");
//TaskUIManager.FindOrCreateInstance();
var taskManager = EquipmentUIManager.Instance;
if (taskManager == null)
throw new NullReferenceException("EquipmentUIManager not found!!!");
//StaticDataList = (new EquipmentDataListManager()).SetUiManager(ref taskManager);
DynamicDataList = (new EquipmentDataListManager()).SetUiManager(ref taskManager);
DataLoader = new EquipmentDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
// taskManager.SetList();
//SaveData(taskManager.GetList(), SceneElementTypeEnum.None);
((EquipmentDataListManager)DynamicDataList).InitEquipment();
//LoadData(SceneElementTypeEnum.None, ref StaticDataList);
//taskManager.SetList(StaticDataList.GetList());
if (NewGame)
{
//taskManager.SetList(tmp);
}
else
{
LoadDynamicData();
taskManager.SetList(DynamicDataList.GetList());
// BuildList();
// when chest detect player in near arrea and player press "c"
// open panel -> chest controller handle Open panel in Scene Chest Manager passing info about what Chest Palyer want to open
// Manager Build panel and pass info about chest content
}
}
#region override load & save
protected override bool LoadData(SceneElementTypeEnum type, ref DataListManager<IndexValuePair<int, EquippableItem>> dataListManager)
{
try
{
// 1. Convert EquippableItemPrefabAsset to EquippableItem list
List<IndexValuePair<int, EquippableItem>> convertedList = new List<IndexValuePair<int, EquippableItem>>();
foreach (IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>)DataLoader.LoadData(type))
{
if(loadedEquippableItemPrefarbAssetElement.Value != null)
convertedList.Add(new IndexValuePair<int, EquippableItem>((int)loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
else
convertedList.Add(new IndexValuePair<int, EquippableItem>((int)loadedEquippableItemPrefarbAssetElement.Key, null));
}
// 2. Pass loaded list to InventoryDataManager
dataListManager.SetList(convertedList);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return false;
}
protected override bool SaveData(List<IndexValuePair<int, EquippableItem>> _elements, SceneElementTypeEnum type)
{
try
{
// 1. Convert EquippableItem to EquippableItemPrefabAsset list
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>();
foreach (IndexValuePair<int, EquippableItem> itemElement in _elements)
{
if(itemElement.Value)
convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>(
(EquipmentPanelSlotsTypeEnum)itemElement.Key,
new EquippableItemPrefabAsset(
itemElement.Value.Name,
itemElement.Value.ItemModel.name,
new Vector3(0, 0, 0),
itemElement.Value
)
));
else
convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>((EquipmentPanelSlotsTypeEnum)itemElement.Key, null));
}
// 2. Pass loaded list to InventoryDataManager
DataLoader.SaveData(convertedList, type);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return false;
}
#endregion
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
{
return GameObject.FindObjectOfType<EquipmentDataManager>();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<EquipmentDataManager>();
}
public override bool SaveDynamicData()
{
Debug.Log("SaveDynamicData");
// tem approach
DynamicDataList.SetList(EquipmentUIManager.Instance.GetList());
return base.SaveDynamicData();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1126efb434c50f446b49891f5584c786
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class SaveEquipmentManager : SaveModelSystem<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>
{
public override bool SaveModelItem(IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> model)
{
return base.SaveModelItem(model);
}
public override bool SaveModelList(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> list)
{
BinaryFormatter formatter = new BinaryFormatter();
Debug.Log("Saved Equipment at " + Path);
if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
/* Main logic of conversion data format */
var data = ConvertObjectsListToListOfDataModels(list);
FileStream stream = new FileStream(
Path + GetFileName(ObjectListName),
FileMode.Create
);
formatter.Serialize(stream, data);
stream.Close();
return true;
}
public override IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> LoadModelItem()
{
return base.LoadModelItem();
}
public override List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> LoadModelList()
{
string path = Path + GetFileName(ObjectListName);
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> euipmentList = formatter.Deserialize(stream) as List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>>;
stream.Close();
return ConvertListOfDataModelsToListOfObject(euipmentList);
}
else
{
Debug.Log("Save file not found in " + path);
}
return new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>();
}
// Support function
// 1. From model to data format
private IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData> ConvertObjectToDataModel(IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> model)
{
if (model.Value != null)
return new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>(model.Key, new EquippableItemPrefabAssetData(model.Value));
else
return new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>(model.Key, null);
}
private List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> ConvertObjectsListToListOfDataModels(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> modelsList)
{
// 1. prepare list with all slot possibiliteies
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> convertedList = GetEmptyListOfEquipmentSlotsPrefabData();
// 2. Assign values
foreach (IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> model in modelsList)
{
convertedList.RemoveAll(slot => slot.Key == model.Key);
convertedList.Add(ConvertObjectToDataModel(model));
}
return convertedList;
}
// 2. From data to model format
private IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> ConvertDataModelToObject(IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData> dataModel)
{
if(dataModel.Value != null)
return new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>(dataModel.Key, (EquippableItemPrefabAsset)dataModel.Value.MapDataToPrefabAssetModel());
else
return new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>(dataModel.Key, null);
}
private List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> ConvertListOfDataModelsToListOfObject(List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> dataModelsList)
{
// 1. prepare list with all slot possibiliteies
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> convertedList = GetEmptyListOfEquipmentSlotsPrefab();
// 2. Assign values
foreach (IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData> dataModel in dataModelsList)
{
convertedList.RemoveAll(slot => slot.Key == (EquipmentPanelSlotsTypeEnum)dataModel.Key);
convertedList.Add(ConvertDataModelToObject(dataModel));
}
return convertedList;
}
// Empty list factories functions
private List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> GetEmptyListOfEquipmentSlotsPrefab()
{
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>();
foreach (EquipmentPanelSlotsTypeEnum emptyElement in Enum.GetValues(typeof(EquipmentPanelSlotsTypeEnum))) { convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>(emptyElement, null)); }
return convertedList;
}
private List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> GetEmptyListOfEquipmentSlotsPrefabData()
{
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>> convertedList = new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>>();
foreach (EquipmentPanelSlotsTypeEnum emptyElement in Enum.GetValues(typeof(EquipmentPanelSlotsTypeEnum))) { convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAssetData>(emptyElement, null)); }
return convertedList;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 71f06b6e41edc7c4aa8393be45e09f12
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,21 +1,21 @@
using System.Collections.Generic;
using UnityEngine;
public class InventoryDataListManager : DataListManager<KeyValuePair<int, Item>> // or maybe EquippableItem - but its get conflicts...
public class InventoryDataListManager : DataListManager<IndexValuePair<int, EquippableItem>> // or maybe EquippableItem - but its get conflicts...
{
public new DataListManager<KeyValuePair<int, Item>> SetUiManager(ref InventoryUIManager _uiManager)
public new DataListManager<IndexValuePair<int, EquippableItem>> SetUiManager(ref InventoryUIManager _uiManager)
{
uiManager = _uiManager;
return this;
}
public override void AddElementToList(KeyValuePair<int, Item> newElement)
public override void AddElementToList(IndexValuePair<int, EquippableItem> newElement)
{
Elements.Add(newElement);
}
public override void RemoveElementFromList(KeyValuePair<int, Item> element)
public override void RemoveElementFromList(IndexValuePair<int, EquippableItem> element)
{
throw new System.NotImplementedException();
}

View File

@ -3,14 +3,14 @@ using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class SaveInventoryManager : SaveModelSystem<KeyValuePair<int, EquippableItemPrefabAsset>>
public class SaveInventoryManager : SaveModelSystem<IndexValuePair<int, EquippableItemPrefabAsset>>
{
public override bool SaveModelItem(KeyValuePair<int, EquippableItemPrefabAsset> model)
public override bool SaveModelItem(IndexValuePair<int, EquippableItemPrefabAsset> model)
{
return base.SaveModelItem(model);
}
public override bool SaveModelList(List<KeyValuePair<int, EquippableItemPrefabAsset>> list)
public override bool SaveModelList(List<IndexValuePair<int, EquippableItemPrefabAsset>> list)
{
BinaryFormatter formatter = new BinaryFormatter();
@ -32,12 +32,12 @@ public class SaveInventoryManager : SaveModelSystem<KeyValuePair<int, Equippable
return true;
}
public override KeyValuePair<int, EquippableItemPrefabAsset> LoadModelItem()
public override IndexValuePair<int, EquippableItemPrefabAsset> LoadModelItem()
{
return base.LoadModelItem();
}
public override List<KeyValuePair<int, EquippableItemPrefabAsset>> LoadModelList()
public override List<IndexValuePair<int, EquippableItemPrefabAsset>> LoadModelList()
{
string path = Path + GetFileName(ObjectListName);
@ -46,7 +46,7 @@ public class SaveInventoryManager : SaveModelSystem<KeyValuePair<int, Equippable
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<KeyValuePair<int, EquippableItemPrefabAssetData>> questsList = formatter.Deserialize(stream) as List<KeyValuePair<int, EquippableItemPrefabAssetData>>;
List<IndexValuePair<int, EquippableItemPrefabAssetData>> questsList = formatter.Deserialize(stream) as List<IndexValuePair<int, EquippableItemPrefabAssetData>>;
stream.Close();
return ConvertListOfDataModelsToListOfObject(questsList);
@ -56,22 +56,22 @@ public class SaveInventoryManager : SaveModelSystem<KeyValuePair<int, Equippable
Debug.Log("Save file not found in " + path);
}
return new List<KeyValuePair<int, EquippableItemPrefabAsset>>();
return new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
}
// Support function
// 1. From model to data format
private KeyValuePair<int, EquippableItemPrefabAssetData> ConvertObjectToDataModel(KeyValuePair<int, EquippableItemPrefabAsset> model)
private IndexValuePair<int, EquippableItemPrefabAssetData> ConvertObjectToDataModel(IndexValuePair<int, EquippableItemPrefabAsset> model)
{
return new KeyValuePair<int, EquippableItemPrefabAssetData>(model.Key, new EquippableItemPrefabAssetData(model.Value));
return new IndexValuePair<int, EquippableItemPrefabAssetData>(model.Key, new EquippableItemPrefabAssetData(model.Value));
}
private List<KeyValuePair<int, EquippableItemPrefabAssetData>> ConvertObjectsListToListOfDataModels(List<KeyValuePair<int, EquippableItemPrefabAsset>> modelsList)
private List<IndexValuePair<int, EquippableItemPrefabAssetData>> ConvertObjectsListToListOfDataModels(List<IndexValuePair<int, EquippableItemPrefabAsset>> modelsList)
{
List<KeyValuePair<int, EquippableItemPrefabAssetData>> convertedList = new List<KeyValuePair<int, EquippableItemPrefabAssetData>>();
List<IndexValuePair<int, EquippableItemPrefabAssetData>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAssetData>>();
foreach(KeyValuePair <int, EquippableItemPrefabAsset> model in modelsList)
foreach(IndexValuePair<int, EquippableItemPrefabAsset> model in modelsList)
{
convertedList.Add(ConvertObjectToDataModel(model));
}
@ -80,16 +80,16 @@ public class SaveInventoryManager : SaveModelSystem<KeyValuePair<int, Equippable
}
// 2. From data to model format
private KeyValuePair<int, EquippableItemPrefabAsset> ConvertDataModelToObject(KeyValuePair<int, EquippableItemPrefabAssetData> dataModel)
private IndexValuePair<int, EquippableItemPrefabAsset> ConvertDataModelToObject(IndexValuePair<int, EquippableItemPrefabAssetData> dataModel)
{
return new KeyValuePair<int, EquippableItemPrefabAsset>(dataModel.Key, (EquippableItemPrefabAsset)dataModel.Value.MapDataToPrefabAssetModel());
return new IndexValuePair<int, EquippableItemPrefabAsset>(dataModel.Key, (EquippableItemPrefabAsset)dataModel.Value.MapDataToPrefabAssetModel());
}
private List<KeyValuePair<int, EquippableItemPrefabAsset>> ConvertListOfDataModelsToListOfObject(List<KeyValuePair<int, EquippableItemPrefabAssetData>> dataModelsList)
private List<IndexValuePair<int, EquippableItemPrefabAsset>> ConvertListOfDataModelsToListOfObject(List<IndexValuePair<int, EquippableItemPrefabAssetData>> dataModelsList)
{
List<KeyValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<KeyValuePair<int, EquippableItemPrefabAsset>>();
List<IndexValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
foreach (KeyValuePair<int, EquippableItemPrefabAssetData> dataModel in dataModelsList)
foreach (IndexValuePair<int, EquippableItemPrefabAssetData> dataModel in dataModelsList)
{
convertedList.Add(ConvertDataModelToObject(dataModel));
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class SceneInventoryDataLoader : SceneBaseDataLoader<KeyValuePair<int, EquippableItemPrefabAsset>>
public class SceneInventoryDataLoader : SceneBaseDataLoader<IndexValuePair<int, EquippableItemPrefabAsset>>
{
public SceneInventoryDataLoader(string _objectListName, string _objectFolderName)
{
@ -12,14 +12,14 @@ public class SceneInventoryDataLoader : SceneBaseDataLoader<KeyValuePair<int, Eq
SaveModelSystem.ObjectListName = _objectListName;
}
protected override List<KeyValuePair<int, EquippableItemPrefabAsset>> LoadGenericData()
protected override List<IndexValuePair<int, EquippableItemPrefabAsset>> LoadGenericData()
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
return SaveModelSystem.LoadModelList();
}
protected override bool SaveGenericData(List<KeyValuePair<int, EquippableItemPrefabAsset>> _elements)
protected override bool SaveGenericData(List<IndexValuePair<int, EquippableItemPrefabAsset>> _elements)
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
@ -34,11 +34,31 @@ public class SceneInventoryDataLoader : SceneBaseDataLoader<KeyValuePair<int, Eq
return false;
}
protected override List<KeyValuePair<int, EquippableItemPrefabAsset>> LoadStaticData() { throw new System.NotImplementedException(); }
protected override List<IndexValuePair<int, EquippableItemPrefabAsset>> LoadStaticData() { throw new System.NotImplementedException(); }
protected override List<KeyValuePair<int, EquippableItemPrefabAsset>> LoadDynamicData() { throw new System.NotImplementedException(); }
protected override List<IndexValuePair<int, EquippableItemPrefabAsset>> LoadDynamicData()
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
protected override bool SaveStaticData(List<KeyValuePair<int, EquippableItemPrefabAsset>> _elements) { throw new System.NotImplementedException(); }
return SaveModelSystem.LoadModelList();
}
protected override bool SaveDynamicData(List<KeyValuePair<int, EquippableItemPrefabAsset>> _elements) { throw new System.NotImplementedException(); }
protected override bool SaveStaticData(List<IndexValuePair<int, EquippableItemPrefabAsset>> _elements) { throw new System.NotImplementedException(); }
protected override bool SaveDynamicData(List<IndexValuePair<int, EquippableItemPrefabAsset>> _elements)
{
SaveModelSystem.Path = PathBuilder.BuildSavePath().GetString();
Debug.Log(SaveModelSystem.Path);
try
{
SaveModelSystem.SaveModelList(_elements);
return true;
}
catch (Exception e) { Debug.LogError(e.Message); }
return false;
}
}

View File

@ -3,12 +3,31 @@ using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class SceneInventoryDataManager : SceneBaseDataManager<KeyValuePair<int, Item>>
public class SceneInventoryDataManager : SceneBaseDataManager<IndexValuePair<int, EquippableItem>>
{
protected override string OBJECT_FOLDER_NAME { get { return "Inventory"; } }
protected override string OBJECT_LIST_NAME { get { return "InventoryList"; } }
protected new SceneBaseDataLoader<KeyValuePair<int, EquippableItemPrefabAsset>> DataLoader { get; set; }
protected new SceneBaseDataLoader<IndexValuePair<int, EquippableItemPrefabAsset>> DataLoader { get; set; }
public new static SceneBaseDataManager<IndexValuePair<int, EquippableItem>> Instance; // { get; private set; }
public override void Awake()
{
if (Instance == null)
{
Debug.Log("Create: " + gameObject);
Instance = this;
}
else
{
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
}
}
private void Start()
{
@ -37,16 +56,16 @@ public class SceneInventoryDataManager : SceneBaseDataManager<KeyValuePair<int,
}
#region override load & save
protected override bool LoadData(SceneElementTypeEnum type, ref DataListManager<KeyValuePair<int, Item>> dataListManager)
protected override bool LoadData(SceneElementTypeEnum type, ref DataListManager<IndexValuePair<int, EquippableItem>> dataListManager)
{
try
{
// 1. Convert EquippableItemPrefabAsset to EquippableItem list
List<KeyValuePair<int, Item>> convertedList = new List<KeyValuePair<int, Item>>();
List<IndexValuePair<int, EquippableItem>> convertedList = new List<IndexValuePair<int, EquippableItem>>();
foreach(KeyValuePair<int, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<KeyValuePair<int, EquippableItemPrefabAsset>>)DataLoader.LoadData(SceneElementTypeEnum.None))
foreach(IndexValuePair<int, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<IndexValuePair<int, EquippableItemPrefabAsset>>)DataLoader.LoadData(SceneElementTypeEnum.None))
{
convertedList.Add(new KeyValuePair<int, Item>(loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
convertedList.Add(new IndexValuePair<int, EquippableItem>(loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
}
// 2. Pass loaded list to InventoryDataManager
@ -62,22 +81,22 @@ public class SceneInventoryDataManager : SceneBaseDataManager<KeyValuePair<int,
return false;
}
protected override bool SaveData(List<KeyValuePair<int, Item>> _elements, SceneElementTypeEnum type)
protected override bool SaveData(List<IndexValuePair<int, EquippableItem>> _elements, SceneElementTypeEnum type)
{
try
{
// 1. Convert EquippableItem to EquippableItemPrefabAsset list
List<KeyValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<KeyValuePair<int, EquippableItemPrefabAsset>>();
List<IndexValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
foreach (KeyValuePair<int, Item> itemElement in _elements)
foreach (IndexValuePair<int, EquippableItem> itemElement in _elements)
{
convertedList.Add(new KeyValuePair<int, EquippableItemPrefabAsset>(
convertedList.Add(new IndexValuePair<int, EquippableItemPrefabAsset>(
itemElement.Key,
new EquippableItemPrefabAsset(
itemElement.Value.Name,
itemElement.Value.ItemModel.name,
new Vector3(0, 0, 0),
(EquippableItem)itemElement.Value
itemElement.Value
)
));
}
@ -96,12 +115,12 @@ public class SceneInventoryDataManager : SceneBaseDataManager<KeyValuePair<int,
}
#endregion
protected SceneBaseDataManager<KeyValuePair<int, Item>> GetObjectType()
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
{
return GameObject.FindObjectOfType<SceneInventoryDataManager>();
}
protected SceneBaseDataManager<KeyValuePair<int, Item>> CreateInstance(ref GameObject managerGameObject)
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<SceneInventoryDataManager>();
}

View File

@ -18,13 +18,16 @@ public abstract class SceneBaseDataManager<T> : MonoBehaviour
public virtual void Awake()
{
if (Instance == null)
{
Debug.Log("Create: "+gameObject);
Instance = this;
}
else
{
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
}
}
@ -74,8 +77,6 @@ public abstract class SceneBaseDataManager<T> : MonoBehaviour
{
try
{
Debug.Log("SaveData");
Debug.Log(DataLoader);
DataLoader.SaveData(_elements, type);
return true;
@ -138,17 +139,16 @@ public abstract class SceneBaseDataManager<T> : MonoBehaviour
DynamicDataList.RemoveElementFromList(_element);
}
public bool SaveDynamicData()
public virtual bool SaveDynamicData()
{
// TODO
// how to get list
// 1. List in UI manager should be synchronized with list in this manager
// approach:
// 1. get from outside, update local list && us it
// 2. Handle list synchronized all the time & pass local list
Debug.Log("SaveDynamicData");
return SaveData(DynamicDataList.GetList(), SceneElementTypeEnum.Dynamic);
}
}

View File

@ -3,7 +3,6 @@ using UnityEngine;
public interface ManagerInterface<T>
{
//public void SetPanelController(ref PanelController<T> dynamicPanelController);
public bool OpenPanel();
public bool ClosePanel();
@ -16,7 +15,4 @@ public interface ManagerInterface<T>
public void Add(T model);
public void UpdateList();
public void Remove();
/* public T Find();
*/
}

View File

@ -4,7 +4,7 @@ using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public abstract class DraggablePanelController : PanelController<KeyValuePair<int, Item>>, DraggablePanelInterface
public abstract class DraggablePanelController : PanelController<IndexValuePair<int, EquippableItem>>, DraggablePanelInterface
{
[SerializeField] protected new List<ISlot> ChildBoxList = new List<ISlot>();
@ -51,13 +51,12 @@ public abstract class DraggablePanelController : PanelController<KeyValuePair<in
DraggedSlotController.Instance.RemoveDraggedSlot();
// Rebuild/apply UiManager content (list of items) base on slots values after its updating
for (int i = 0; i < ChildBoxList.Count; i++)
{
if (ChildBoxList[i].Item != null)
{
UiManager.Add(new KeyValuePair<int, Item>(i, ChildBoxList[i].Item));
UiManager.Add(new IndexValuePair<int, EquippableItem>(i, ChildBoxList[i].Item));
}
else
{
@ -82,9 +81,9 @@ public abstract class DraggablePanelController : PanelController<KeyValuePair<in
//draggedSlot.Item = (EquippableItem)new Item("test", "test desc", 1, null, null);
Item tmpDraggedItem = draggedSlot.Item == null ? null : new EquippableItem(draggedSlot.Item); // InventoryUIManager.Instance.DraggedSlot.Item; // remember temporary currently dragged item
UiManager.Add(new KeyValuePair<int, Item>(dropItemSlot.Number, tmpDraggedItem));
EquippableItem tmpDraggedItem = draggedSlot.Item == null ? null : new EquippableItem(draggedSlot.Item); // InventoryUIManager.Instance.DraggedSlot.Item; // remember temporary currently dragged item
UiManager.Add(new IndexValuePair<int, EquippableItem>(dropItemSlot.Number, tmpDraggedItem));
Debug.Log(UiManager);
// !!! remember item from dropped position to future operation !!!
if (dropItem)
DraggedSlotController.Instance.UpdateItem(new EquippableItem(dropItem));

View File

@ -46,7 +46,7 @@ public class DraggedSlotController : MonoBehaviour
MakeDraggableItem(beginingPosition);
}
private void MakeDraggableItem( Vector3 beginingPosition)
private void MakeDraggableItem(Vector3 beginingPosition)
{
if (DraggedSlotImage)
{

View File

@ -23,17 +23,16 @@ public abstract class WarehousePanelController : DraggablePanelController
// 2. Rebuild/apply UiManager content (list of items) base on slots values after its updating
// 2.1 Make copy because Add -> base.Add() -> base.Add() -> UpdateList() rebuild content ad pass "new" list (in this case with only one - firtst passed - item) to ChestContentUiMangaer.Element
var ChildBoxListCopy = new List<KeyValuePair<int, Item>>(ChildBoxList.Where(slot => slot.Item != null).Select(slot => new KeyValuePair<int, Item>(slot.Number, slot.Item)).ToList());
var ChildBoxListCopy = new List<IndexValuePair<int, EquippableItem>>(ChildBoxList.Where(slot => slot.Item != null).Select(slot => new IndexValuePair<int, EquippableItem>(slot.Number, slot.Item)).ToList());
UiManager.RemoveAll();
foreach(KeyValuePair<int, Item> slot in ChildBoxListCopy)
foreach(IndexValuePair<int, EquippableItem> slot in ChildBoxListCopy)
{
if (slot.Value != null)
{
Debug.Log($"Slot nr: {slot.Key} with item: {slot.Value}");
UiManager.Add(new KeyValuePair<int, Item>(slot.Key, slot.Value));
UiManager.Add(new IndexValuePair<int, EquippableItem>(slot.Key, slot.Value));
}
else
{
@ -42,7 +41,7 @@ public abstract class WarehousePanelController : DraggablePanelController
}
}
public override void BuildPanelContent(List<KeyValuePair<int, Item>> elements)
public override void BuildPanelContent(List<IndexValuePair<int, EquippableItem>> elements)
{
base.BuildPanelContent(elements);

View File

@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public abstract class UISlotPanelManager<T> : UIBaseManager<T>
{
public virtual int SLOTS_NUMBER => 0;
/// <summary>
/// Function to find item in warehouser by its id, returns list of all slots where item occured
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public abstract List<T> FindItemInWarehouse(int itemId);
/// <summary>
/// Function to find item in warehouser by its name, returns list of all slots where item occured
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public abstract List<T> FindItemInWarehouseByName(string itemName);
public abstract void RemoveByPosition(int keyPosition);
public abstract int RemoveByItemId(int itemId);
public abstract int RemoveByItemName(string itemName);
public virtual void RemoveAll()
{
Elements.Clear();
}
public bool IsFull()
{
return Elements.Count() == SLOTS_NUMBER;
}
protected bool CheckIfSlotIsInRange(int slotNumber)
{
return slotNumber < SLOTS_NUMBER;
}
protected abstract bool CheckIfSlotExists(int slotNumber);
/// <summary>
/// Check if slot is empty in local list
/// </summary>
/// <returns></returns>
protected abstract bool CheckIfPositionIsEmpty(int slotNumber);
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fa3cacb98d47401d9f5e909d2d59d40e
timeCreated: 1669072060

View File

@ -3,9 +3,9 @@ using System.Linq;
using UnityEditor;
using UnityEngine;
public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>>
public abstract class UIWarehouseManager : UISlotPanelManager<IndexValuePair<int, EquippableItem>>
{
public virtual int SLOTS_NUMBER => 0;
public override int SLOTS_NUMBER => 48;
public static new UIWarehouseManager Instance { get; protected set; }
/*
@ -17,7 +17,7 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public List<KeyValuePair<int, Item>> FindItemInWarehouse(int itemId)
public override List<IndexValuePair<int, EquippableItem>> FindItemInWarehouse(int itemId)
{
return Elements.Where(item => item.Value.Id == itemId).ToList();
}
@ -27,7 +27,7 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public List<KeyValuePair<int, Item>> FindItemInWarehouseByName(string itemName)
public override List<IndexValuePair<int, EquippableItem>> FindItemInWarehouseByName(string itemName)
{
return Elements.Where(item => item.Value.name == itemName).ToList();
}
@ -38,9 +38,8 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
/// Function (SetItemOnPosition) to add slot with its number and item to list (which will be mapped with panel content)
/// </summary>
/// <param name="itemOnSlot"></param>
public virtual void Add(KeyValuePair<int, Item> itemOnSlot)
public virtual void Add(IndexValuePair<int, EquippableItem> itemOnSlot)
{
if (!CheckIfSlotIsInRange(itemOnSlot.Key))
throw new System.Exception($"Slot number: {itemOnSlot.Key} is out of range, avaiable: {SLOTS_NUMBER} slots");
@ -57,7 +56,7 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
/// Function to add item on first empty slot to list (which will be mapped with panel content)
/// </summary>
/// <param name="item"></param>
public virtual void Add(Item item)
public virtual void Add(EquippableItem item)
{
if(IsFull())
throw new System.Exception($"Warehouse is full!!!");
@ -65,12 +64,11 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
// find first empty position / slot
var max = Elements.Max(itemSlot => itemSlot.Key);
base.Add(new KeyValuePair<int, Item>(max, item));
base.Add(new IndexValuePair<int, EquippableItem>(max, item));
}
public virtual void RemoveByPosition(int keyPosition)
public override void RemoveByPosition(int keyPosition)
{
if (!CheckIfSlotExists(keyPosition))
return; // throw new System.Exception($"Slot with number: {keyPosition} don't exist");
@ -80,36 +78,17 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
Elements.RemoveAll(itemSlot => itemSlot.Key == keyPosition);
}
public virtual int RemoveByItemId(int itemId)
public override int RemoveByItemId(int itemId)
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Id == itemId);
}
public virtual int RemoveByItemName(string itemName)
public override int RemoveByItemName(string itemName)
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Name == itemName);
}
public virtual void RemoveAll()
{
Elements.Clear();
}
public bool IsFull()
{
return Elements.Count() == SLOTS_NUMBER;
}
private bool CheckIfSlotIsInRange(int slotNumber)
{
return slotNumber < SLOTS_NUMBER;
}
private bool CheckIfSlotExists(int slotNumber)
protected override bool CheckIfSlotExists(int slotNumber)
{
return Elements.Any(itemSlot => itemSlot.Key == slotNumber);
}
@ -118,7 +97,7 @@ public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>
/// Check if slot is empty in local list
/// </summary>
/// <returns></returns>
private bool CheckIfPositionIsEmpty(int slotNumber)
protected override bool CheckIfPositionIsEmpty(int slotNumber)
{
if(CheckIfSlotExists(slotNumber)) {
return Elements.Where(itemSlot => itemSlot.Key == slotNumber).First().Value == null;

View File

@ -47,7 +47,7 @@ public class ChestContentUIManager : UIWarehouseManager
public override void SetupPanel()
{
base.SetupPanel();
Debug.Log(Elements);
// setup models list
DynamicPanel.GetComponent<ChestPanelController>().SetUp(Elements);
}
@ -58,14 +58,14 @@ public class ChestContentUIManager : UIWarehouseManager
}
#region adust parent function
public override void Add(KeyValuePair<int, Item> itemOnSlot)
public override void Add(IndexValuePair<int, EquippableItem> itemOnSlot)
{
base.Add(itemOnSlot);
UpdateChestContent();
}
public override void Add(Item item)
public override void Add(EquippableItem item)
{
base.Add(item);

View File

@ -22,8 +22,6 @@ public class ChestUIManager : UIBaseManager<Chest>
}
else
{
Debug.Log(gameObject);
Debug.Log(Instance);
Destroy(gameObject);
}
}
@ -82,7 +80,7 @@ public class ChestUIManager : UIBaseManager<Chest>
//DynamicPanel.GetComponent<ChestPanelController>().BuildPanelContent(chest.GetContent());
}
public void UpdateChestContent(string chestName, List<KeyValuePair<int, Item>> chestContent)
public void UpdateChestContent(string chestName, List<IndexValuePair<int, EquippableItem>> chestContent)
{
Elements.Where(chest => chest.name == chestName).ToList().ForEach(chest => chest.SetContent(chestContent));
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9477ac7958629984098c425d3a3a64fe
guid: 153d18ca7804a58499afd259c70a2108
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class EquipmentUIManager : UIWarehouseManager
{
public override int SLOTS_NUMBER => 9;
public static new EquipmentUIManager Instance { get; protected set; }
public const string ITEM_LOCALIZATION = "UiPanels/";
public const string PANEL_NAME = "EquipmentPanel";
public void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public override void SetupPanel()
{
base.SetupPanel();
// setup models list
DynamicPanel.GetComponent<EquipmentPanelController>().SetUp(Elements);
}
public override void UpdateList()
{
DynamicPanel.GetComponent<EquipmentPanelController>().BuildPanelContent(Elements);
}
protected override GameObject GetTemplatePanel()
{
// Resources = default path - Asset/Resources ... .obj
return Resources.Load(ITEM_LOCALIZATION + PANEL_NAME) as GameObject;
}
#region Override list functions
/// <summary>
/// Function (SetItemOnPosition) to add slot with its number and item to list (which will be mapped with panel content)
/// </summary>
/// <param name="itemOnSlot"></param>
public override void Add(IndexValuePair<int, EquippableItem> itemOnSlot)
{
Debug.Log(itemOnSlot.Value);
if (!CheckIfSlotIsInRange(itemOnSlot.Key))
throw new System.Exception($"Slot number: {itemOnSlot.Key} is out of range, avaiable: {SLOTS_NUMBER} slots");
if (CheckIfSlotExists(itemOnSlot.Key) && !CheckIfPositionIsEmpty(itemOnSlot.Key))
RemoveByPosition(itemOnSlot.Key);
if (itemOnSlot.Value != null)
{
Elements.RemoveAll(equipment => equipment.Key == itemOnSlot.Key);
Elements.Add(new IndexValuePair<int, EquippableItem>(itemOnSlot.Key, itemOnSlot.Value));
UpdateList();
}
}
public override void RemoveByPosition(int keyPosition)
{
if (!CheckIfSlotExists(keyPosition))
return; // throw new System.Exception($"Slot with number: {keyPosition} don't exist");
Debug.Log($"Remove from position: {keyPosition}");
Elements.RemoveAll(equipment => equipment.Key == keyPosition);
Elements.Add(new IndexValuePair<int, EquippableItem>(keyPosition, null));
}
#endregion
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e09bf4772fa4f3042a7b2d2e04051247
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,7 +8,7 @@ using System;
public class ChestPanelController : WarehousePanelController
{
protected override UIBaseManager<KeyValuePair<int, Item>> FetchUiManager()
protected override UIBaseManager<IndexValuePair<int, EquippableItem>> FetchUiManager()
{
return GameObject.FindObjectOfType<ChestContentUIManager>();
}
@ -61,7 +61,7 @@ public class ChestPanelController : WarehousePanelController
// 2. Set up panel additn items to it
public override void SetUp(List<KeyValuePair<int, Item>> elements)
public override void SetUp(List<IndexValuePair<int, EquippableItem>> elements)
{
// Build panel content template
BuildPanelSlots();
@ -71,15 +71,15 @@ public class ChestPanelController : WarehousePanelController
}
public override void BuildPanelContent(List<KeyValuePair<int, Item>> elements)
public override void BuildPanelContent(List<IndexValuePair<int, EquippableItem>> elements)
{
base.BuildPanelContent(elements);
Debug.Log("Build content");
foreach (KeyValuePair<int, Item> element in elements)
foreach (IndexValuePair<int, EquippableItem> element in elements)
{
Debug.Log($"key: {element.Key} - value: {element.Value}");
Debug.Log($"key: {element.Key} - value: {element.Value} - type: {element.Value.EquipmentType}");
ChildBoxList[element.Key].SetItem(new EquippableItem(element.Value));
}
}

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class EquipmentPanelController : DraggablePanelController
{
[SerializeField] protected GameObject _panelAdditionalSlotsContent;
protected override UIBaseManager<IndexValuePair<int, EquippableItem>> FetchUiManager()
{
return GameObject.FindObjectOfType<EquipmentUIManager>();
}
public override void BuildPanelSlots()
{
InitSlotsList();
}
public void InitSlotsList()
{
ChildBoxList.Add(SetupDragAndDropToSlot(_panelContent.transform.Find("equipment_slot_helmet").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelContent.transform.Find("equipment_slot_armor").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelContent.transform.Find("equipment_slot_weapon").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelContent.transform.Find("equipment_slot_boots").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelAdditionalSlotsContent.transform.Find("equipment_slot_ring").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelAdditionalSlotsContent.transform.Find("equipment_slot_bracelet").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelAdditionalSlotsContent.transform.Find("equipment_slot_necklet").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelAdditionalSlotsContent.transform.Find("equipment_slot_potion_i").GetComponent<EquipmentSlot>()));
ChildBoxList.Add(SetupDragAndDropToSlot(_panelAdditionalSlotsContent.transform.Find("equipment_slot_potion_ii").GetComponent<EquipmentSlot>()));
}
public override GameObject BuildSlot(int key, GameObject _parent) { throw new NotImplementedException(); }
// 2. Set up panel additn items to it
//public void SetUp - in parent
public override void BuildPanelContent(List<IndexValuePair<int, EquippableItem>> elements)
{
base.BuildPanelContent(elements);
Debug.Log("Build content");
foreach (IndexValuePair<int, EquippableItem> element in elements)
{
Debug.Log($"key: {element.Key} - value: {element.Value}");
if(element.Value != null)
ChildBoxList.Where(slot => ((EquipmentSlot)slot).type == (EquipmentPanelSlotsTypeEnum)element.Key).ToList().ForEach(slot => slot.SetItem((EquippableItem)element.Value));
}
}
public override void ClearSlots()
{
foreach (ItemSlot ChestSlot in ChildBoxList)
{
ChestSlot.ResetSlot();
}
}
public virtual void EndDrag(ItemSlot itemSlot)
{
if (!DraggedSlotController.Instance.IsDragged()) // if there was nothing dragged - ignore event
return;
DraggedSlotController.Instance.RemoveDraggedSlot();
Debug.Log(this);
// Rebuild/apply UiManager content (list of items) base on slots values after its updating
for (int i = 0; i < ChildBoxList.Count; i++)
{
if (ChildBoxList[i].Item != null)
{
UiManager.Add(new IndexValuePair<int, EquippableItem>(i, ChildBoxList[i].Item));
}
else
{
UiManager.RemoveByPosition(i);
}
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 42863f9f48506b944b90213e49c99763
guid: e143aeb4404531f4aadd27d560e12565
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -8,7 +8,7 @@ using System;
public class InventoryPanelController : WarehousePanelController
{
protected override UIBaseManager<KeyValuePair<int, Item>> FetchUiManager()
protected override UIBaseManager<IndexValuePair<int, EquippableItem>> FetchUiManager()
{
var uiManager = GameObject.FindObjectOfType<InventoryUIManager>();
@ -66,7 +66,7 @@ public class InventoryPanelController : WarehousePanelController
// 2. Set up panel additn items to it
public override void SetUp(List<KeyValuePair<int, Item>> elements)
public override void SetUp(List<IndexValuePair<int, EquippableItem>> elements)
{
// Build panel content template
BuildPanelSlots();
@ -76,16 +76,16 @@ public class InventoryPanelController : WarehousePanelController
}
public override void BuildPanelContent(List<KeyValuePair<int, Item>> elements)
public override void BuildPanelContent(List<IndexValuePair<int, EquippableItem>> elements)
{
base.BuildPanelContent(elements);
Debug.Log("Build content");
foreach (KeyValuePair<int, Item> element in elements)
foreach (IndexValuePair<int, EquippableItem> element in elements)
{
Debug.Log($"key: {element.Key} - value: {element.Value}");
ChildBoxList[element.Key].SetItem((EquippableItem)element.Value);
ChildBoxList[element.Key].SetItem(element.Value);
}
}
}

View File

@ -0,0 +1,13 @@
[System.Serializable]
public enum EquipmentPanelSlotsTypeEnum
{
HelmetSlot,
ArmorSlot,
WeaponSlot,
BootsSlot,
RingSlot,
BraceletSlot,
NeckletSlot,
PotionSlotSt,
PotionSlotNd
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30d5a385ac41da54986a749a11874525
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,6 +1,7 @@
[System.Serializable]
public enum EquipmentTypeEnum
{
None, // IMPORTANT after cast from Item to DefaultItem it gets defaulty first type so...
Helmet,
Chest,
Gloves,

View File

@ -16,6 +16,19 @@ public struct IndexValuePair<T>
}
}
[System.Serializable]
public struct IndexValuePair<V, T>
{
[SerializeField] public V Key;
[SerializeField] public T Value;
public IndexValuePair(V key, T value)
{
Key = key;
Value = value;
}
}
[System.Serializable]
[CreateAssetMenu(fileName = "New Chest", menuName = "Inventory/Chest")]
public class Chest
@ -50,7 +63,7 @@ public class Chest
public ChestTypeEnum ChestType;
public List<IndexValuePair<Item>> Content = new List<IndexValuePair<Item>>();
public List<IndexValuePair<EquippableItem>> Content = new List<IndexValuePair<EquippableItem>>();
public Chest() { }
@ -72,7 +85,7 @@ public class Chest
this.ChestType = _type;
}
public Chest(string _name, string _description, GameObject _chestModel, ChestTypeEnum _type, List<KeyValuePair<int, Item>> _content)
public Chest(string _name, string _description, GameObject _chestModel, ChestTypeEnum _type, List<IndexValuePair<int, EquippableItem>> _content)
{
this.Name = _name;
this.Description = _description;
@ -82,23 +95,23 @@ public class Chest
SetContent(_content);
}
public void SetContent(List<KeyValuePair<int, Item>> _content)
public void SetContent(List<IndexValuePair<int, EquippableItem>> _content)
{
Content.Clear();
foreach (KeyValuePair<int, Item> element in _content)
foreach (IndexValuePair<int, EquippableItem> element in _content)
{
Content.Add(new IndexValuePair<Item>(element.Key, element.Value));
Content.Add(new IndexValuePair<EquippableItem>(element.Key, element.Value));
}
}
public List<KeyValuePair<int, Item>> GetContent()
public List<IndexValuePair<int, EquippableItem>> GetContent()
{
List<KeyValuePair<int, Item>> castedContent = new List<KeyValuePair<int, Item>>();
List<IndexValuePair<int, EquippableItem>> castedContent = new List<IndexValuePair<int, EquippableItem>>();
foreach (IndexValuePair<Item> element in Content)
foreach (IndexValuePair<EquippableItem> element in Content)
{
castedContent.Add(new KeyValuePair<int, Item>(element.Key, element.Value));
castedContent.Add(new IndexValuePair<int, EquippableItem>(element.Key, element.Value));
}
return castedContent;

View File

@ -22,7 +22,7 @@ public class ChestData : ModelData<Chest, GameObject>
{
description = chest.description;
foreach(IndexValuePair<Item> item in chest.Content)
foreach(IndexValuePair<EquippableItem> item in chest.Content)
{
content.Add(
new IndexValuePair<EquippableItemData>(item.Key, new EquippableItemData(item.Value))
@ -42,8 +42,15 @@ public class ChestData : ModelData<Chest, GameObject>
chest.Content.Clear();
foreach (IndexValuePair<EquippableItemData> item in content)
{
var castedObject = item.Value.MapDataToObject() as EquippableItem;
if(castedObject == null)
chest.Content.Add(
new IndexValuePair<Item>(item.Key, item.Value.MapDataToObject())
new IndexValuePair<EquippableItem>(item.Key, new EquippableItem(item.Value.MapDataToObject()))
);
else
chest.Content.Add(
new IndexValuePair<EquippableItem>(item.Key, castedObject)
);
}

View File

@ -9,7 +9,7 @@ public class ChestPrefabAsset : PrefabAssetModel
[SerializeField]
public Chest Chest = null;
//public List<KeyValuePair<int, EquippableItemPrefabAsset>> Content { get; set; }
//public List<IndexValuePair<int, EquippableItemPrefabAsset>> Content { get; set; }
public ChestPrefabAsset(Chest _chest)
: base(_chest.name, _chest.ChestModel.name, new Vector3(0,0,0))

View File

@ -5,17 +5,17 @@ using UnityEngine;
public class ChestPrefabAssetData : PrefabAssetModelData
{
[SerializeField] ChestData ChestData;
//public List<KeyValuePair<int, EquippableItemPrefabAssetData>> Content { get; set; }
//public List<IndexValuePair<int, EquippableItemPrefabAssetData>> Content { get; set; }
public ChestPrefabAssetData(ChestPrefabAsset assetModel) : base(assetModel)
{
ChestData = MapModelToData(assetModel.Chest);
/* Content = new List<KeyValuePair<int, EquippableItemPrefabAssetData>>();
/* Content = new List<IndexValuePair<int, EquippableItemPrefabAssetData>>();
Debug.Log("ChestPrefarbAsset to ChestPrefarbAssetData - " + assetModel.Name);
foreach(KeyValuePair<int, EquippableItemPrefabAsset> itemEntry in assetModel.Content)
foreach(IndexValuePair<int, EquippableItemPrefabAsset> itemEntry in assetModel.Content)
{
Debug.Log("Slot nr " + itemEntry.Key);
Content[itemEntry.Key] = itemEntry.Value.MapPrefabAssetModelToData();
@ -42,8 +42,8 @@ public class ChestPrefabAssetData : PrefabAssetModelData
);
// todo convert each item to data
Debug.Log(ChestData);
chestPrefabAsset.Chest = ChestData.MapDataToObject();
// clear content and set items from data - this will modify Scri-0ptableObject data also in source
//chestPrefabAsset.Chest.Content.Clear();
/* foreach (IndexValuePair<EquippableItemData> modelData in ChestData.content)

View File

@ -15,11 +15,11 @@ public class EquippableItem : Item
public bool isStackable = false;
[Space]
public EquipmentTypeEnum EquipmentType;
public EquipmentTypeEnum EquipmentType = EquipmentTypeEnum.None;
public EquippableItem(){}
public EquippableItem(Item _item) : base(_item){}
public EquippableItem(Item _item) : base(_item) {}
public EquippableItem(EquippableItem _item) : base(_item.name, _item.description, _item.level, _item.itemModel, _item.image)
{
@ -27,6 +27,8 @@ public class EquippableItem : Item
AgilityBonus = _item.AgilityBonus;
InteligenceBonus = _item.InteligenceBonus;
VitalityBonus = _item.VitalityBonus;
EquipmentType = _item.EquipmentType;
}
public EquippableItem(string _name, string _description, int _level, GameObject _itemModel, Sprite _image) : base(_name, _description, _level, _itemModel, _image) { }

View File

@ -1,3 +1,4 @@
using System;
using UnityEngine;
[System.Serializable]

View File

@ -1,3 +1,4 @@
using System;
using UnityEngine;
@ -57,7 +58,7 @@ public abstract class ItemData : ModelData<Item, Item>
protected override Item TryFindResource(string modelName)
{
var resource = Resources.Load<Item>(MODEL_LOCALIZATION + modelName);
Debug.Log(MODEL_LOCALIZATION + modelName);
if (!resource)
throw new System.Exception($"Resource {modelName} not found!!");

View File

@ -2,8 +2,12 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class EquipmentSlot : ItemSlot
{
[SerializeField] public EquipmentPanelSlotsTypeEnum type;
public EquipmentTypeEnum EquipmentType;
public override bool CanReceiveItem(Item item)
@ -12,6 +16,7 @@ public class EquipmentSlot : ItemSlot
return true;
EquippableItem equippableItem = item as EquippableItem; // jeśli item pierwotnie nie był typu EquippableItem to casting zwróci NULL
return equippableItem != null && equippableItem.EquipmentType == EquipmentType; // the second condition is to put items on fields with matched type
return equippableItem != null && equippableItem.EquipmentType != EquipmentTypeEnum.None && equippableItem.EquipmentType == EquipmentType; // the second condition is to put items on fields with matched type
}
}

View File

@ -19,9 +19,10 @@ public class SaveController : MonoBehaviour
PlayerPrefs.SetInt("continued", continued);
}
public void SaveItems()
public void SaveItems() // toDO change name to save Equipment - save items will save items on map eg after drops
{
SceneEquippableItemManager.Instance.SaveEquippableItems();
//SceneEquippableItemManager.Instance.SaveEquippableItems();
EquipmentDataManager.Instance.SaveDynamicData();
}
public void SaveQuests()
@ -38,6 +39,6 @@ public class SaveController : MonoBehaviour
public void SaveChests()
{
SceneChestManager.Instance.SaveChests();
SceneChestDataManager.Instance.SaveDynamicData();
}
}

View File

@ -9,7 +9,7 @@ namespace GUI_Scripts
public void manageWindow()
{
if(!InventoryUIManager.Instance.GetPanelStatus() && !EquipmentManager.Instance.isOpen)
if(!InventoryUIManager.Instance.GetPanelStatus() && !EquipmentUIManager.Instance.GetPanelStatus())
this.OpenPlayerPanel();
else
this.ClosePlayerPanel();
@ -17,19 +17,19 @@ namespace GUI_Scripts
}
public void OpenPlayerPanel() {
if(!InventoryUIManager.Instance.GetPanelStatus() || !EquipmentManager.Instance.isOpen)
if(!InventoryUIManager.Instance.GetPanelStatus() || !EquipmentUIManager.Instance.GetPanelStatus())
{
InventoryUIManager.Instance.OpenPanel();
EquipmentManager.Instance.OpenPanel();
EquipmentUIManager.Instance.OpenPanel();
}
}
public void ClosePlayerPanel() {
if(InventoryUIManager.Instance.GetPanelStatus() || EquipmentManager.Instance.isOpen)
if(InventoryUIManager.Instance.GetPanelStatus() || EquipmentUIManager.Instance.GetPanelStatus())
{
InventoryUIManager.Instance.ClosePanel();
EquipmentManager.Instance.ClosePanel();
EquipmentUIManager.Instance.ClosePanel();
}
}

View File

@ -1,222 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
abstract public class BasePanelController : MonoBehaviour
{
[Header("Panel Information")]
[SerializeField] protected GameObject _panel;
[SerializeField] protected GameObject _blankSlot;
[SerializeField] protected Button _panelCloseButton;
[Header("Object Informations")]
[SerializeField] protected GameObject _instance;
// SYF
[Header("Dragged Informations")]
[SerializeField] protected Image _itemTemplate;
[SerializeField] protected Image _tmp;
[Header("Slots List")]
[SerializeField] public int MAX_SLOT_CUNT = 6 * 8;
[SerializeField] public List<ISlot> _itemSlots = new List<ISlot>();
public void Awake()
{
_instance = gameObject;
this.InitPanelSlots();
}
void Start()
{
if(_panelCloseButton)
{
_panelCloseButton.onClick.AddListener(CloseOnClick);
}
}
public abstract void CloseOnClick();
#region Main logic
protected abstract ISlot SetupSlot(int key, GameObject _parent);
public void SetupDragAndDropToSlot(ISlot slot)
{
slot.OnBeginDragEvent += BeginDrag;
slot.OnEndDragEvent += EndDrag;
slot.OnDragEvent += Drag;
slot.OnDropEvent += Drop;
}
protected void InitPanelSlots()
{
if(_panel)
{
for(int i = 0; i < MAX_SLOT_CUNT; i++)
{
ISlot newSlot = SetupSlot(i, _panel);
// Set new Slot instance
_itemSlots.Add(newSlot);
// Assign events
this.SetupDragAndDropToSlot(_itemSlots[i]);
}
}
}
protected void MakeDraggableItem(ItemSlot itemSlot)
{
if(_tmp)
{
_tmp.enabled = true;
return;
}
GameObject globalGUI = GameObject.FindGameObjectsWithTag("GUI")[0];
if(globalGUI)
{
_tmp = Instantiate(_itemTemplate, _itemTemplate.transform.position, Quaternion.identity, globalGUI.transform);
_tmp.transform.localPosition = _panel.transform.position;
_tmp.sprite = itemSlot.Item.Image;
_tmp.transform.position = Input.mousePosition;
_tmp.enabled = true;
} else {
Debug.Log("Can't find global GUI object!!!");
}
}
#endregion
#region setup panel with content
public void Setup(Dictionary<int, Item> _items)
{
SetPanelItems(_items);
}
private void SetPanelItems(Dictionary<int, Item> _items)
{
foreach(int key in _items.Keys)
{
_itemSlots[key].SetItem(_items[key]);
}
}
#endregion
#region base panel operations
public bool AddItem(Item _item)
{
for(int i = 0; i < _itemSlots.Count; i++)
{
if(_itemSlots[i].Item == null)
{
_itemSlots[i].Item = _item;
return true;
}
}
return false;
}
public bool RemoveItem(Item _item)
{
for(int i = 0; i < _itemSlots.Count; i++)
{
if(_itemSlots[i].Item == _item)
{
_itemSlots[i].Item = null;
return true;
}
}
return false;
}
public bool IsFull()
{
for(int i = 0; i < _itemSlots.Count; i++)
{
if(_itemSlots[i].Item == null)
{
return false;
}
}
return true;
}
#endregion
#region Drag & Drop
public void BeginDrag(ItemSlot itemSlot)
{
if (itemSlot.Item != null)
{
InventoryManager.Instance.DraggedSlot = itemSlot;
MakeDraggableItem(itemSlot);
}
}
public void EndDrag(ItemSlot itemSlot)
{
InventoryManager.Instance.DraggedSlot = null;
DestroyImmediate(_tmp.gameObject, true);
// Apply list of items
// Update Object surce items list based on slots state and configuration
for (int i = 0; i < _itemSlots.Count; i++)
{
if (_itemSlots[i].Item != null)
{
_instance.GetComponent<BaseWarehouseController>().SetItemOnPosition(i, _itemSlots[i].Item);
}
else
{
_instance.GetComponent<BaseWarehouseController>().RemoveItemFromPosition(i);
}
}
}
public void Drag(ItemSlot itemSlot)
{
_tmp.transform.position = Input.mousePosition;
}
public void Drop(ItemSlot dropItemSlot)
{
if (dropItemSlot.CanReceiveItem(InventoryManager.Instance.DraggedSlot.Item) && InventoryManager.Instance.DraggedSlot.CanReceiveItem(dropItemSlot.Item))
{
EquippableItem dragItem = InventoryManager.Instance.DraggedSlot.Item as EquippableItem;
EquippableItem dropItem = dropItemSlot.Item as EquippableItem;
// for changing chest to evuuipment or onventory panel !!!!
// if(draggedSlot is EquipmentSlot)
// {
// if(dragItem != null) dragItem.Unequip(this);
// if(dropItem != null) dropItem.Equip(this);
// }
// if(dropItemSlot is EquipmentSlot)
// {
// if(dragItem != null) dragItem.Equip(this);
// if(dropItem != null) dropItem.Unequip(this);
// }
Item draggedItem = InventoryManager.Instance.DraggedSlot.Item; // remember temporary currently dragged item
InventoryManager.Instance.DraggedSlot.Item = dropItemSlot.Item;
dropItemSlot.Item = draggedItem;
_instance.GetComponent<BaseWarehouseController>().SetItemOnPosition(InventoryManager.Instance.DraggedSlot.Number, InventoryManager.Instance.DraggedSlot.Item);
_instance.GetComponent<BaseWarehouseController>().SetItemOnPosition(dropItemSlot.Number, dropItemSlot.Item);
}
}
#endregion
}

View File

@ -1,59 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// T will be type panel: inventory, equipment, chest
abstract public class BaseWarehouseController : MonoBehaviour
{
[SerializeField] public GameObject _panel;
[SerializeField] public Dictionary<int, Item> _items = new Dictionary<int, Item>();
protected GameObject dynamicPanel;
public bool isOpen = false;
// Create panel instance on scene on special position and passed content (item list)
public void OpenPanel()
{
GameObject globalGUI = GameObject.FindGameObjectsWithTag("GUI")[0];
if(globalGUI)
{
this.dynamicPanel = Instantiate(_panel, _panel.transform.position, Quaternion.identity, globalGUI.transform); // 4'th arg allow set object as child
this.dynamicPanel.transform.localPosition = _panel.transform.position; // prevent overwritten position by... environment???
this.SetupPanel(); // bind pandel to current chest
isOpen = true;
} else {
Debug.Log("Can't find global GUI object!!!");
}
}
public virtual void ClosePanel()
{
Destroy(dynamicPanel);
isOpen = false;
}
protected abstract void SetupPanel();
public virtual void SetItemOnPosition(int _keyPosition, Item _item)
{
// Drag setup first end second object on the same panel - should only one - this wwere dropped item
// Becouse of this source slot from other panel is settuping in dropped panel too - list get empty item and it cause errors
if(_item != null)
this._items[_keyPosition] = _item;
// if we assign null its removed later in EndDrag method
}
public virtual void RemoveItemFromPosition(int _keyPosition)
{
this._items.Remove(_keyPosition);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 967250d8d9011314e8c6d1e58b524211
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: