106 lines
2.7 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using Entitas;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Component.Components;
using JNGame.Sync.Frame.Entity.Components;
using JNGame.Sync.System;
namespace JNGame.Sync.Frame.Entity
{
public abstract class JNContext<T> : Entitas.Context<T>,IJNContext where T : JNEntity, new()
{
public JNSyncService Sync { get; private set; }
public JNEntityLookup CLookup;
public JNContext(): base((new T()).NewCLookup().Count, () => new T())
{
CLookup = (new T()).NewCLookup();
}
public JNContext(int totalComponents, Func<T> entityFactory) : base(totalComponents, entityFactory)
{
}
public JNContext(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func<IEntity, IAERC> aercFactory, Func<T> entityFactory) : base(totalComponents, startCreationIndex, contextInfo, aercFactory, entityFactory)
{
}
public JNSyncService GetSync()
{
return Sync;
}
public void InitReference(JNSyncService data)
{
Sync = data;
}
public T GetService<T>() where T : SLogicSystem
{
return Sync.GetSystem<T>();
}
protected T NewEntity()
{
T entity = base.CreateEntity();
//绑定组件查询器
entity.CLookup = CLookup;
return entity;
}
protected virtual T BindInitialize(T entity)
{
entity.OnInit(this);
return entity;
}
protected virtual T BindComponent(T entity)
{
entity.AddComponent<JNTransformComponent>();
return entity;
}
protected virtual T BindLifeCycle(T entity)
{
entity.OnSyncStart();
return entity;
}
public sealed override T CreateEntity()
{
var entity = NewEntity();
BindInitialize(entity);
BindComponent(entity);
BindLifeCycle(entity);
return entity;
}
//帧同步 生命周期
public virtual void OnSyncStart(){}
public virtual void OnSyncUpdate()
{
//给实体推帧
foreach (var entity in GetEntities())
{
if (entity.isEnabled)
{
entity.OnSyncUpdate();
}
}
}
public virtual void OnSyncDestroy()
{}
}
public interface IJNContext : IJNSyncCycle
{
public abstract JNSyncService GetSync();
public abstract void InitReference(JNSyncService data);
}
}