using System.Runtime.CompilerServices; using UnityEngine; namespace DCFApixels.DragonECS { public class EcsEntityConnect : MonoBehaviour { private sealed class Aspect : EcsAspect { public readonly EcsPool unityGameObjects; public Aspect(Builder b) { unityGameObjects = b.Include(); } } private entlong _entity; private EcsWorld _world; [SerializeField] private ScriptableEntityTemplate[] _scriptableTemplates; [SerializeField] private MonoEntityTemplate[] _monoTemplates; internal void SetTemplates_Editor(MonoEntityTemplate[] tempaltes) { _monoTemplates = tempaltes; } #region Properties public entlong Entity { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _entity; } public EcsWorld World { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _world; } public bool IsConected { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _entity.IsAlive; } #endregion public void ConnectWith(entlong entity, bool applyTemplates = false) { if (_entity.TryGetID(out int oldE) && _world != null) { var s = _world.GetAspect(); s.unityGameObjects.Del(oldE); } _world = null; if (entity.TryGetID(out int newE)) { _entity = entity; _world = _entity.World; var s = _world.GetAspect(); if (!s.unityGameObjects.Has(newE)) s.unityGameObjects.Add(newE) = new UnityGameObject(gameObject); if (applyTemplates) ApplyTemplates(); } else { _entity = entlong.NULL; } } public void ApplyTemplates() { ApplyTemplatesFor(_entity.ID); } public void ApplyTemplatesFor(int entityID) { foreach (var t in _scriptableTemplates) { t.Apply(_world.id, entityID); } foreach (var t in _monoTemplates) { t.Apply(_world.id, entityID); } } } }