188 lines
4.7 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using System.Collections.Generic;
2024-09-12 18:06:22 +08:00
using System.Threading;
2024-08-17 14:27:18 +08:00
using Entitas;
2024-10-17 01:59:25 +08:00
using JNGame.Runtime.Util;
2024-08-17 14:27:18 +08:00
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Service;
using JNGame.Sync.System;
using UnityEngine;
2024-10-17 01:59:25 +08:00
namespace JNGame.Runtime.Sync
2024-08-17 14:27:18 +08:00
{
public abstract class JNSyncDefaultService : JNSyncService
{
private SBaseSystem[] _allSystems;
protected override JNBaseSystem[] AllSystems => _allSystems;
public JNContexts Contexts;
public abstract bool IsStartGame { get; }
2024-09-12 18:06:22 +08:00
private Thread thread;
//执行时间
protected int TickTime;
2024-08-17 14:27:18 +08:00
public JNSyncDefaultService() : base()
{
2024-09-13 04:06:25 +08:00
var main = UnityMainThreadDispatcher.Instance;
2024-08-17 14:27:18 +08:00
}
public override void Initialize()
{
base.Initialize();
Contexts = CreateContexts();
Contexts.Sync = this;
Contexts.InitReference(this);
var systems = new List<SBaseSystem>();
systems.Add(CreateRandom());
systems.AddRange(NewLogicSystems());
systems.AddRange(NewDataSystems());
systems.AddRange(NewViewSystems());
_allSystems = systems.ToArray();
foreach (var system in _allSystems)
{
system.Sync = this;
system.Contexts = Contexts;
Add(system);
}
Debug.Log("Initialize");
}
2024-09-12 18:06:22 +08:00
public void Dispose()
{
base.Cleanup();
thread?.Abort();
thread = null;
}
2024-08-17 14:27:18 +08:00
public override Systems Add(ISystem system)
{
(system as SLogicSystem)?.OnSyncStart();
return base.Add(system);
}
public virtual JNContexts CreateContexts()
{
return new JNContexts();
}
//随机数系统
public virtual JNRandomSystem CreateRandom()
{
return new JNRandomSystem(1000);
}
public virtual SLogicSystem[] NewLogicSystems()
{
return Array.Empty<SLogicSystem>();
}
public virtual SDataSystemBase[] NewDataSystems()
{
return Array.Empty<SDataSystemBase>();
}
public virtual SViewSystem[] NewViewSystems()
{
return Array.Empty<SViewSystem>();
}
/// <summary>
2024-09-12 18:06:22 +08:00
/// 更新(不可在外部调用)
2024-08-17 14:27:18 +08:00
/// </summary>
public override void Execute()
{
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
if (paused) return;
#endif
2024-09-12 18:06:22 +08:00
UnityMainThreadDispatcher.Instance.Enqueue(base.Execute);
2024-08-17 14:27:18 +08:00
}
2024-09-12 18:06:22 +08:00
/// <summary>
/// Unity主线程更新
/// </summary>
/// <param name="ms"></param>
public void MExecute(int ms = -1)
{
if (ms <= -1) ms = (int)Time.deltaTime * 1000;
TickTime = ms;
Execute();
}
/// <summary>
/// 线程更新
/// </summary>
public void TStartExecute(int ms = -1)
{
2024-09-13 04:06:25 +08:00
2024-09-12 18:06:22 +08:00
if (ms <= -1) ms = DeltaTime;
thread = new Thread(() =>
{
while (thread.ThreadState != ThreadState.Aborted)
{
Thread.Sleep(ms);
TickTime = ms;
Execute();
}
});
thread.Start();
}
public void TStopExecute()
{
thread?.Abort();
thread = null;
}
2024-08-17 14:27:18 +08:00
/// <summary>
/// 推逻辑帧
/// </summary>
public void Simulate()
{
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
SyncDuration = 0;
var timeSimulate = DateTime.Now.Ticks;
#endif
for (int i = 0; i < _syncSystems.Count; i++)
{
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
if (syncSystemInfos[i].isActive){
var time = DateTime.Now.Ticks;
2024-08-31 21:05:29 +08:00
_syncSystems[i].OnSyncUpdate(DeltaTime);
2024-08-17 14:27:18 +08:00
double time1 = (DateTime.Now.Ticks - time) / 10000f;
syncSystemInfos[i].AddSyncUpdateDuration(time1);
}
#else
2024-08-31 21:05:29 +08:00
_syncSystems[i].OnSyncUpdate(DeltaTime);
2024-08-17 14:27:18 +08:00
#endif
}
//给实体推帧
Contexts.Simulate();
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
SyncDuration = (DateTime.Now.Ticks - timeSimulate) / 10000f;
#endif
}
}
}