1
This commit is contained in:
@@ -5,29 +5,27 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
// ReSharper disable RedundantTypeArgumentsOfMethod
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class Entity : MonoBehaviour, IEntity
|
||||
public class Entity : MonoBehaviour, IUnityEntity
|
||||
{
|
||||
private readonly GenericEvent genericEvent = new();
|
||||
private readonly Processor processor = new();
|
||||
public IEntityComponent[] entityComponents { get; set; }
|
||||
public ulong Id { get; private set; }
|
||||
public CancellationToken CancellationToken => _cancellationToken;
|
||||
public CancellationToken CancellationToken { get; private set; }
|
||||
public IEntityBehavior[] Behaviors { get;private set; }
|
||||
public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray();
|
||||
|
||||
Core.Entites.IEntityComponent[] Core.Entites.IEntity.Components => _components;
|
||||
|
||||
bool Core.Entites.IEntity.RegisterComponent<T>(T component)
|
||||
bool Entities.IEntity.RegisterComponent<T>(T component)
|
||||
{
|
||||
throw new InvalidOperationException("Unity Entity can't register component");
|
||||
}
|
||||
|
||||
IServiceProvider Core.Entites.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
|
||||
IServiceProvider Entities.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
|
||||
public void Inject(object obj)
|
||||
{
|
||||
foreach (var fieldInfo in obj
|
||||
@@ -44,7 +42,7 @@ namespace BITKit.Entities
|
||||
{
|
||||
case null:
|
||||
break;
|
||||
case Core.Entites.IEntityComponent entityComponent:
|
||||
case Entities.IEntityComponent entityComponent:
|
||||
if(entityComponent.Entity.Id == Id)
|
||||
continue;
|
||||
break;
|
||||
@@ -61,23 +59,37 @@ namespace BITKit.Entities
|
||||
}
|
||||
else if(attribute?.CanBeNull is false)
|
||||
{
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
|
||||
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{obj.GetType().Name}需要的{type.FullName}");
|
||||
//BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CancellationToken _cancellationToken;
|
||||
public void AddService<T>(T service)
|
||||
{
|
||||
AddService(typeof(T),service);
|
||||
}
|
||||
|
||||
public void AddService(object service)
|
||||
{
|
||||
AddService(service.GetType(),service);
|
||||
}
|
||||
|
||||
public void AddService(Type type, object service)
|
||||
{
|
||||
genericEvent.Set(type,service);
|
||||
genericEvent.Set(type.FullName, service);
|
||||
genericEvent.SetDirect(type.FullName,service);
|
||||
}
|
||||
|
||||
private bool isInitialized;
|
||||
private Core.Entites.IEntityComponent[] _components => entityComponents.Cast<Core.Entites.IEntityComponent>().ToArray();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Id = (ulong)Guid.NewGuid().GetHashCode();
|
||||
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
Set(_cancellationToken);
|
||||
entityComponents = GetComponentsInChildren<IEntityComponent>(true).Distinct().ToArray();
|
||||
|
||||
CancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
Set(CancellationToken);
|
||||
Behaviors = GetComponentsInChildren<IEntityBehavior>(true).Distinct().ToArray();
|
||||
UnityEntitiesService.Register(this);
|
||||
}
|
||||
private void Start()
|
||||
@@ -85,6 +97,7 @@ namespace BITKit.Entities
|
||||
try
|
||||
{
|
||||
var monoBehaviours = GetComponentsInChildren<MonoBehaviour>(true);
|
||||
Behaviors.ForEach(x => x.Initialize(this));
|
||||
foreach (var x in monoBehaviours)
|
||||
{
|
||||
foreach (var att in x
|
||||
@@ -95,9 +108,7 @@ namespace BITKit.Entities
|
||||
.OfType<CustomTypeAttribute>()
|
||||
)
|
||||
{
|
||||
genericEvent.Set(att.Type,x);
|
||||
genericEvent.Set(att.Type.FullName, x);
|
||||
genericEvent.SetDirect(att.Type.FullName,x);
|
||||
AddService(att.Type, x);
|
||||
}
|
||||
genericEvent.Set(x.GetType(),x);
|
||||
}
|
||||
@@ -105,10 +116,10 @@ namespace BITKit.Entities
|
||||
{
|
||||
Inject(x);
|
||||
}
|
||||
entityComponents.ForEach(x => x.Initialize(this));
|
||||
|
||||
|
||||
entityComponents.ForEach(x => x.OnAwake());
|
||||
entityComponents.ForEach(x => x.OnStart());
|
||||
Behaviors.ForEach(x => x.OnAwake());
|
||||
Behaviors.ForEach(x => x.OnStart());
|
||||
isInitialized = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -122,21 +133,21 @@ namespace BITKit.Entities
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnDestroyComponent());
|
||||
Behaviors.ForEach(x => x.OnDestroyComponent());
|
||||
}
|
||||
UnityEntitiesService.UnRegister(this);
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnUpdate(Time.deltaTime));
|
||||
Behaviors.ForEach(x => x.OnUpdate(Time.deltaTime));
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
|
||||
Behaviors.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
|
||||
}
|
||||
private void LateUpdate()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnLateUpdate(Time.deltaTime));
|
||||
Behaviors.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);
|
||||
@@ -155,42 +166,6 @@ namespace BITKit.Entities
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(Entity))]
|
||||
|
Reference in New Issue
Block a user