using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.VisualScripting; using UnityEngine.Events; using System.Linq; using UnityEngine.UIElements; namespace BITKit.Entities { [Inspectable] public class Entity : MonoBehaviour, IEntity { public string addressablePath="Entity"; GenericEvent genericEvent = new(); Processor processor = new(); public IEntityComponent[] entityComponents { get; set; } public int Id { get { if (id is 0) { id = Guid.NewGuid().GetHashCode(); } return id; } set { if (id != value) { id = value; EntitiesManager.Dictionary.TryRemove(id, out var _); EntitiesManager.GetOrAdd(id, x => this); } } } int id; protected virtual void Awake() { entityComponents = GetComponentsInChildren(true); entityComponents.ForEach(x => x.Initialize(this)); } protected virtual void Start() { entityComponents.ForEach(x => x.OnAwake()); entityComponents.ForEach(x => x.OnStart()); EntitiesManager.Dictionary.AddOrUpdate(id, (x) => this, (x1, x2) => this); } protected virtual void OnDestroy() { entityComponents.ForEach(x => x.OnDestroyComponent()); EntitiesManager.Dictionary.TryRemove(id, out var _); } protected virtual void Update() { entityComponents.ForEach(x => x.OnUpdate(Time.deltaTime)); } protected virtual void FixedUpdate() { entityComponents.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime)); } protected virtual void LateUpdate() { entityComponents.ForEach(x => x.OnLateUpdate(Time.deltaTime)); } public void AddListener(Action action) => genericEvent.AddListener(action); public void Invoke(T value) => genericEvent.Invoke(value); public void RemoveListener(Action action) => genericEvent.RemoveListener(action); public void AddListener(string key, Action action) => genericEvent.AddListener(key, action); public void Invoke(string key, T value) => genericEvent.Invoke(key, value); public void Invoke() where T : new() => genericEvent.Invoke(); public void RemoveListener(string key, Action action) => genericEvent.RemoveListener(key, action); public T Get(string key = Constant.System.Internal) { var value = genericEvent.Get(key); if (value is null && typeof(T).IsAssignableFrom(typeof(Component))) { if (TryGetComponent(out var component)) { Set(component); return component; } } return value; } public void Set(T value) => genericEvent.Set(value); public void Set(string key = Constant.System.Internal, T value = default) => genericEvent.Set(key, value); public T GetContext(T value = default) => processor.GetContext(value); public void AddProcessor(Func func) => processor.AddProcessor(func); public void RemoveProcessor(Func func) => processor.RemoveProcessor(func); public T GetContext(string key, T value) => processor.GetContext(value); public void AddProcessor(string key, Func func) => processor.AddProcessor(key, func); public void RemoveProcessor(string key, Func func) => processor.RemoveProcessor(key, func); public void RegisterCallback(T t) { var value = GetCallbacks() as List; value.Add(t); } public void UnRegisterCallback(T t) { var value = GetCallbacks() as List; value.Remove(t); } public IEnumerable GetCallbacks() { var value = Get>(nameof(ICallback)).CreateOrAddIfEmety(() => { List newList = new(); Set>(nameof(ICallback), newList); return newList; }); if (value is null) { Debug.LogWarning("List is Null"); } return value; } public int[] GetInts() => new int[] { id }; public float[] GetFloats() => null; public byte[] GetBytes() => null; } #if UNITY_EDITOR [UnityEditor.CustomEditor(typeof(Entity))] public class EntityInspector : BITInspector { public override VisualElement CreateInspectorGUI() { FillDefaultInspector(); var label = CreateSubTitle("Identity"); var idLabel = root.Create