53 lines
1.3 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using Entitas;
using JNGame.Sync.Frame.Entity;
namespace JNGame.Sync.Entity
{
public class JNContexts : Entitas.IContexts
{
public virtual IContext[] allContexts => Array.Empty<IContext>();
public JNSyncService Sync;
public JNContexts()
{
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.OnSyncStart();
}
}
//给所有实体推帧
public void Simulate()
{
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.OnSyncUpdate();
}
}
public T GetContext<T>() where T : IContext
{
for (int i = 0; i < allContexts.Length; i++)
{
if (allContexts[i] is T) return (T)allContexts[i];
}
throw new InvalidOperationException($"No context of type {typeof(T).Name} was found.");
}
//注入 JNSyncFrameService
public void InitReference(JNSyncService data)
{
Sync = data;
foreach (var context in allContexts)
{
(context as IJNContext)?.InitReference(data);
}
}
}
}