init
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Extend this to create new type of data for <<typeparamref name="T"/>> type of object.<br/>
|
||||
/// To retrieve this data from <<typeparamref name="T"/>> object use <see cref="DataModule.Get{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"><see cref="BaseObject"/> (or any of its children) that this event can be attached to.</typeparam>
|
||||
[Serializable]
|
||||
public abstract class BaseData<T> : BaseData where T : BaseObject
|
||||
{
|
||||
public new T parent => (T)base.parent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b>DO NOT EXTEND THIS</b>, use <see cref="BaseData{T}"/> instead!
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public abstract class BaseData
|
||||
{
|
||||
internal BaseObject parent;
|
||||
|
||||
private protected BaseData()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a960c2eadeca4d4aa8d1c454daca552f
|
||||
timeCreated: 1773604425
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9970c419f4d94c22988563c8f8a50dc7
|
||||
timeCreated: 1774171193
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using RPGCore.Core.Objects;
|
||||
using RPGCoreCommon.Helpers.PropertyAttributeDrawers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class BaseObjectData : BaseData<BaseObject>
|
||||
{
|
||||
[Header("Uniqueness")]
|
||||
[field: SerializeField, ReadOnly] public string guid { get; private set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Header("Display")]
|
||||
public string name;
|
||||
public Texture2D icon;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53d2c35f3deb43588a3585e248d0a05e
|
||||
timeCreated: 1774171200
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(BaseObject))]
|
||||
public class DataModule : ObjectModule<BaseObject>
|
||||
{
|
||||
// SERIALIZED
|
||||
[SerializeReference] private List<BaseData> _dataList = new();
|
||||
|
||||
// RUNTIME
|
||||
private Dictionary<Type, BaseData> _dataDict;
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (!parent) return;
|
||||
|
||||
// Remove invalid
|
||||
if (_dataList.Any(data => data == null))
|
||||
_dataList = _dataList.Where(data => data != null).ToList();
|
||||
|
||||
// Remove duplicates
|
||||
if (_dataList.GroupBy(data => data.GetType()).Any(g => g.Count() > 1))
|
||||
_dataList = _dataList.GroupBy(data => data.GetType()).Select(g => g.First()).ToList();
|
||||
|
||||
// Create missing datas
|
||||
UnityEditor.TypeCache.GetTypesDerivedFrom<BaseObject>()
|
||||
.Append(typeof(BaseObject))
|
||||
.Where(t => t.IsAssignableFrom(parent.GetType()))
|
||||
.Select(t => typeof(BaseData<>).MakeGenericType(t))
|
||||
.SelectMany(t => UnityEditor.TypeCache.GetTypesDerivedFrom(t))
|
||||
.Where(t => !_dataList.Select(data => data.GetType()).Contains(t))
|
||||
.ToList().ForEach(t => _dataList.Add(Activator.CreateInstance(t) as BaseData));
|
||||
|
||||
_dataList.ForEach(data => data.parent = parent);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_dataDict = _dataList.ToDictionary(data => data.GetType(), data => data);
|
||||
}
|
||||
|
||||
public T Get<T>() where T : BaseData
|
||||
{
|
||||
return (T)_dataDict[typeof(T)];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa1c3e4ec0e74aa1a52278c024599a01
|
||||
timeCreated: 1773604350
|
||||
Reference in New Issue
Block a user