提交新概念 Tile从服务器

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-31 15:35:12 +08:00
parent 77db4d7d71
commit d67032e1de
1039 changed files with 57738 additions and 412 deletions

View File

@@ -20,6 +20,8 @@ namespace JNGame.Sync.State.Tile.Entity.Component
return false;
}
}
public IJNTileEntity TileEntity => Entity as IJNTileEntity;
public virtual void OnTileEnter(){}

View File

@@ -70,9 +70,8 @@ namespace JNGame.Sync.State.Tile.Entity
/// <returns></returns>
public T[] GetHostEntities()
{
if (_entitiesCache is null) return Array.Empty<T>();
var items = new List<T>();
foreach (var item in _entitiesCache)
foreach (var item in GetEntities())
{
if (item.IsHost)
{

View File

@@ -11,9 +11,19 @@ namespace JNGame.Sync.State.Tile.Entity
{
/// <summary>
/// 是否所属当前区块
/// 是否有权限
/// </summary>
public bool IsHost { get; set; }
/// <summary>
/// 是否将数据同步到从服务器
/// </summary>
public bool IsSyncSlave { get; set; }
/// <summary>
/// 是否将数据同步到主服务器
/// </summary>
public bool IsSyncMaster { get; set; }
/// <summary>
/// 区块同步属性(通过网络同步过来的实体数据)
@@ -28,8 +38,21 @@ namespace JNGame.Sync.State.Tile.Entity
public abstract class JNTileEntity : JNEntity,IJNTileCycle,IJNTileEntity
{
/// <summary>
/// 是否有权限
/// </summary>
public bool IsHost { get; set; } = false;
/// <summary>
/// 是否将数据同步到从服务器
/// </summary>
public bool IsSyncSlave { get; set; } = false;
/// <summary>
/// 是否将数据同步到主服务器
/// </summary>
public bool IsSyncMaster { get; set; } = false;
public abstract void TileSyncData(ISTileData data);
//区块生命周期

View File

@@ -0,0 +1,20 @@
namespace JNGame.Sync.State.Tile
{
public enum TileMasterSlaveEnum
{
Master,
Slave,
}
/// <summary>
/// 瓦片状态同步 : 主从服务器代码
/// </summary>
public abstract partial class JNSSTileServerService : JNSStateServerService
{
public abstract TileMasterSlaveEnum MSRole { get; }
public bool IsMaster => MSRole == TileMasterSlaveEnum.Master;
public bool IsSlave => MSRole == TileMasterSlaveEnum.Slave;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b9fc15e9ea9f4f46b05ef0f56aa0423f
timeCreated: 1724726167

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Entitas;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Component.Components;
@@ -13,6 +14,9 @@ namespace JNGame.Sync.Frame.Entity
public JNSyncService Sync { get; private set; }
public JNEntityLookup CLookup;
//方便查抄的实体Map
public Dictionary<ulong, T> Entities = new ();
public JNContext(): base((new T()).NewCLookup().Count, () => new T())
{
@@ -36,7 +40,23 @@ namespace JNGame.Sync.Frame.Entity
{
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<T>() where T : SLogicSystem
{
return Sync.GetSystem<T>();
@@ -100,7 +120,9 @@ namespace JNGame.Sync.Frame.Entity
{
public abstract JNSyncService GetSync();
public abstract void InitReference(JNSyncService data);
public void AddEntity(ulong id,JNEntity entity);
public void RemoveEntity(ulong id);
}
}

View File

@@ -58,6 +58,7 @@ namespace JNGame.Sync.Entity
{
_id = GetSystem<JNRandomSystem>().NextId();
}
Context.AddEntity(Id,this);
}
public abstract JNEntityLookup NewCLookup();
@@ -126,6 +127,7 @@ namespace JNGame.Sync.Entity
public override void Destroy()
{
Context.RemoveEntity(Id);
OnSyncDestroy();
RemoveAllComponents();
base.Destroy();

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using AppGame.Sync;
using DotRecast.Core.Collections;
using JNGame.Math;
using JNGame.Sync.Entity;
@@ -21,13 +23,17 @@ namespace JNGame.Sync.System.Data
/// <summary>
/// 获取有权限的全部字节
/// </summary>
public Dictionary<ulong, byte[]> GetHostDataBytes();
public Dictionary<ulong, byte[]> GetHostDataBytes(Func<JNTileEntity,bool> filter = null);
/// <summary>
/// 获取有权限的全部字节 (过滤条件 : 需同步从服务器)
/// </summary>
public Dictionary<ulong, byte[]> GetHostDataBytesFilterSlave(Func<JNTileEntity,bool> filter = null);
/// <summary>
/// 获取指定区块的全部字节
/// </summary>
public Dictionary<ulong, byte[]> GetTileDataBytes(int index);
public Dictionary<ulong, byte[]> GetTileDataBytes(int index,Func<JNTileEntity,bool> filter = null);
@@ -35,16 +41,17 @@ namespace JNGame.Sync.System.Data
public abstract class ISTileData : ISStateData
{
public abstract bool IsHost { get; }
public JNTileEntity Entity;
/// <summary>
/// 绑定实体到数据 (销毁数据时顺带销毁实体)
/// 绑定实体到数据
/// </summary>
/// <param name="entity"></param>
public void BindEntity(JNTileEntity entity)
public virtual void BindEntity(JNTileEntity entity)
{
Entity = entity;
Id = entity.Id;
}
/// <summary>
@@ -61,7 +68,10 @@ namespace JNGame.Sync.System.Data
{
public abstract JNTileContext<E> NodeContext { get; }
public JNSSTileServerService TileSync => Sync as JNSSTileServerService;
public bool IsMaster => TileSync is not null && TileSync.IsMaster;
public bool IsSlave => TileSync is not null && TileSync.IsSlave;
protected STileDataSystem(SStateDataEnum type) : base(type)
@@ -76,7 +86,42 @@ namespace JNGame.Sync.System.Data
OnDataSyncContext();
}
}
public override void OnSendUBytes(Dictionary<ulong, byte[]> bytes)
{
Dictionary<ulong, byte[]> all = bytes;
Dictionary<ulong, byte[]> master = new Dictionary<ulong, byte[]>();
Dictionary<ulong, byte[]> slave = new Dictionary<ulong, byte[]>();
all.ForEach(keyValue =>
{
var entity = NodeContext.Query(keyValue.Key);
//给从服务器发送数据
if (IsMaster && entity is not null && entity.IsSyncSlave)
{
slave[keyValue.Key] = keyValue.Value;
}
});
OnSendAllData(all);
OnSendMasterData(master);
OnSendSlaveData(slave);
}
/// <summary>
/// 发送玩家数据
/// </summary>
public abstract void OnSendAllData(Dictionary<ulong, byte[]> bytes);
/// <summary>
/// 发送主服务器数据
/// </summary>
public virtual void OnSendMasterData(Dictionary<ulong, byte[]> bytes){}
/// <summary>
/// 发送从服务器数据
/// </summary>
public virtual void OnSendSlaveData(Dictionary<ulong, byte[]> bytes){}
/// <summary>
/// 将数据Data同步到Context
/// </summary>
@@ -118,12 +163,14 @@ namespace JNGame.Sync.System.Data
//只更新有权限的实体
public override void Update(T data)
{
if (!data.IsHost) return;
var entity = NodeContext.Query(data.Id);
if (entity is null || !entity.IsHost) return;
base.Update(data);
}
public override void Add(T data)
{
if (!data.IsHost) return;
var entity = NodeContext.Query(data.Id);
if (entity is null || !entity.IsHost) return;
base.Add(data);
}
@@ -175,16 +222,27 @@ namespace JNGame.Sync.System.Data
}
public Dictionary<ulong, byte[]> GetHostDataBytes()
public Dictionary<ulong, byte[]> GetHostDataBytesFilterSlave(Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
return GetHostDataBytes(entity => entity.IsSyncSlave && filter(entity) );
}
public Dictionary<ulong, byte[]> GetHostDataBytes(Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary<ulong, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (child.Value.IsHost)
var entity = NodeContext.Query(child.Key);
if (entity is not null && entity.IsHost && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}
@@ -195,16 +253,19 @@ namespace JNGame.Sync.System.Data
}
public Dictionary<ulong, byte[]> GetTileDataBytes(int index)
public Dictionary<ulong, byte[]> GetTileDataBytes(int index,Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary<ulong, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (IsTileInside(index,child.Value))
var entity = NodeContext.Query(child.Key);
if (IsTileInside(index,child.Value) && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}