using System; using System.Collections.Generic; 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 : Entitas.Context,IJNContext where T : JNEntity, new() { public JNSyncService Sync { get; private set; } public JNEntityLookup CLookup; //方便查抄的实体Map public Dictionary Entities = new (); public JNContext(): base((new T()).NewCLookup().Count, () => new T()) { CLookup = (new T()).NewCLookup(); } public JNContext(int totalComponents, Func entityFactory) : base(totalComponents, entityFactory) { } public JNContext(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func aercFactory, Func entityFactory) : base(totalComponents, startCreationIndex, contextInfo, aercFactory, entityFactory) { } public JNSyncService GetSync() { return Sync; } public void InitReference(JNSyncService data) { Sync = data; } public T Query(ulong id) { Entities.TryGetValue(id, out var entity); return entity; } public void AddEntity(ulong id, JNEntity entity) { Entities.Add(id,entity as T); } public void RemoveEntity(ulong id) { Entities.Remove(id); } public T GetService() where T : SLogicSystem { return Sync.GetSystem(); } 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(); 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); public void AddEntity(ulong id,JNEntity entity); public void RemoveEntity(ulong id); } }