breakpoint

This commit is contained in:
CortexCore
2023-09-15 23:02:46 +08:00
parent f6bf8fffe3
commit eabf0c6188
43 changed files with 1701 additions and 1582 deletions

View File

@@ -2,7 +2,10 @@ using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BITKit.Core.Entites;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit;
/// <summary>
/// 用于Godot的ECS.Entity实现
@@ -12,7 +15,7 @@ public partial class Entity : Node,IEntity
/// <summary>
/// 类型组件的缓存
/// </summary>
private readonly Dictionary<Type,IEntityComponent> TypeComponents=new ();
private readonly Dictionary<Type,object> TypeComponents=new ();
/// <summary>
/// IEntityService的缓存
/// </summary>
@@ -27,6 +30,11 @@ public partial class Entity : Node,IEntity
/// </summary>
public ulong Id { get; private set; }
/// <summary>
/// 服务提供者
/// </summary>
public IServiceProvider ServiceProvider { get; private set; }
private IServiceCollection _serviceCollection;
/// <summary>
/// 加载所有EntityComponent的内部实现
/// </summary>
public override void _Ready()
@@ -34,9 +42,26 @@ public partial class Entity : Node,IEntity
List<IEntityComponent> entityComponents = new();
Id = GetInstanceId();
_entitiesService = DI.Get<IEntitiesService>();
_serviceCollection = new ServiceCollection();
_serviceCollection.AddLogging();
foreach (var x in MathNode.GetAllNode(this))
{
GetInstanceId();
if (x is IEntityComponent entityComponent)
{
entityComponent.BuildService(_serviceCollection);
}
foreach (var customType in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
{
_serviceCollection.AddSingleton(customType.Type,x);
}
ServiceProvider = _serviceCollection.BuildServiceProvider();
if (x is not IEntityComponent component) continue;
component.Entity = this;
TypeComponents.TryAdd(component.BaseType,component);
@@ -44,7 +69,7 @@ public partial class Entity : Node,IEntity
component.OnAwake();
entityComponents.Add(component);
}
foreach (var component in TypeComponents.Values)
foreach (var component in TypeComponents.Values.OfType<IEntityComponent>())
{
component.OnStart();
}
@@ -52,7 +77,7 @@ public partial class Entity : Node,IEntity
this._components = entityComponents.ToArray();
SetMeta("Components",Variant.From(_components.Select(x=>x.GetType().Name).ToArray()));
}
public bool TryGetComponent<T>(out T component) where T : IEntityComponent
public bool TryGetComponent<T>(out T component)
{
if (TypeComponents.TryGetValue(typeof(T), out var iComponent) && iComponent is T _component)
{
@@ -71,8 +96,10 @@ public partial class Entity : Node,IEntity
}
}
public bool RegisterComponent<T>(T component) where T : IEntityComponent
public bool RegisterComponent<T>(T component)
{
return TypeComponents.TryAdd(typeof(T), component);
}
}

View File

@@ -1,6 +1,7 @@
using System;
using BITKit.Core.Entites;
using Godot;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit;
/// <summary>
@@ -10,6 +11,7 @@ public partial class EntityComponent : Node,IEntityComponent
{
public virtual Type BaseType => GetType();
public IEntity Entity { get; set; }
public virtual void BuildService(IServiceCollection serviceCollection){}
public virtual void OnStart(){}
public virtual void OnAwake(){}
}

View File

@@ -47,7 +47,7 @@ public partial class GodotEntitiesService : Node,IEntitiesService
throw new NotImplementedException();
}
public IEntity[] Query<T>() where T : IEntityComponent
public IEntity[] Query<T>()
{
return _entities.Values.Where(x => x.TryGetComponent<T>(out _)).ToArray();
}
@@ -76,7 +76,7 @@ public partial class GodotEntitiesService : Node,IEntitiesService
.Select(x => new ValueTuple<T>(x))
.ToArray();
}
public (T, T1)[] QueryComponents<T, T1>() where T : IEntityComponent where T1 : IEntityComponent
public (T, T1)[] QueryComponents<T, T1>()
{
var entities = _entities.Values.Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _));
var result = new List<(T, T1)>();
@@ -89,7 +89,7 @@ public partial class GodotEntitiesService : Node,IEntitiesService
return result.ToArray();
}
public (T, T1, T2)[] QueryComponents<T, T1, T2>() where T : IEntityComponent where T1 : IEntityComponent where T2 : IEntityComponent
public (T, T1, T2)[] QueryComponents<T, T1, T2>()
{
return _entities.Values
.Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _) && x.TryGetComponent<T2>(out _))