DragonECS-Unity/src/Connectors/EcsWorldProvider.cs

147 lines
4.6 KiB
C#
Raw Normal View History

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();
2024-03-10 04:56:29 +08:00
public abstract EcsWorld GetCurrentWorldRaw();
2023-06-29 14:26:01 +08:00
}
[Serializable]
2025-03-10 13:08:18 +08:00
public abstract class EcsWorldProvider<TWorld> : EcsWorldProviderBase, IEcsModule
where TWorld : EcsWorld
2023-06-29 14:26:01 +08:00
{
2024-03-03 03:51:49 +08:00
private TWorld _world;
2025-03-10 13:08:18 +08:00
[Header("Default Configs")]
[Header("Base")]
2024-03-07 03:18:00 +08:00
[SerializeField]
2025-03-10 13:08:18 +08:00
private short _worldID = -1;
private string _worldName = "";
2024-03-07 03:18:00 +08:00
[Header("Entites")]
[SerializeField]
2024-03-07 21:22:48 +08:00
private int _entitiesCapacity = EcsWorldConfig.Default.EntitiesCapacity;
2024-03-07 03:18:00 +08:00
[Header("Groups")]
[SerializeField]
2024-03-07 21:22:48 +08:00
private int _groupCapacity = EcsWorldConfig.Default.GroupCapacity;
2024-03-07 03:18:00 +08:00
[Header("Pools/Components")]
[SerializeField]
2024-03-07 21:22:48 +08:00
private int _poolsCapacity = EcsWorldConfig.Default.PoolsCapacity;
2024-03-07 03:18:00 +08:00
[SerializeField]
2024-03-07 21:22:48 +08:00
private int _poolComponentsCapacity = EcsWorldConfig.Default.PoolComponentsCapacity;
2024-03-07 03:18:00 +08:00
[SerializeField]
2024-03-07 21:22:48 +08:00
private int _poolRecycledComponentsCapacity = EcsWorldConfig.Default.PoolRecycledComponentsCapacity;
2024-03-07 03:18:00 +08:00
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; }
}
2024-12-06 19:41:09 +08:00
public short WorldID
{
get { return _worldID; }
}
2025-03-10 13:08:18 +08:00
public string WorldName
{
get { return _worldName; }
}
2024-03-07 21:22:48 +08:00
public int EntitiesCapacity
{
get { return _entitiesCapacity; }
}
public int GroupCapacity
{
get { return _groupCapacity; }
}
public int PoolsCapacity
{
get { return _poolsCapacity; }
}
public int PoolComponentsCapacity
{
get { return _poolComponentsCapacity; }
}
public int PoolRecycledComponentsCapacity
{
get { return _poolRecycledComponentsCapacity; }
}
2024-03-03 03:51:49 +08:00
#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();
}
2024-03-10 04:56:29 +08:00
public sealed override EcsWorld GetCurrentWorldRaw()
{
return _world;
}
2024-03-03 03:51:49 +08:00
public TWorld Get()
2023-06-29 14:26:01 +08:00
{
if (_world == null || _world.IsDestroyed)
{
2024-12-06 19:41:09 +08:00
EcsWorldConfig config = new EcsWorldConfig(_entitiesCapacity, _groupCapacity, _poolsCapacity, _poolComponentsCapacity, _poolRecycledComponentsCapacity);
ConfigContainer configs = new ConfigContainer().Set(config);
Set(BuildWorld(configs));
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;
}
2025-03-10 13:08:18 +08:00
void IEcsModule.Import(EcsPipeline.Builder b)
{
var wolrd = Get();
b.Inject(wolrd);
if (b.IsEnableWorldDebug())
{
b.AddUnityDebug(wolrd);
}
}
2024-03-03 03:51:49 +08:00
#endregion
#region Events
2025-03-10 13:08:18 +08:00
//TODO переименовать в CreateWorld
2024-12-06 19:41:09 +08:00
protected abstract TWorld BuildWorld(ConfigContainer configs);
2024-03-03 03:51:49 +08:00
protected virtual void OnWorldCreated(TWorld world) { }
#endregion
}
2024-03-06 21:37:21 +08:00
}