using System;
using System.Collections.Generic;
using System.Threading;
using Entitas;
using JNGame.Runtime.Util;
using JNGame.Sync;
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();
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 IJNSyncCycle)?.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();
}
public virtual SDataSystemBase[] NewDataSystems()
{
return Array.Empty();
}
public virtual SViewSystem[] NewViewSystems()
{
return Array.Empty();
}
///
/// 更新(不可在外部调用)
///
public override void Execute()
{
#if (!ENTITAS_DISABLE_VISUAL_DEBUGGING && UNITY_EDITOR)
if (paused) return;
#endif
UnityMainThreadDispatcher.Instance.Enqueue(base.Execute);
}
///
/// Unity主线程更新
///
///
public void MExecute(int ms = -1)
{
if (ms <= -1) ms = (int)(Time.deltaTime * 1000);
TickTime = ms;
Execute();
}
///
/// 线程更新
///
public void TStartExecute(int ms = -1)
{
if (ms <= -1) ms = DeltaTime;
thread = new Thread(() =>
{
while (thread is not null && thread.ThreadState != ThreadState.Aborted)
{
Thread.Sleep(ms);
TickTime = ms;
if(thread is not null) Execute();
}
});
thread.Start();
}
public void TStopExecute()
{
thread?.Abort();
thread = null;
}
///
/// 推逻辑帧
///
protected virtual 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
}
}
}