Scriptum/Assets/Scripts/REFACTORING/Models/Chest/ChestData.cs
2022-11-24 03:03:30 +01:00

72 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[System.Serializable]
public class ChestData : ModelData<Chest, GameObject>
{
[SerializeField]
public string description;
[SerializeField]
public ChestTypeEnum type;
[SerializeField]
public List<IndexValuePair<EquippableItemData>> content = new List<IndexValuePair<EquippableItemData>>();
protected override string SPRITE_LOCALIZATION => "Sprites/Object Sprites/";
protected override string MODEL_LOCALIZATION => "Chests/";
public ChestData(Chest chest) : base(chest.id, chest.name, chest.chestModel.name)
{
description = chest.description;
foreach(IndexValuePair<EquippableItem> item in chest.Content)
{
content.Add(
new IndexValuePair<EquippableItemData>(item.Key, new EquippableItemData(item.Value))
);
}
}
// dont use parameter - chest is not scriptable object, dont have its model in Assets
public override Chest MapDataToObject()
{
// 1. Find object
GameObject chestGameObject = TryFindResource(modelName);
var chest = new Chest(name, description, chestGameObject, type);
// 2. Set chest content from passed data
chest.Content.Clear();
foreach (IndexValuePair<EquippableItemData> item in content)
{
var castedObject = item.Value.MapDataToObject() as EquippableItem;
if(castedObject == null)
chest.Content.Add(
new IndexValuePair<EquippableItem>(item.Key, new EquippableItem(item.Value.MapDataToObject()))
);
else
chest.Content.Add(
new IndexValuePair<EquippableItem>(item.Key, castedObject)
);
}
return chest;
}
public override Chest MapDataToObject(string prefarbAssetName) { throw new System.NotImplementedException(); }
protected override GameObject TryFindResource(string modelName)
{
var resource = Resources.Load<GameObject>(MODEL_LOCALIZATION + modelName);
if (!resource)
throw new System.Exception($"Resource {modelName} not found!!");
return resource;
}
}