This commit is contained in:
CortexCore
2023-11-02 20:58:36 +08:00
parent 3beceb1645
commit 7712c80804
75 changed files with 552 additions and 417 deletions

View File

@@ -73,7 +73,13 @@ public partial class FreeLookCamera : Node3D,IVirtualCamera
switch (@event)
{
case InputEventMouseMotion mouseMotion:
if(Input.IsMouseButtonPressed(MouseButton.Middle) is false)break;
switch (OS.GetName())
{
case "Windows":
if(Input.IsMouseButtonPressed(MouseButton.Middle) is false)return;
break;
}
if (isMoving)
{
var velocity = mouseMotion.Relative;
@@ -88,6 +94,14 @@ public partial class FreeLookCamera : Node3D,IVirtualCamera
else
{
var mouseVelocity = mouseMotion.Relative /* 0.022f*/ * 1.81f * (float)GetProcessDeltaTime();
switch (OS.GetName())
{
case "Android":
mouseVelocity *= 0.1f;
break;
}
euler.X -= mouseVelocity.Y;
euler.Y -= mouseVelocity.X;
euler.Y %= 360;

View File

@@ -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)
{

View 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()
{
}
}

View File

@@ -1,5 +1,5 @@
using System;
using BITKit.Core.Entites;
using BITKit.Entities;
using Godot;
using Microsoft.Extensions.DependencyInjection;

View File

@@ -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)

View File

@@ -1,7 +1,7 @@
using Godot;
using System;
using System.Text;
using BITKit.Core.Entites;
using BITKit.Entities;
namespace BITKit;
[Tool]

View File

@@ -0,0 +1,18 @@
using Godot;
using System;
namespace BITKit.UX;
public partial class UXProfiler : Node
{
[Export] private Label fpsLabel;
[Export] private Label resolutionLabel;
public override void _Process(double delta)
{
var windowSize = DisplayServer.WindowGetSize();
if (fpsLabel is not null)
fpsLabel.Text = $"FPS: {Engine.GetFramesPerSecond()}";
if (resolutionLabel is not null)
resolutionLabel.Text = $"Resolution: {windowSize.X}x{windowSize.Y}";
}
}

View File

@@ -2,6 +2,7 @@ using Godot;
using System;
using System.Threading;
using BITKit.Packages.Core.LazyLoad;
using HttpClient = System.Net.Http.HttpClient;
namespace BITKit;
public partial class HttpGet : Node,IActivable
@@ -18,51 +19,47 @@ public partial class HttpGet : Node,IActivable
[Signal]
public delegate void OnGetEventHandler(string httpContext);
/// <summary>
/// 最大并行请求数量
/// </summary>
private readonly LimitTimes limitConcurrent =new (1);
/// <summary>
/// 请求数据的间隔
/// </summary>
private readonly IntervalTimer _intervalTimer = new(1);
/// <summary>
/// http客户端
/// </summary>
private readonly ServiceLoader<System.Net.Http.HttpClient> httpClient=new();
private readonly HttpClient httpClient=new();
/// <summary>
/// 取消令牌用于取消Http Get
/// </summary>
private CancellationToken _cancellationToken;
private bool allowNextRequest = true;
public override void _Ready()
{
_cancellationToken = new CancellationToken();
}
/// <summary>
/// 物理帧用于控制并发和间隔的同时请求数据
/// </summary>
/// <param name="delta"></param>
public override void _PhysicsProcess(double delta)
{
if(Enabled is false)return;
if (Enabled is false) return;
//等待依赖加载
//请求间隔控制+请求并发控制
if (_intervalTimer.Allow is false || httpClient.IsLoaded is false) return;
if (_intervalTimer.Allow is false) return;
//如果url为空
if (string.IsNullOrEmpty(url)) return;
if (!limitConcurrent.AllowOnly) return;
//提交并发
limitConcurrent.CanUpdate();
//发送请求
Request();
if (allowNextRequest)
Request();
}
private async void Request()
{
allowNextRequest = false;
//获取json
try
{
var json = await httpClient.Value.GetStringAsync(url, _cancellationToken);
var json = await httpClient.GetStringAsync(url, _cancellationToken);
//取消执行,如果已取消令牌
_cancellationToken.ThrowIfCancellationRequested();
@@ -72,14 +69,11 @@ public partial class HttpGet : Node,IActivable
catch (InvalidOperationException)
{
BIT4Log.Warning(url);
//返回并发数量
limitConcurrent.Release();
}
catch (OperationCanceledException)
{
//返回并发数量
limitConcurrent.Release();
}
allowNextRequest = true;
}
public void SetActive(bool active) => Enabled=active;