Scriptum/Assets/Scripts/REFACTORING/Models/Item/EquippableItem/EquippableItemPrefabAssetData.cs

61 lines
2.2 KiB
C#
Raw Permalink Normal View History

2022-11-06 21:34:17 +01:00
using UnityEngine;
[System.Serializable]
public class EquippableItemPrefabAssetData : PrefabAssetModelData
{
public EquippableItemData EquippableItemData;
2022-11-06 21:34:17 +01:00
public EquippableItemPrefabAssetData(EquippableItemPrefabAsset assetModel) : base(assetModel)
{
EquippableItemData = MapModelToData(assetModel.EquippableItem);
2022-11-06 21:34:17 +01:00
}
/// <summary>
/// Function to convert equitable item (current object partial variable) object to data format
/// </summary>
/// <param name="equippableItem"></param>
/// <returns></returns>
public EquippableItemData MapModelToData(EquippableItem equippableItem)
2022-11-06 21:34:17 +01:00
{
return new EquippableItemData(equippableItem);
}
/// <summary>
/// Function to convert EquippableItem Asset format to data format
/// </summary>
/// <param name="assetModel"></param>
/// <returns></returns>
public override PrefabAssetModelData PrefabAssetModelToData(PrefabAssetModel assetModel)
{
return new EquippableItemPrefabAssetData((EquippableItemPrefabAsset)assetModel);
}
public override PrefabAssetModel MapDataToPrefabAssetModel()
{
EquippableItemPrefabAsset equippableItemPrefabAsset = new EquippableItemPrefabAsset(
Name,
PrefabAssetName,
new Vector3(Position[0], Position[1], Position[2])
);
2022-12-06 23:35:24 +01:00
// if we forcefully cast on EquippableItem by creating new clss variable - we lost data about Type
// because constructo think we pass Item not EquippableItem (because MapDataToObject return Item type)
var item = EquippableItemData.MapDataToObject(equippableItemPrefabAsset.PrefabAssetName) as EquippableItem;
if (item != null)
{
// successfully cast - it was oryginally EquippableItem
equippableItemPrefabAsset.EquippableItem = item;
}
else
{
// cast failed - it never were EquippableItem it plain Item
equippableItemPrefabAsset.EquippableItem = new EquippableItem(
EquippableItemData.MapDataToObject(equippableItemPrefabAsset.PrefabAssetName)
);
}
2022-11-06 21:34:17 +01:00
return equippableItemPrefabAsset;
}
}