147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
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<IEntityComponent>(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<T>(Action<T> action) => genericEvent.AddListener<T>(action);
|
|
public void Invoke<T>(T value) => genericEvent.Invoke<T>(value);
|
|
public void RemoveListener<T>(Action<T> action) => genericEvent.RemoveListener<T>(action);
|
|
public void AddListener<T>(string key, Action<T> action) => genericEvent.AddListener<T>(key, action);
|
|
public void Invoke<T>(string key, T value) => genericEvent.Invoke<T>(key, value);
|
|
public void Invoke<T>() where T : new() => genericEvent.Invoke<T>();
|
|
public void RemoveListener<T>(string key, Action<T> action) => genericEvent.RemoveListener<T>(key, action);
|
|
public T Get<T>(string key = Constant.System.Internal)
|
|
{
|
|
var value = genericEvent.Get<T>(key);
|
|
if (value is null && typeof(T).IsAssignableFrom(typeof(Component)))
|
|
{
|
|
if (TryGetComponent<T>(out var component))
|
|
{
|
|
Set<T>(component);
|
|
return component;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
public void Set<T>(T value) => genericEvent.Set<T>(value);
|
|
public void Set<T>(string key = Constant.System.Internal, T value = default) => genericEvent.Set<T>(key, value);
|
|
public T GetContext<T>(T value = default) => processor.GetContext<T>(value);
|
|
public void AddProcessor<T>(Func<T, T> func) => processor.AddProcessor<T>(func);
|
|
public void RemoveProcessor<T>(Func<T, T> func) => processor.RemoveProcessor<T>(func);
|
|
public T GetContext<T>(string key, T value) => processor.GetContext<T>(value);
|
|
public void AddProcessor<T>(string key, Func<T, T> func) => processor.AddProcessor<T>(key, func);
|
|
public void RemoveProcessor<T>(string key, Func<T, T> func) => processor.RemoveProcessor<T>(key, func);
|
|
|
|
public void RegisterCallback<T>(T t)
|
|
{
|
|
var value = GetCallbacks<T>() as List<T>;
|
|
value.Add(t);
|
|
}
|
|
|
|
public void UnRegisterCallback<T>(T t)
|
|
{
|
|
var value = GetCallbacks<T>() as List<T>;
|
|
value.Remove(t);
|
|
}
|
|
|
|
public IEnumerable<T> GetCallbacks<T>()
|
|
{
|
|
var value = Get<List<T>>(nameof(ICallback)).CreateOrAddIfEmety(() =>
|
|
{
|
|
List<T> newList = new();
|
|
Set<List<T>>(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<Entity>
|
|
{
|
|
public override VisualElement CreateInspectorGUI()
|
|
{
|
|
FillDefaultInspector();
|
|
var label = CreateSubTitle("Identity");
|
|
var idLabel = root.Create<Label>();
|
|
idLabel.text = agent.Id.ToString();
|
|
return root;
|
|
}
|
|
}
|
|
#endif
|
|
} |