Init
This commit is contained in:
56
Scripts/Core/BITAppForGodot.cs
Normal file
56
Scripts/Core/BITAppForGodot.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using HttpClient = System.Net.Http.HttpClient;
|
||||
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
/// 为Godot提供的BITApp加载服务
|
||||
/// </summary>
|
||||
public partial class BITAppForGodot : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖服务集合
|
||||
/// </summary>
|
||||
public static ServiceCollection ServiceCollection { get; private set; } = new();
|
||||
/// <summary>
|
||||
/// 依赖服务提供接口
|
||||
/// </summary>
|
||||
public static ServiceProvider ServiceProvider { get; private set; }
|
||||
/// <summary>
|
||||
/// 在构造函数中注册Logger
|
||||
/// </summary>
|
||||
public BITAppForGodot()
|
||||
{
|
||||
BIT4Log.OnLog += GD.Print;
|
||||
BIT4Log.OnWarning += GD.PushWarning;
|
||||
BIT4Log.OnNextLine += () => GD.Print();
|
||||
BIT4Log.OnException += x=>GD.PrintErr(x.ToString());
|
||||
|
||||
//启动BITApp
|
||||
BITApp.Start();
|
||||
BIT4Log.Log<BITAppForGodot>("已创建BITApp");
|
||||
}
|
||||
|
||||
public override async void _Ready()
|
||||
{
|
||||
BIT4Log.Log<BITAppForGodot>("正在创建BITWebApp");
|
||||
|
||||
//添加测试用HttpClient
|
||||
ServiceCollection.AddSingleton<HttpClient>();
|
||||
|
||||
//构造依赖服务提供接口
|
||||
ServiceProvider = ServiceCollection.BuildServiceProvider();
|
||||
|
||||
}
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
#pragma warning disable CS4014
|
||||
//停止BITApp
|
||||
BITApp.Stop();
|
||||
#pragma warning restore CS4014
|
||||
BIT4Log.Log<BITAppForGodot>("已安全退出App");
|
||||
}
|
||||
}
|
56
Scripts/Core/IntervalTimer.cs
Normal file
56
Scripts/Core/IntervalTimer.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Godot;
|
||||
|
||||
namespace BITKit;
|
||||
|
||||
/// <summary>
|
||||
/// 固定间隔工具,用于控制执行速率
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class IntervalTimer
|
||||
{
|
||||
public IntervalTimer()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 在构造函数中声明间隔时间
|
||||
/// </summary>
|
||||
/// <param name="interval"></param>
|
||||
public IntervalTimer(ulong interval)
|
||||
{
|
||||
this.interval = interval;
|
||||
}
|
||||
/// <summary>
|
||||
/// 间隔时间
|
||||
/// </summary>
|
||||
private readonly ulong interval;
|
||||
/// <summary>
|
||||
/// 可以执行的事件
|
||||
/// </summary>
|
||||
private ulong allowTime;
|
||||
/// <summary>
|
||||
/// 是否可以执行(执行重置间隔)
|
||||
/// </summary>
|
||||
public bool Allow
|
||||
{
|
||||
get
|
||||
{
|
||||
if (allowTime >= Time.GetTicksMsec()) return false;
|
||||
Release();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否可以执行(不会执行重置间隔)
|
||||
/// </summary>
|
||||
public bool AllowWithoutRelease => allowTime >= Time.GetTicksMsec();
|
||||
/// <summary>
|
||||
/// 重置执行间隔
|
||||
/// </summary>
|
||||
/// <param name="immediately">是否可以立即执行</param>
|
||||
public void Release(bool immediately=false)
|
||||
{
|
||||
var currentTime = Time.GetTicksMsec();
|
||||
allowTime = immediately ? currentTime : currentTime + interval*1000;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user