2023-06-29 14:26:01 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2024-03-28 23:24:39 +08:00
|
|
|
|
|
|
|
|
|
|
2023-06-29 14:26:01 +08:00
|
|
|
|
#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]
|
|
|
|
|
public abstract class EcsWorldProvider<TWorld> : EcsWorldProviderBase where TWorld : EcsWorld
|
|
|
|
|
{
|
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]
|
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-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-03-28 23:24:39 +08:00
|
|
|
|
Set(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 21:22:48 +08:00
|
|
|
|
EcsWorldConfig config = new EcsWorldConfig(_entitiesCapacity, _groupCapacity, _poolsCapacity, _poolComponentsCapacity, _poolRecycledComponentsCapacity);
|
2024-03-07 08:26:42 +08:00
|
|
|
|
ConfigContainer configs = new ConfigContainer().Set(config);
|
2024-12-05 14:15:21 +08:00
|
|
|
|
return (TWorld)Activator.CreateInstance(typeof(TWorld), new object[] { configs, null, _worldID });
|
2024-03-03 03:51:49 +08:00
|
|
|
|
}
|
|
|
|
|
protected virtual void OnWorldCreated(TWorld world) { }
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2024-03-06 21:37:21 +08:00
|
|
|
|
}
|