2024-03-09 03:35:01 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2023-05-07 00:50:44 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
2024-03-09 22:36:35 +08:00
|
|
|
using UnityEditor;
|
2023-05-07 00:50:44 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2024-03-09 09:56:45 +08:00
|
|
|
[DisallowMultipleComponent]
|
2023-05-07 00:50:44 +08:00
|
|
|
public class EcsEntityConnect : MonoBehaviour
|
|
|
|
{
|
2023-06-22 14:30:03 +08:00
|
|
|
private sealed class Aspect : EcsAspect
|
2023-05-07 00:50:44 +08:00
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
public EcsPool<UnityGameObject> unityGameObjects;
|
|
|
|
protected override void Init(Builder b)
|
2023-05-07 00:50:44 +08:00
|
|
|
{
|
|
|
|
unityGameObjects = b.Include<UnityGameObject>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private entlong _entity;
|
|
|
|
private EcsWorld _world;
|
|
|
|
|
|
|
|
[SerializeField]
|
2024-03-03 03:51:49 +08:00
|
|
|
private ScriptableEntityTemplate[] _scriptableTemplates;
|
2023-05-07 00:50:44 +08:00
|
|
|
[SerializeField]
|
2024-03-03 03:51:49 +08:00
|
|
|
private MonoEntityTemplate[] _monoTemplates;
|
2023-05-07 00:50:44 +08:00
|
|
|
|
|
|
|
#region Properties
|
|
|
|
public entlong Entity
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-03-09 03:35:01 +08:00
|
|
|
get { return _entity; }
|
2023-05-07 00:50:44 +08:00
|
|
|
}
|
|
|
|
public EcsWorld World
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
get => _world;
|
|
|
|
}
|
2024-03-03 03:51:49 +08:00
|
|
|
public bool IsConected
|
2023-05-07 00:50:44 +08:00
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
get => _entity.IsAlive;
|
|
|
|
}
|
2024-03-09 03:35:01 +08:00
|
|
|
public IEnumerable<ScriptableEntityTemplate> ScriptableTemplates
|
|
|
|
{
|
|
|
|
get { return _scriptableTemplates; }
|
|
|
|
}
|
|
|
|
public IEnumerable<MonoEntityTemplate> MonoTemplates
|
|
|
|
{
|
|
|
|
get { return _monoTemplates; }
|
|
|
|
}
|
|
|
|
public IEnumerable<ITemplateInternal> AllTemplates
|
|
|
|
{
|
|
|
|
get { return ((IEnumerable<ITemplateInternal>)_scriptableTemplates).Concat(_monoTemplates); }
|
|
|
|
}
|
2023-05-07 00:50:44 +08:00
|
|
|
#endregion
|
|
|
|
|
2024-03-09 03:35:01 +08:00
|
|
|
#region Connect
|
2023-05-07 00:50:44 +08:00
|
|
|
public void ConnectWith(entlong entity, bool applyTemplates = false)
|
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
if (_entity.TryGetID(out int oldEntityID) && _world != null)
|
2023-05-07 00:50:44 +08:00
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
var a = _world.GetAspect<Aspect>();
|
2024-03-09 09:42:04 +08:00
|
|
|
a.unityGameObjects.TryDel(oldEntityID);
|
2023-05-07 00:50:44 +08:00
|
|
|
}
|
|
|
|
_world = null;
|
|
|
|
|
2024-03-09 03:35:01 +08:00
|
|
|
if (entity.TryGetID(out int newEntityID))
|
2023-05-07 00:50:44 +08:00
|
|
|
{
|
|
|
|
_entity = entity;
|
|
|
|
_world = _entity.World;
|
2024-03-09 03:35:01 +08:00
|
|
|
var a = _world.GetAspect<Aspect>();
|
|
|
|
if (!a.unityGameObjects.Has(newEntityID))
|
|
|
|
{
|
|
|
|
a.unityGameObjects.Add(newEntityID) = new UnityGameObject(gameObject);
|
|
|
|
}
|
2023-05-07 00:50:44 +08:00
|
|
|
if (applyTemplates)
|
2024-03-09 03:35:01 +08:00
|
|
|
{
|
2023-05-07 00:50:44 +08:00
|
|
|
ApplyTemplates();
|
2024-03-09 03:35:01 +08:00
|
|
|
}
|
2023-05-07 00:50:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_entity = entlong.NULL;
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 03:35:01 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ApplyTemplates
|
2024-03-03 03:51:49 +08:00
|
|
|
public void ApplyTemplates()
|
|
|
|
{
|
|
|
|
ApplyTemplatesFor(_entity.ID);
|
|
|
|
}
|
2023-05-07 00:50:44 +08:00
|
|
|
public void ApplyTemplatesFor(int entityID)
|
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
foreach (var template in _scriptableTemplates)
|
2024-03-03 03:51:49 +08:00
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
template.Apply(_world.id, entityID);
|
2024-03-03 03:51:49 +08:00
|
|
|
}
|
2024-03-09 03:35:01 +08:00
|
|
|
foreach (var template in _monoTemplates)
|
2024-03-03 03:51:49 +08:00
|
|
|
{
|
2024-03-09 03:35:01 +08:00
|
|
|
template.Apply(_world.id, entityID);
|
2024-03-03 03:51:49 +08:00
|
|
|
}
|
2023-05-07 00:50:44 +08:00
|
|
|
}
|
2024-03-09 03:35:01 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Editor
|
2024-03-09 22:36:35 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
[ContextMenu("Autoset")]
|
|
|
|
internal void Autoset_Editor()
|
2024-03-09 03:35:01 +08:00
|
|
|
{
|
2024-03-09 22:36:35 +08:00
|
|
|
Autoset(this);
|
2024-03-09 03:35:01 +08:00
|
|
|
}
|
2024-03-09 22:36:35 +08:00
|
|
|
[ContextMenu("Autoset Cascade")]
|
|
|
|
internal void AutosetCascade_Editor()
|
|
|
|
{
|
|
|
|
foreach (var item in GetComponentsInChildren<EcsEntityConnect>())
|
|
|
|
{
|
|
|
|
Autoset(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[ContextMenu("Unlink Entity")]
|
|
|
|
internal void UnlinkEntity_Editor()
|
|
|
|
{
|
|
|
|
ConnectWith(entlong.NULL);
|
|
|
|
}
|
|
|
|
[ContextMenu("Delete Entity")]
|
|
|
|
internal void DeleteEntity_Editor()
|
|
|
|
{
|
|
|
|
if (_entity.TryUnpack(out int id, out EcsWorld world))
|
|
|
|
{
|
|
|
|
world.DelEntity(id);
|
|
|
|
}
|
|
|
|
UnlinkEntity_Editor();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Autoset(EcsEntityConnect target)
|
|
|
|
{
|
|
|
|
var result = target.MonoTemplates.Where(o => o != null).Union(GetTemplatesFor(target.transform));
|
|
|
|
|
|
|
|
target._monoTemplates = result.ToArray();
|
|
|
|
EditorUtility.SetDirty(target);
|
|
|
|
}
|
|
|
|
private static IEnumerable<MonoEntityTemplate> GetTemplatesFor(Transform parent)
|
|
|
|
{
|
|
|
|
IEnumerable<MonoEntityTemplate> result = parent.GetComponents<MonoEntityTemplate>();
|
|
|
|
for (int i = 0; i < parent.childCount; i++)
|
|
|
|
{
|
|
|
|
var child = parent.GetChild(i);
|
|
|
|
if (child.TryGetComponent<EcsEntityConnect>(out _))
|
|
|
|
{
|
|
|
|
return Enumerable.Empty<MonoEntityTemplate>();
|
|
|
|
}
|
|
|
|
result = result.Concat(GetTemplatesFor(child));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-09 03:35:01 +08:00
|
|
|
#endregion
|
2023-05-07 00:50:44 +08:00
|
|
|
}
|
2024-03-06 21:37:21 +08:00
|
|
|
}
|