2023-06-29 14:26:01 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
|
|
|
|
public abstract class EcsWorldProviderBase : ScriptableObject
|
|
|
|
|
{
|
2024-03-03 03:51:49 +08:00
|
|
|
|
public abstract bool IsEmpty { get; }
|
|
|
|
|
public abstract void SetRaw(EcsWorld world);
|
|
|
|
|
public abstract EcsWorld GetRaw();
|
2023-06-29 14:26:01 +08:00
|
|
|
|
}
|
|
|
|
|
[Serializable]
|
|
|
|
|
public abstract class EcsWorldProvider<TWorld> : EcsWorldProviderBase where TWorld : EcsWorld
|
|
|
|
|
{
|
2024-03-07 03:18:00 +08:00
|
|
|
|
private readonly static EcsWorldConfig _emptyConfig = new EcsWorldConfig();
|
2024-03-03 03:51:49 +08:00
|
|
|
|
private TWorld _world;
|
|
|
|
|
|
2024-03-07 03:18:00 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
public short _worldID = -1;
|
|
|
|
|
|
|
|
|
|
[Header("Default Configs")]
|
|
|
|
|
[Header("Entites")]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int EntitiesCapacity = _emptyConfig.Get_EntitiesCapacity();
|
|
|
|
|
|
|
|
|
|
[Header("Groups")]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int GroupCapacity = _emptyConfig.Get_GroupCapacity();
|
|
|
|
|
|
|
|
|
|
[Header("Pools/Components")]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int PoolsCapacity = _emptyConfig.Get_PoolsCapacity();
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int PoolComponentsCapacity = _emptyConfig.Get_PoolComponentsCapacity();
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int PoolRecycledComponentsCapacity = _emptyConfig.Get_PoolRecycledComponentsCapacity();
|
|
|
|
|
|
2024-03-03 03:51:49 +08:00
|
|
|
|
#region Properties
|
|
|
|
|
public sealed override bool IsEmpty
|
2023-06-29 14:26:01 +08:00
|
|
|
|
{
|
2024-03-03 03:51:49 +08:00
|
|
|
|
get { return _world == null; }
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
public sealed override void SetRaw(EcsWorld worldRaw)
|
|
|
|
|
{
|
|
|
|
|
Set((TWorld)worldRaw);
|
2023-06-29 14:26:01 +08:00
|
|
|
|
}
|
2024-03-03 03:51:49 +08:00
|
|
|
|
public void Set(TWorld world)
|
|
|
|
|
{
|
|
|
|
|
_world = world;
|
|
|
|
|
}
|
|
|
|
|
public sealed override EcsWorld GetRaw()
|
|
|
|
|
{
|
|
|
|
|
return Get();
|
|
|
|
|
}
|
|
|
|
|
public TWorld Get()
|
2023-06-29 14:26:01 +08:00
|
|
|
|
{
|
|
|
|
|
if (_world == null || _world.IsDestroyed)
|
|
|
|
|
{
|
2024-03-03 03:51:49 +08:00
|
|
|
|
_world = BuildWorld();
|
2023-06-29 14:26:01 +08:00
|
|
|
|
OnWorldCreated(_world);
|
|
|
|
|
}
|
|
|
|
|
return _world;
|
|
|
|
|
}
|
2024-03-03 03:51:49 +08:00
|
|
|
|
protected static TProvider FindOrCreateSingleton<TProvider>() where TProvider : EcsWorldProvider<TWorld>
|
|
|
|
|
{
|
|
|
|
|
return FindOrCreateSingleton<TProvider>(typeof(TProvider).Name + "Singleton");
|
|
|
|
|
}
|
|
|
|
|
protected static TProvider FindOrCreateSingleton<TProvider>(string name) where TProvider : EcsWorldProvider<TWorld>
|
2023-06-29 14:26:01 +08:00
|
|
|
|
{
|
|
|
|
|
TProvider instance = Resources.Load<TProvider>(name);
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = CreateInstance<TProvider>();
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (AssetDatabase.IsValidFolder("Assets/Resources/") == false)
|
|
|
|
|
{
|
|
|
|
|
System.IO.Directory.CreateDirectory(Application.dataPath + "/Resources/");
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
}
|
|
|
|
|
AssetDatabase.CreateAsset(instance, "Assets/Resources/" + name + ".asset");
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2024-03-03 03:51:49 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
protected virtual TWorld BuildWorld()
|
|
|
|
|
{
|
2024-03-07 03:18:00 +08:00
|
|
|
|
EcsWorldConfig config = new EcsWorldConfig();
|
|
|
|
|
config.Set_EntitiesCapacity(EntitiesCapacity);
|
|
|
|
|
config.Set_GroupCapacity(GroupCapacity);
|
|
|
|
|
config.Set_PoolComponentsCapacity(PoolComponentsCapacity);
|
|
|
|
|
config.Set_PoolRecycledComponentsCapacity(PoolRecycledComponentsCapacity);
|
|
|
|
|
config.Set_PoolsCapacity(PoolsCapacity);
|
|
|
|
|
return (TWorld)Activator.CreateInstance(typeof(TWorld), new object[] { config, _worldID });
|
2024-03-03 03:51:49 +08:00
|
|
|
|
}
|
|
|
|
|
protected virtual void OnWorldCreated(TWorld world) { }
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2024-03-06 21:37:21 +08:00
|
|
|
|
}
|