This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using BITKit.Core.Entites;
using Cysharp.Threading.Tasks;
@@ -17,30 +18,62 @@ namespace BITKit.Entities
private readonly Processor processor = new();
public IEntityComponent[] entityComponents { get; set; }
public ulong Id { get; private set; }
public CancellationToken CancellationToken => _cancellationToken;
Core.Entites.IEntityComponent[] Core.Entites.IEntity.Components => _components;
bool Core.Entites.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");
private CancellationToken _cancellationToken;
private bool isInitialized;
private Core.Entites.IEntityComponent[] _components => entityComponents;
private void Awake()
{
Id = (ulong)Guid.NewGuid().GetHashCode();
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
Set(_cancellationToken);
foreach (var serviceRegister in GetComponentsInChildren<IServiceRegister>(true))
{
genericEvent.Set(serviceRegister.BaseType, serviceRegister);
}
entityComponents = GetComponentsInChildren<IEntityComponent>(true).Distinct().ToArray();
foreach (var x in entityComponents)
{
foreach (var att in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
{
genericEvent.Set(att.Type, x);
}
}
entityComponents.ForEach(x => x.Initialize(this));
UnityEntitiesService.Register(this);
}
private void Start()
{
foreach (var x in entityComponents)
{
foreach (var att in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
{
genericEvent.Set(att.Type,x);
genericEvent.Set(att.Type.FullName, x);
genericEvent.SetDirect(att.Type.FullName,x);
}
}
foreach (var x in GetComponentsInChildren<MonoBehaviour>(true))
{
foreach (var fieldInfo in x
.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(fieldInfo=>fieldInfo.GetCustomAttribute<InjectAttribute>() is not null))
{
var type = fieldInfo.FieldType;
if(genericEvent.TryGetObjectDirect(type.FullName,out var value))
{
fieldInfo.SetValue(x,value);
}
else
{
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
}
}
}
entityComponents.ForEach(x => x.Initialize(this));
entityComponents.ForEach(x => x.OnAwake());
entityComponents.ForEach(x => x.OnStart());
isInitialized = true;