mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
188 lines
4.7 KiB
C#
188 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Entitas;
|
|
using JNGame.Runtime.Util;
|
|
using JNGame.Sync.Entity;
|
|
using JNGame.Sync.Frame.Service;
|
|
using JNGame.Sync.System;
|
|
using UnityEngine;
|
|
|
|
namespace JNGame.Runtime.Sync
|
|
{
|
|
public abstract class JNSyncDefaultService : JNSyncService
|
|
{
|
|
|
|
private SBaseSystem[] _allSystems;
|
|
protected override JNBaseSystem[] AllSystems => _allSystems;
|
|
|
|
public JNContexts Contexts;
|
|
|
|
public abstract bool IsStartGame { get; }
|
|
|
|
private Thread thread;
|
|
|
|
//执行时间
|
|
protected int TickTime;
|
|
|
|
|
|
public JNSyncDefaultService() : base()
|
|
{
|
|
var main = UnityMainThreadDispatcher.Instance;
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
base.Cleanup();
|
|
thread?.Abort();
|
|
thread = null;
|
|
}
|
|
|
|
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>
|
|
/// 更新(不可在外部调用)
|
|
/// </summary>
|
|
public override void Execute()
|
|
{
|
|
|
|
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
|
|
if (paused) return;
|
|
#endif
|
|
UnityMainThreadDispatcher.Instance.Enqueue(base.Execute);
|
|
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
|
|
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;
|
|
}
|
|
|
|
/// <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;
|
|
_syncSystems[i].OnSyncUpdate(DeltaTime);
|
|
double time1 = (DateTime.Now.Ticks - time) / 10000f;
|
|
syncSystemInfos[i].AddSyncUpdateDuration(time1);
|
|
|
|
}
|
|
#else
|
|
_syncSystems[i].OnSyncUpdate(DeltaTime);
|
|
#endif
|
|
}
|
|
|
|
//给实体推帧
|
|
Contexts.Simulate();
|
|
|
|
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
|
|
SyncDuration = (DateTime.Now.Ticks - timeSimulate) / 10000f;
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|