Skip to content

Instantly share code, notes, and snippets.

@skjalgsm
Last active January 7, 2021 11:22
Show Gist options
  • Save skjalgsm/af54f87d38e21f32f6ef2bd17f278329 to your computer and use it in GitHub Desktop.
Save skjalgsm/af54f87d38e21f32f6ef2bd17f278329 to your computer and use it in GitHub Desktop.
public static class RPGItemUtility
{
private static readonly List<RPGItem> All = new List<RPGItem>();
private static bool IsLoaded;
private static TaskCompletionSource<bool> IsLoadingComplete;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Load()
{
IsLoadingComplete = new TaskCompletionSource<bool>();
IsLoaded = false;
All.Clear();
var assetLabel = new AssetLabelReference {labelString = "Items"};
Addressables.LoadAssetsAsync<RPGItem>(assetLabel, LoadCallback).Completed += OnCompleted;
}
private static void OnCompleted(AsyncOperationHandle<IList<RPGItem>> obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
var list = obj.Result;
All.Clear();
All.AddRange(list.Distinct());
IsLoaded = true;
IsLoadingComplete.TrySetResult(IsLoaded);
}
}
private static void LoadCallback(RPGItem task)
{ }
public static async Task<List<RPGItem>> Events()
{
await IsLoadingComplete.Task;
return All;
}
}
[Flags]
public enum EquipmentSlot
{
LeftHand,
RightHand,
Chest,
Head,
Feet
}
[Flags]
public enum DamageType
{
Regular,
Fire,
Poison
}
[Serializable]
public class Damage
{
public uint Amount;
public DamageType DamageType;
}
[Serializable]
public class Heal
{
public uint Amount;
public HealType DamageType;
}
[Flags]
public enum HealType
{
Regular,
Beer
}
//The Addressables/Items folder is added to addressables list and tagged with "Items"
[CreateAssetMenu(fileName = "Addressables/Items/New RPGItem", menuName = "Assets/Item")]
public class RPGItem : ScriptableObject
{
//Sword
public LocalizedString LocalizedTitle;
//A sharp weapon of death
public LocalizedString LocalizedDescription;
//The 2d sprite used in the UI
public AssetReference InventoryUIAsset;
//The 3d model dropped on the ground
public AssetReference WorldAsset;
public uint PurchaseValue;
public uint SellValue;
//None means not equippable (i.e consumable)
public EquipmentSlot EquipmentSlot;
//The damage this item deals
public Damage Damage;
//The heal this item gives
public Heal Heal;
}
[CustomEditor(typeof(RPGItem))]
public class RPGItemEditor : Editor
{
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
var item = target as RPGItem;
if (item != null)
{
if (item.WorldAsset != null && item.WorldAsset.IsValid())
{
//Draw WorldAsset
}
else if (item.InventoryUIAsset != null && item.InventoryUIAsset.IsValid())
{
//Draw InventoryAsset
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment