修复主从交互bug

This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2024-09-23 03:50:27 +08:00
parent ca64b708ac
commit b65b860b98
55 changed files with 233 additions and 119 deletions

View File

@@ -26,6 +26,6 @@ namespace JNGame.Sync.State.Tile.Entity.Component
public virtual void OnTileEnter(){}
public virtual void OnTileExit(){}
public void OnTileSlaveExit(){}
}
}

View File

@@ -125,6 +125,7 @@ namespace JNGame.Sync.State.Tile.Entity
if (isHost && !isContains)
{
OnTileExit();
if (SyncTile.IsSlave) OnTileSlaveExit();
}
}
@@ -147,5 +148,13 @@ namespace JNGame.Sync.State.Tile.Entity
}
}
public virtual void OnTileSlaveExit()
{
//给组件生命周期
foreach (var component in GetComponents())
{
(component as JNTileComponent)?.OnTileSlaveExit();
}
}
}
}

View File

@@ -13,5 +13,10 @@
/// </summary>
public void OnTileExit();
/// <summary>
/// 从服务器 - 退出当前区块
/// </summary>
public void OnTileSlaveExit();
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using DotRecast.Core.Collections;
using Entitas;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Component.Components;
@@ -17,18 +18,20 @@ namespace JNGame.Sync.Frame.Entity
//方便查抄的实体Map
public Dictionary<ulong, T> Entities = new ();
public JNContext(): base((new T()).NewCLookup().Count, () => new T())
public JNContext()
: this((new T()).NewCLookup().Count, () => new T())
{
CLookup = (new T()).NewCLookup();
}
public JNContext(int totalComponents, Func<T> entityFactory) : base(totalComponents, entityFactory)
public JNContext(int totalComponents, Func<T> entityFactory) : this(totalComponents, 0, null, null, entityFactory)
{
}
public JNContext(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func<IEntity, IAERC> aercFactory, Func<T> entityFactory) : base(totalComponents, startCreationIndex, contextInfo, aercFactory, entityFactory)
public JNContext(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func<IEntity, IAERC> aercFactory, Func<T> entityFactory)
: base(totalComponents, startCreationIndex, contextInfo, aercFactory, entityFactory)
{
CLookup = (new T()).NewCLookup();
}
public JNSyncService GetSync()
@@ -96,8 +99,20 @@ namespace JNGame.Sync.Frame.Entity
BindLifeCycle(entity);
return entity;
}
//生效延迟销毁
public void DelayDestroy()
{
GetEntities().ForEach(child =>
{
if (child.IsDelayDestroy)
{
child.Destroy();
}
});
}
//帧同步 生命周期
//生命周期
public virtual void OnSyncStart(){}
public virtual void OnSyncUpdate(int dt)
@@ -122,6 +137,7 @@ namespace JNGame.Sync.Frame.Entity
public abstract void InitReference(JNSyncService data);
public void AddEntity(ulong id,JNEntity entity);
public void RemoveEntity(ulong id);
public void DelayDestroy();
}

View File

@@ -19,12 +19,18 @@ namespace JNGame.Sync.Entity
}
//给所有实体推帧
public void Simulate()
public virtual void Simulate()
{
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.OnSyncUpdate(Sync.DeltaTime);
}
//延迟销毁
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.DelayDestroy();
}
}
public T GetContext<T>() where T : IContext

View File

@@ -50,6 +50,8 @@ namespace JNGame.Sync.Entity
/// </summary>
public LVector3 Position => Transform.Position;
public bool IsDelayDestroy { get; private set; } = false;
public virtual void OnInit(IJNContext context,ulong id = 0)
{
Context = context;
@@ -127,12 +129,18 @@ namespace JNGame.Sync.Entity
public override void Destroy()
{
//清理缓存
Context.RemoveEntity(Id);
OnSyncDestroy();
RemoveAllComponents();
base.Destroy();
}
public void DelayDestroy()
{
IsDelayDestroy = true;
}
//生命周期
public virtual void OnSyncStart(){}
@@ -146,7 +154,11 @@ namespace JNGame.Sync.Entity
}
public virtual void OnSyncDestroy()
{}
{
_id = 0;
IsDelayDestroy = false;
}
}
}

View File

@@ -148,7 +148,6 @@ namespace JNGame.Sync.System.Data
if (NodeContext.Query(child.Key) is null)
{
Delete(child.Key);
return;
}
//主动更新
@@ -179,7 +178,7 @@ namespace JNGame.Sync.System.Data
{
var entity = NodeContext.Query(keyValue.Key);
//给从服务器发送数据
if (IsMaster && entity is not null && entity.IsSyncSlave)
if (IsMaster && ((entity is not null && entity.IsSyncSlave) || keyValue.Value == SDByteOperate.Delete))
{
slave[keyValue.Key] = keyValue.Value;
}