Init
This commit is contained in:
77
Scripts/ECS/Entity.cs
Normal file
77
Scripts/ECS/Entity.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit.Core.Entites;
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
/// 用于Godot的ECS.Entity实现
|
||||
/// </summary>
|
||||
public partial class Entity : Node,IEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 类型组件的缓存
|
||||
/// </summary>
|
||||
private readonly Dictionary<Type,IEntityComponent> TypeComponents=new ();
|
||||
/// <summary>
|
||||
/// IEntityService的缓存
|
||||
/// </summary>
|
||||
private IEntitiesService _entitiesService;
|
||||
/// <summary>
|
||||
/// 所有EntityComponent
|
||||
/// </summary>
|
||||
private IEntityComponent[] _components;
|
||||
IEntityComponent[] IEntity.Components => _components;
|
||||
/// <summary>
|
||||
/// IEntity.Id实现
|
||||
/// </summary>
|
||||
public ulong Id { get; private set; }
|
||||
/// <summary>
|
||||
/// 加载所有EntityComponent的内部实现
|
||||
/// </summary>
|
||||
public override void _Ready()
|
||||
{
|
||||
List<IEntityComponent> entityComponents = new();
|
||||
Id = GetInstanceId();
|
||||
_entitiesService = DI.Get<IEntitiesService>();
|
||||
foreach (var x in MathNode.GetAllNode(this))
|
||||
{
|
||||
GetInstanceId();
|
||||
if (x is not IEntityComponent component) continue;
|
||||
component.Entity = this;
|
||||
TypeComponents.TryAdd(x.GetType(),component);
|
||||
BIT4Log.Log<Entity>($"已加载组件:{x.Name}");
|
||||
component.OnAwake();
|
||||
entityComponents.Add(component);
|
||||
}
|
||||
foreach (var component in TypeComponents.Values)
|
||||
{
|
||||
component.OnStart();
|
||||
}
|
||||
_entitiesService.Register(this);
|
||||
this._components = entityComponents.ToArray();
|
||||
}
|
||||
public bool TryGetComponent<T>(out T component) where T : IEntityComponent
|
||||
{
|
||||
if (TypeComponents.TryGetValue(typeof(T), out var iComponent) && iComponent is T _component)
|
||||
{
|
||||
component = _component;
|
||||
return true;
|
||||
}
|
||||
component = default;
|
||||
return false;
|
||||
}
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
if (disposing)
|
||||
{
|
||||
_entitiesService.UnRegister(this);
|
||||
}
|
||||
}
|
||||
|
||||
public bool RegisterComponent<T>(T component) where T : IEntityComponent
|
||||
{
|
||||
return TypeComponents.TryAdd(typeof(T), component);
|
||||
}
|
||||
}
|
13
Scripts/ECS/EntityComponent.cs
Normal file
13
Scripts/ECS/EntityComponent.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using BITKit.Core.Entites;
|
||||
using Godot;
|
||||
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
/// 基于Godot.Node3D的IEntityComponent实现
|
||||
/// </summary>
|
||||
public partial class EntityComponent : Node3D,IEntityComponent
|
||||
{
|
||||
public IEntity Entity { get; set; }
|
||||
public virtual void OnStart(){}
|
||||
public virtual void OnAwake(){}
|
||||
}
|
42
Scripts/ECS/GodotEntitiesService.cs
Normal file
42
Scripts/ECS/GodotEntitiesService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
// ReSharper disable All
|
||||
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
/// 基于Godot.Node的IEntitiesService实现
|
||||
/// </summary>
|
||||
public partial class GodotEntitiesService : Node,IEntitiesService
|
||||
{
|
||||
public GodotEntitiesService()
|
||||
{
|
||||
DI.Register<IEntitiesService>(this);
|
||||
}
|
||||
private readonly Dictionary<ulong,IEntity> _entities=new ();
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
public IEntity[] Entities => _entities.Values.ToArray();
|
||||
public bool Register(IEntity entity)
|
||||
{
|
||||
return _entities.TryAdd(entity.Id, entity);
|
||||
}
|
||||
public bool UnRegister(IEntity entity)
|
||||
{
|
||||
return _entities.TryRemove(entity.Id);
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_cancellationTokenSource = new();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if(disposing)_cancellationTokenSource.Cancel();
|
||||
}
|
||||
|
||||
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
||||
}
|
Reference in New Issue
Block a user