readme
This commit is contained in:
@@ -39,7 +39,7 @@ public partial class Entity : Node,IEntity
|
||||
GetInstanceId();
|
||||
if (x is not IEntityComponent component) continue;
|
||||
component.Entity = this;
|
||||
TypeComponents.TryAdd(x.GetType(),component);
|
||||
TypeComponents.TryAdd(component.BaseType,component);
|
||||
//BIT4Log.Log<Entity>($"已加载组件:{x.Name}");
|
||||
component.OnAwake();
|
||||
entityComponents.Add(component);
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using BITKit.Core.Entites;
|
||||
using Godot;
|
||||
|
||||
@@ -5,8 +6,9 @@ namespace BITKit;
|
||||
/// <summary>
|
||||
/// 基于Godot.Node3D的IEntityComponent实现
|
||||
/// </summary>
|
||||
public partial class EntityComponent : Node3D,IEntityComponent
|
||||
public partial class EntityComponent : Node,IEntityComponent
|
||||
{
|
||||
public virtual Type BaseType => GetType();
|
||||
public IEntity Entity { get; set; }
|
||||
public virtual void OnStart(){}
|
||||
public virtual void OnAwake(){}
|
97
BITKit/Scripts/ECS/Core/GodotEntitiesService.cs
Normal file
97
BITKit/Scripts/ECS/Core/GodotEntitiesService.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using Godot;
|
||||
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;
|
||||
|
||||
public IEntity[] Query<T>() where T : IEntityComponent
|
||||
{
|
||||
return _entities.Values.Where(x => x.TryGetComponent<T>(out _)).ToArray();
|
||||
}
|
||||
|
||||
public ValueTuple<T>[] QueryComponents<T>() where T : IEntityComponent
|
||||
{
|
||||
return _entities.Values
|
||||
.Where(x => x.TryGetComponent<T>(out _))
|
||||
.Select(x =>
|
||||
{
|
||||
var component = x.Components.Single(x => x is T);
|
||||
return (T)component;
|
||||
})
|
||||
.Select(x => new ValueTuple<T>(x))
|
||||
.ToArray();
|
||||
}
|
||||
public (T, T1)[] QueryComponents<T, T1>() where T : IEntityComponent where T1 : IEntityComponent
|
||||
{
|
||||
// return _entities.Values
|
||||
// .Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _))
|
||||
// .Select(x =>
|
||||
// {
|
||||
// var component = (T)x.Components.Single(x => x is T);
|
||||
// var component1 = (T1)x.Components.Single(x => x is T1);
|
||||
// (T, T1) value = new(component, component1);
|
||||
// return value;
|
||||
// })
|
||||
// .ToArray();
|
||||
var entities = _entities.Values.Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _));
|
||||
var result = new List<(T, T1)>();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var t = (T)entity.Components.Single(x => x is T);
|
||||
var t1 = (T1)entity.Components.Single(x => x is T1);
|
||||
result.Add(new(t,t1));
|
||||
}
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public (T, T1, T2)[] QueryComponents<T, T1, T2>() where T : IEntityComponent where T1 : IEntityComponent where T2 : IEntityComponent
|
||||
{
|
||||
return _entities.Values
|
||||
.Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _) && x.TryGetComponent<T2>(out _))
|
||||
.Select(x =>
|
||||
{
|
||||
var component = (T)x.Components.Single(x => x is T);
|
||||
var component1 = (T1)x.Components.Single(x => x is T1);
|
||||
var component2 = (T2)x.Components.Single(x => x is T2);
|
||||
(T, T1, T2) value = new(component, component1, component2);
|
||||
return value;
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
using Godot;
|
||||
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;
|
||||
|
||||
public IEntity[] Query<T>() where T : IEntityComponent
|
||||
{
|
||||
return _entities.Values.Where(x => x.TryGetComponent<T>(out _)).ToArray();
|
||||
}
|
||||
}
|
6
BITKit/Scripts/ECS/Node/ConditionComponent.cs
Normal file
6
BITKit/Scripts/ECS/Node/ConditionComponent.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace BITKit;
|
||||
|
||||
public abstract partial class ConditionComponent:EntityComponent,ICondition
|
||||
{
|
||||
public abstract bool OnCheck();
|
||||
}
|
9
BITKit/Scripts/Quest/QuestComponent.cs
Normal file
9
BITKit/Scripts/Quest/QuestComponent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using BITKit;
|
||||
namespace BITKit;
|
||||
public partial class QuestComponent : EntityComponent
|
||||
{
|
||||
[Export] public string QuestName;
|
||||
[Export] public bool QuestCompleted;
|
||||
}
|
28
BITKit/Scripts/Quest/QuestService.cs
Normal file
28
BITKit/Scripts/Quest/QuestService.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Text;
|
||||
using BITKit.Core.Entites;
|
||||
namespace BITKit;
|
||||
|
||||
[Tool]
|
||||
public partial class QuestService : Node
|
||||
{
|
||||
private static IEntitiesService entitiesService => DI.Get<IEntitiesService>();
|
||||
|
||||
[Export]
|
||||
[ReadOnly]
|
||||
public string quests;
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Engine.IsEditorHint()) return;
|
||||
var stringBuilder= new StringBuilder();
|
||||
foreach (var (quest, condition) in entitiesService.QueryComponents<QuestComponent, ConditionComponent>())
|
||||
{
|
||||
quest.QuestCompleted = condition.OnCheck();
|
||||
//stringBuilder.AppendLine($"{quest.Name}:{quest.QuestCompleted?"已完成":"未完成"}");
|
||||
}
|
||||
|
||||
quests = stringBuilder.ToString();
|
||||
}
|
||||
}
|
15
BITKit/Scripts/Video/VideoPlayer.cs
Normal file
15
BITKit/Scripts/Video/VideoPlayer.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
namespace BITKit;
|
||||
public partial class VideoPlayer : VideoStreamPlayer
|
||||
{
|
||||
[Export] private bool loop;
|
||||
public override void _Ready()
|
||||
{
|
||||
if (loop)
|
||||
{
|
||||
Finished += Play;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user