mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 18:26:23 +00:00
优化属性更新逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using JNGame.Sync.Entity;
|
||||
using JNGame.Sync.Frame.Entity;
|
||||
using JNGame.Sync.Frame.Entity.Components;
|
||||
using JNGame.Sync.State.Tile.Entity.Component;
|
||||
using JNGame.Sync.System.Data;
|
||||
@@ -86,8 +87,27 @@ namespace JNGame.Sync.State.Tile.Entity
|
||||
|
||||
public abstract void TileSyncData(ISTileData data);
|
||||
|
||||
public override void OnInit(IJNContext context, ulong id = 0)
|
||||
{
|
||||
base.OnInit(context, id);
|
||||
|
||||
//如果不是Tile系统则直接拥有权限
|
||||
if (Context.GetSync() is not JNSSTileServerService)
|
||||
{
|
||||
IsHost = true;
|
||||
IsSelfCreate = true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void HostUpdate()
|
||||
{
|
||||
|
||||
//如果不是Tile系统则直接返回
|
||||
if (Context.GetSync() is not JNSSTileServerService)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool isContains = SyncTile.IsContains(Position);
|
||||
bool isHost = IsHost;
|
||||
|
||||
|
@@ -50,7 +50,7 @@ namespace JNGame.Sync.Entity
|
||||
/// </summary>
|
||||
public LVector3 Position => Transform.Position;
|
||||
|
||||
public void OnInit(IJNContext context,ulong id = 0)
|
||||
public virtual void OnInit(IJNContext context,ulong id = 0)
|
||||
{
|
||||
Context = context;
|
||||
_id = id;
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using DotRecast.Core.Collections;
|
||||
using UnityEngine;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
@@ -6,6 +7,16 @@ using NotImplementedException = System.NotImplementedException;
|
||||
namespace JNGame.Sync.System.Data
|
||||
{
|
||||
|
||||
public static class SDByteOperate
|
||||
{
|
||||
public static readonly byte[] Delete = { 0 }; //删除
|
||||
|
||||
public static bool IsDelete(byte[] value)
|
||||
{
|
||||
return value.Length == 1 && value[0] == Delete[0];
|
||||
}
|
||||
}
|
||||
|
||||
public enum SStateDataEnum
|
||||
{
|
||||
Server,
|
||||
@@ -57,7 +68,7 @@ namespace JNGame.Sync.System.Data
|
||||
|
||||
/// <summary>
|
||||
/// 状态同步的数据系统 (支持网络数据)
|
||||
/// 注意:帧同步也可以使用不过不建议 因为会有额外开销 除非你的游戏 经常在帧同步或者状态同步之间切换
|
||||
/// 注意:帧同步也可以使用不过不建议 因为会有额外开销 除非你的游戏 经常在帧同步或者状态同步之间切换 如果你要在帧同步系统中使用 则使用 ServerClient类型
|
||||
/// </summary>
|
||||
public abstract class SStateDataSystem<T> : SDataSystem<T>,ISStateDataSystem where T : ISStateData,new()
|
||||
{
|
||||
@@ -79,10 +90,10 @@ namespace JNGame.Sync.System.Data
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void OnSyncUpdate(int dt)
|
||||
{
|
||||
|
||||
while (WaitUBytes.Count > 0)
|
||||
{
|
||||
OnUByteUpdate(WaitUBytes.Dequeue());
|
||||
@@ -241,7 +252,7 @@ namespace JNGame.Sync.System.Data
|
||||
/// </summary>
|
||||
public virtual void Delete(ulong id)
|
||||
{
|
||||
UBytes[id] = SDByteOperate.DELETE;
|
||||
UBytes[id] = SDByteOperate.Delete;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -76,7 +76,6 @@ namespace JNGame.Sync.System.Data
|
||||
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)
|
||||
{
|
||||
@@ -113,6 +112,14 @@ namespace JNGame.Sync.System.Data
|
||||
|
||||
public override void OnSyncUpdate(int dt)
|
||||
{
|
||||
|
||||
//如果不是Tile系统直接调用base
|
||||
if (Sync is not JNSSTileServerService)
|
||||
{
|
||||
base.OnSyncUpdate(dt);
|
||||
return;
|
||||
}
|
||||
|
||||
while (WaitUBytes.Count > 0)
|
||||
{
|
||||
OnUByteUpdate(WaitUBytes.Dequeue());
|
||||
|
@@ -1,23 +1,11 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DotRecast.Core.Collections;
|
||||
using JNGame.Sync.Frame.Service;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
|
||||
namespace JNGame.Sync.System
|
||||
{
|
||||
|
||||
public class SDByteOperate
|
||||
{
|
||||
public static readonly byte[] DELETE = { 0 }; //删除
|
||||
|
||||
public static bool IsDelete(byte[] value)
|
||||
{
|
||||
return value.Length == 1 && value[0] == DELETE[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据接口
|
||||
/// </summary>
|
||||
@@ -76,7 +64,7 @@ namespace JNGame.Sync.System
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回最新数据 (收集最新的ISData数据 正常来讲只有服务端会运行)
|
||||
/// 返回最新数据 (收集最新的ISData数据)
|
||||
/// </summary>
|
||||
public virtual ConcurrentDictionary<ulong, T> GetLatest()
|
||||
{
|
||||
|
@@ -17,7 +17,7 @@ namespace JNGame.Sync.View
|
||||
public abstract class ViewData<T> : IViewData where T : ISData
|
||||
{
|
||||
|
||||
private Dictionary<ulong, ViewGameObject> views = new();
|
||||
protected Dictionary<ulong, ViewGameObject> views = new();
|
||||
private SViewSystem root;
|
||||
public SViewSystem Root => root;
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JNGame.Util;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Plugins.JNGame.Util
|
||||
@@ -107,6 +108,30 @@ namespace Plugins.JNGame.Util
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程分发事件 [禁止帧同步中使用主线分发事件]
|
||||
/// </summary>
|
||||
public void TryMainDispatch<T>(string eventId, T args)
|
||||
{
|
||||
UnityMainThreadDispatcher.Instance.Enqueue(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Dispatch<T>(eventId,args);
|
||||
}catch(Exception e){Debug.LogError(e.Message);}
|
||||
});
|
||||
}
|
||||
public void TryMainDispatch(string eventId)
|
||||
{
|
||||
UnityMainThreadDispatcher.Instance.Enqueue(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Dispatch(eventId);
|
||||
}catch(Exception e){Debug.LogError(e.Message);}
|
||||
});
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
|
3
JNFrame2/Assets/Scripts/Game/Data/State.meta
Normal file
3
JNFrame2/Assets/Scripts/Game/Data/State.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cecc61f462364f198db69cf70c6c557d
|
||||
timeCreated: 1726941153
|
@@ -10,6 +10,10 @@ using JNGame.Sync.System.Data;
|
||||
|
||||
namespace Game.JNGState.Logic.Data
|
||||
{
|
||||
public enum EDPlayerValueCode : int
|
||||
{
|
||||
Auth = 201
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class EDPlayerValue : GDataValue
|
||||
@@ -46,7 +50,11 @@ namespace Game.JNGState.Logic.Data
|
||||
public override void UData(EDPlayerValue data)
|
||||
{
|
||||
base.UData(data);
|
||||
if (data.Auth is not null) Value.Auth = data.Auth;
|
||||
if (data.Auth is not null)
|
||||
{
|
||||
Value.Auth = data.Auth;
|
||||
WriteUpdate((int)EDPlayerValueCode.Auth);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,17 +9,41 @@ using JNGame.Sync.System;
|
||||
using JNGame.Sync.System.Data;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
using Plugins.JNGame.Util;
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace Game.JNGState.Logic.Data
|
||||
{
|
||||
|
||||
public enum GDataValueCode : int
|
||||
{
|
||||
Position = 101
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GDataValue
|
||||
{
|
||||
public DValuePosition Position = null;
|
||||
}
|
||||
|
||||
public abstract class IGDataBase : ISTileData
|
||||
{
|
||||
|
||||
//计入修改
|
||||
public readonly ConcurrentList<int> WriteCodes = new();
|
||||
|
||||
/// <summary>
|
||||
/// 计入修改
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
public void WriteUpdate(int code)
|
||||
{
|
||||
if (WriteCodes.Contains(code)) return;
|
||||
WriteCodes.Add(code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class GDataBase<Self,T,N> : ISTileData where Self : GDataBase<Self,T,N> where T : GDataValue,new() where N : JNTileEntity
|
||||
public class GDataBase<Self,T,N> : IGDataBase where Self : GDataBase<Self,T,N> where T : GDataValue,new() where N : JNTileEntity
|
||||
{
|
||||
|
||||
public readonly T Value = new ();
|
||||
@@ -93,7 +117,11 @@ namespace Game.JNGState.Logic.Data
|
||||
/// </summary>
|
||||
public virtual void UData(T data)
|
||||
{
|
||||
if (data.Position is not null) Value.Position = data.Position;
|
||||
if (data.Position is not null)
|
||||
{
|
||||
Value.Position = data.Position;
|
||||
WriteUpdate((int)GDataValueCode.Position);
|
||||
}
|
||||
}
|
||||
|
||||
public override LVector3 GetDataPosition()
|
@@ -12,29 +12,30 @@ using UnityEngine;
|
||||
|
||||
namespace Game.JNGFrame.View.Entity
|
||||
{
|
||||
public class VDBoss : ViewData<EDBossData>
|
||||
public class VDBoss : VDEntityBasis<EDBossData>
|
||||
{
|
||||
public GameObject VWorld => App.Resource.VWorld;
|
||||
public GameObject Boss => App.Resource.Boss;
|
||||
|
||||
public VDBoss(SViewSystem root) : base(root)
|
||||
{
|
||||
Register((int)GDataValueCode.Position, OnUpdatePosition);
|
||||
}
|
||||
|
||||
public override void ViewUpdate(EDBossData data, GameObject view)
|
||||
private void OnUpdatePosition((GameObject View, EDBossData Data) tuple)
|
||||
{
|
||||
|
||||
view.name = $"Boss_{data.Id}";
|
||||
var (view, data) = tuple;
|
||||
if (data.Value.Position != null)
|
||||
{
|
||||
view.transform.DOMove(data.Value.Position.ToVector3(), 0.5f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override GameObject NewView(EDBossData data)
|
||||
{
|
||||
return Object.Instantiate(Boss, VWorld.transform);
|
||||
var view = Object.Instantiate(Boss, VWorld.transform);
|
||||
view.name = $"Boss_{data.Id}";
|
||||
return view;
|
||||
}
|
||||
|
||||
public override EDBossData[] GetData()
|
||||
|
@@ -10,28 +10,32 @@ using UnityEngine;
|
||||
|
||||
namespace Game.JNGFrame.View.Entity
|
||||
{
|
||||
public class VDNodes : ViewData<EDNodeData>
|
||||
public class VDNodes : VDEntityBasis<EDNodeData>
|
||||
{
|
||||
public GameObject VWorld => App.Resource.VWorld;
|
||||
public GameObject Player => App.Resource.Player;
|
||||
public VDNodes(SViewSystem root) : base(root)
|
||||
{
|
||||
Register((int)GDataValueCode.Position, OnUpdatePosition);
|
||||
}
|
||||
|
||||
public override void ViewUpdate(EDNodeData data, GameObject view)
|
||||
/// <summary>
|
||||
/// 更新位置
|
||||
/// </summary>
|
||||
private void OnUpdatePosition((GameObject View, EDNodeData Data) tuple)
|
||||
{
|
||||
|
||||
view.name = $"Node_{data.Id}";
|
||||
var (view, data) = tuple;
|
||||
if (data.Value.Position != null)
|
||||
{
|
||||
view.transform.DOMove(data.Value.Position.ToVector3(), 0.5f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override GameObject NewView(EDNodeData data)
|
||||
{
|
||||
return Object.Instantiate(Player, VWorld.transform);
|
||||
var view = Object.Instantiate(Player, VWorld.transform);
|
||||
view.name = $"Node_{data.Id}";
|
||||
return view;
|
||||
}
|
||||
|
||||
public override EDNodeData[] GetData()
|
||||
|
@@ -9,7 +9,7 @@ using UnityEngine;
|
||||
|
||||
namespace Game.JNGFrame.View.Entity
|
||||
{
|
||||
public class VDPlayers : ViewData<EDPlayerData>
|
||||
public class VDPlayers : VDEntityBasis<EDPlayerData>
|
||||
{
|
||||
public GameObject VWorld => App.Resource.VWorld;
|
||||
public GameObject Player => App.Resource.Player;
|
||||
@@ -17,17 +17,41 @@ namespace Game.JNGFrame.View.Entity
|
||||
|
||||
public VDPlayers(SViewSystem root) : base(root)
|
||||
{
|
||||
|
||||
Register((int)GDataValueCode.Position, OnUpdatePosition);
|
||||
Register((int)EDPlayerValueCode.Auth, OnUpdateAuth);
|
||||
|
||||
}
|
||||
|
||||
public override void ViewUpdate(EDPlayerData data, GameObject view)
|
||||
/// <summary>
|
||||
/// 更新坐标
|
||||
/// </summary>
|
||||
/// <param name="tuple"></param>
|
||||
private void OnUpdatePosition((GameObject View, EDPlayerData Data) tuple)
|
||||
{
|
||||
view.name = $"Player_{data.Id}";
|
||||
var (view, data) = tuple;
|
||||
|
||||
//更新位置
|
||||
if (data.Value.Position != null)
|
||||
{
|
||||
view.transform.DOMove(data.Value.Position.ToVector3(), 0.5f);
|
||||
}
|
||||
|
||||
if (data.Value.Auth == App.ClientID)
|
||||
{
|
||||
//更新本地玩家位置
|
||||
if (data.Value.Position != null) App.Game.GetClient<JNGTileClientSystem>()?.SetPlayerPosition(data.Value.Position.ToLVector3());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新权限
|
||||
/// </summary>
|
||||
private void OnUpdateAuth((GameObject View, EDPlayerData Data) tuple)
|
||||
{
|
||||
|
||||
var (view, data) = tuple;
|
||||
|
||||
//权限操作
|
||||
if (App.IsClient() && data.Value.Auth == App.ClientID)
|
||||
@@ -37,17 +61,14 @@ namespace Game.JNGFrame.View.Entity
|
||||
FreeLook.LookAt = view.transform;
|
||||
FreeLook.Follow = view.transform;
|
||||
|
||||
//更新玩家位置
|
||||
if (data.Value.Position != null) App.Game.GetClient<JNGTileClientSystem>()?.SetPlayerPosition(data.Value.Position.ToLVector3());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override GameObject NewView(EDPlayerData data)
|
||||
{
|
||||
var gameObject = Object.Instantiate(Player, VWorld.transform);
|
||||
// gameObject.transform.DOMove(gameObject.transform.position + new Vector3(0,0,1000), 100);
|
||||
gameObject.name = $"Player_{data.Id}";
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
|
54
JNFrame2/Assets/Scripts/Game/View/VDEntityBasis.cs
Normal file
54
JNFrame2/Assets/Scripts/Game/View/VDEntityBasis.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DotRecast.Core.Collections;
|
||||
using Game.JNGState.Logic.Data;
|
||||
using JNGame.Sync.System;
|
||||
using JNGame.Sync.View;
|
||||
using Plugins.JNGame.Util;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Game.JNGFrame.View
|
||||
{
|
||||
|
||||
public abstract class VDEntityBasis<T> : ViewData<T> where T : IGDataBase
|
||||
{
|
||||
|
||||
private readonly Dictionary<int, Action<(GameObject View, T Data)>> Event = new();
|
||||
|
||||
protected VDEntityBasis(SViewSystem root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
base.Execute();
|
||||
|
||||
//调用改动
|
||||
var dataList = GetData();
|
||||
|
||||
foreach (var data in dataList){
|
||||
|
||||
if (views.TryGetValue(data.Id,out var view))
|
||||
{
|
||||
data.WriteCodes.ForEach(code =>
|
||||
{
|
||||
Event[code]?.Invoke((view.Data,data));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void ViewUpdate(T data, GameObject view)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Register(int code,Action<(GameObject View, T Data)> action)
|
||||
{
|
||||
Event[code] = action;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
3
JNFrame2/Assets/Scripts/Game/View/VDEntityBasis.cs.meta
Normal file
3
JNFrame2/Assets/Scripts/Game/View/VDEntityBasis.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d099ea459fc642698d0c5dcdbb1fa64a
|
||||
timeCreated: 1726944240
|
Reference in New Issue
Block a user