1
This commit is contained in:
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@@ -18,6 +18,8 @@ public partial class Entity : Node,IEntity
|
||||
/// 类型组件的缓存
|
||||
/// </summary>
|
||||
private readonly Dictionary<Type,object> TypeComponents=new ();
|
||||
|
||||
private Object[] Components = Array.Empty<Object>();
|
||||
/// <summary>
|
||||
/// IEntityService的缓存
|
||||
/// </summary>
|
||||
@@ -25,8 +27,7 @@ public partial class Entity : Node,IEntity
|
||||
/// <summary>
|
||||
/// 所有EntityComponent
|
||||
/// </summary>
|
||||
private IEntityComponent[] _components;
|
||||
IEntityComponent[] IEntity.Components => _components;
|
||||
IEntityComponent[] IEntity.Components => Components.OfType<IEntityComponent>().ToArray();
|
||||
/// <summary>
|
||||
/// IEntity.Id实现
|
||||
/// </summary>
|
||||
@@ -40,14 +41,27 @@ public partial class Entity : Node,IEntity
|
||||
/// 服务提供者
|
||||
/// </summary>
|
||||
public IServiceProvider ServiceProvider { get; private set; }
|
||||
/// <summary>
|
||||
/// 服务集合
|
||||
/// </summary>
|
||||
public IServiceCollection ServiceCollection { get; private set; }
|
||||
|
||||
|
||||
public void Inject(object obj)
|
||||
public void Inject(object node)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
foreach (var fieldInfo in node.GetType()
|
||||
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(x=>x.GetCustomAttribute<InjectAttribute>() is not null)
|
||||
)
|
||||
{
|
||||
var attribute = fieldInfo.GetCustomAttribute<InjectAttribute>();
|
||||
if(attribute is null)continue;
|
||||
fieldInfo.SetValue(node,
|
||||
attribute.CanBeNull
|
||||
? ServiceProvider.GetService(fieldInfo.FieldType)
|
||||
: ServiceProvider.GetRequiredService(fieldInfo.FieldType));
|
||||
}
|
||||
}
|
||||
|
||||
private IServiceCollection _serviceCollection;
|
||||
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
@@ -57,7 +71,7 @@ public partial class Entity : Node,IEntity
|
||||
public override void _ExitTree()
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
_entitiesService.UnRegister(this);
|
||||
DI.Get<IEntitiesService>().UnRegister(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -65,44 +79,77 @@ public partial class Entity : Node,IEntity
|
||||
/// </summary>
|
||||
public override void _Ready()
|
||||
{
|
||||
List<IEntityComponent> entityComponents = new();
|
||||
Id = GetInstanceId();
|
||||
|
||||
ServiceCollection = new ServiceCollection();
|
||||
|
||||
DI.Get<IEntitiesService>().Register(this);
|
||||
|
||||
Components = MathNode.GetAllNode(this).ToArray();
|
||||
List<IEntityComponent> entityComponents = new();
|
||||
|
||||
_entitiesService = DI.Get<IEntitiesService>();
|
||||
|
||||
_serviceCollection = new ServiceCollection();
|
||||
_serviceCollection.AddLogging();
|
||||
|
||||
foreach (var x in MathNode.GetAllNode(this))
|
||||
|
||||
foreach (var x in Components)
|
||||
{
|
||||
GetInstanceId();
|
||||
|
||||
|
||||
if (x is IEntityComponent entityComponent)
|
||||
{
|
||||
entityComponent.BuildService(_serviceCollection);
|
||||
entityComponent.BuildService(ServiceCollection);
|
||||
}
|
||||
|
||||
|
||||
TypeComponents.TryAdd(x.GetType(), x);
|
||||
foreach (var customType in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
|
||||
{
|
||||
_serviceCollection.AddSingleton(customType.Type,x);
|
||||
TypeComponents.TryAdd(customType.Type, x);
|
||||
ServiceCollection.AddSingleton(customType.Type, x);
|
||||
}
|
||||
ServiceProvider = _serviceCollection.BuildServiceProvider();
|
||||
|
||||
ServiceProvider = ServiceCollection.BuildServiceProvider();
|
||||
|
||||
if (x is not IEntityComponent component) continue;
|
||||
component.Entity = this;
|
||||
TypeComponents.TryAdd(component.BaseType,component);
|
||||
//BIT4Log.Log<Entity>($"已加载组件:{x.Name}");
|
||||
component.OnAwake();
|
||||
entityComponents.Add(component);
|
||||
}
|
||||
foreach (var component in TypeComponents.Values.OfType<IEntityComponent>())
|
||||
|
||||
foreach (var node in Components)
|
||||
{
|
||||
Inject(node);
|
||||
}
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.Initialize(this);
|
||||
}
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.OnAwake();
|
||||
}
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.OnStart();
|
||||
}
|
||||
_entitiesService.Register(this);
|
||||
this._components = entityComponents.ToArray();
|
||||
SetMeta("Components",Variant.From(_components.Select(x=>x.GetType().Name).ToArray()));
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.OnUpdate((float)delta);
|
||||
}
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.OnLateUpdate((float)delta);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
foreach (var component in Components.OfType<IEntityBehavior>())
|
||||
{
|
||||
component.OnFixedUpdate((float)delta);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetComponent<T>(out T component)
|
||||
{
|
||||
|
26
BITKit/Scripts/ECS/Core/EntityBehaviour.cs
Normal file
26
BITKit/Scripts/ECS/Core/EntityBehaviour.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using BITKit.Entities;
|
||||
|
||||
namespace BITKit;
|
||||
|
||||
public abstract partial class EntityBehaviour:EntityComponent,IEntityBehavior
|
||||
{
|
||||
public virtual void Initialize(IEntity _entity)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnUpdate(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnFixedUpdate(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnLateUpdate(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnDestroyComponent()
|
||||
{
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using Godot;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
@@ -3,7 +3,8 @@ using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
@@ -22,6 +23,7 @@ public partial class GodotEntitiesService : Node,IEntitiesService
|
||||
public IEntity[] Entities => _entities.Values.ToArray();
|
||||
public bool Register(IEntity entity)
|
||||
{
|
||||
entity.ServiceCollection.AddSingleton<IEntitiesService>(this);
|
||||
return _entities.TryAdd(entity.Id, entity);
|
||||
}
|
||||
public bool UnRegister(IEntity entity)
|
||||
|
Reference in New Issue
Block a user