I'm trying to find the best way to create a dictionary that can hold 6 different sub-types of the abstract Reward type, while also remember the individual item's original sub-type when pulled out again. Several approaches have worked, but I feel like they were all sub-par and that there must be better solutions.
Solutions that works, but I think could be improved on:
A list for each reward sub-type. (Will not scale well as I add large amounts of sub-types)
A list of lists. (This is both ugly and seems inefficient)
Multidimensional array. (Same issues as a list of lists)
Here are the relevant class snippets for my scenario:
public class RewardAssetContainer : MonoBehaviour
{
// Sounds
private static AudioClip losenedBowels = new AudioClip();
public static AudioClip LosenedBowels { get { return losenedBowels; } }
}
public abstract class Reward
{
protected EffectTypes effectType;
protected Rareity rareity;
protected Sprite itemIcon;
protected string itemName;
protected GameObject specialEffect;
}
interface iSoundEffect
{
void SetVolume(float _newVol);
void PlaySound();
void StopSound();
}
class DeathSoundEffect: Reward, iSoundEffect
{
private AudioSource sound;
public DeathSoundEffect(string _itemName, AudioClip _sound, float _vol, EffectTypes _effectType, Rareity _rareity, Sprite _itemIcon)
{
sound = new AudioSource();
itemName = _itemName;
sound.volume = _vol;
sound.clip = _sound;
effectType = _effectType;
rareity = _rareity;
itemIcon = _itemIcon;
}
public void PlaySound()
{
sound.Play();
}
}
public class Item_Inventory : MonoBehaviour
{
private static Dictionary<string, Reward> rewardItemsOwned = new Dictionary<string, Reward>();
public static Reward GetSpecificItem(string _keyValue) { return rewardItemsOwned[_keyValue]; }
private void TestPutToInventory()
{
rewardItemsOwned.Add("LosenedBowels", new DeathSoundEffect("LosenedBowels", RewardAssetContainer.LosenedBowels, 0.5f, EffectTypes.DeathSoundEffect, Rareity.Rare, RewardAssetContainer.EpicExample));
}
}
I would like to, in a different class, be able to do something like:
var tempRewardItem = Item_Inventory.GetSpecificItem("LosenedBowels");
tempRewardItem .PlaySound();
But i can't figure out an elegant way to do this with one collection.