mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 18:26:23 +00:00
修复主从交互bug
This commit is contained in:
@@ -35,7 +35,6 @@ namespace AppGame
|
||||
public static readonly JNGClientGroup Client = new JNGClientGroup();
|
||||
public static readonly JNGResService Resource = new JNGResService();
|
||||
public static readonly JNGGame Game = new JNGGame();
|
||||
public static readonly JNGSocket Socket = new JNGSocket();
|
||||
public static readonly JAPI API = new(new JAPIConfig(){BaseURL = "http://127.0.0.1:8080"});
|
||||
public static readonly GAPI GAPI = new GAPI();
|
||||
public static readonly EventDispatcher Event = new();
|
||||
@@ -50,7 +49,6 @@ namespace AppGame
|
||||
Client,
|
||||
Resource,
|
||||
Game,
|
||||
// Socket,
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -89,7 +89,7 @@ namespace AppGame.Sync
|
||||
protected override void OnSendInput(JNFrameInputs inputs)
|
||||
{
|
||||
//发送帧数据给服务端
|
||||
App.Socket.Send((int)NActionEnum.NSyncFrameInput,inputs);
|
||||
App.Business.Send((int)NActionEnum.NSyncFrameInput,inputs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -89,7 +89,7 @@ namespace AppGame.Sync
|
||||
if (inputs.Inputs.Count > 0)
|
||||
{
|
||||
//发送帧数据给服务端
|
||||
App.Socket.Send((int)NActionEnum.NSyncFrameInput,inputs);
|
||||
App.Business.Send((int)NActionEnum.NSyncFrameInput,inputs);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -74,7 +74,7 @@ namespace AppGame.Systems
|
||||
private void OnNSyncStateDataUpdate(byte[] data)
|
||||
{
|
||||
var info = JNStateItemData.Parser.ParseFrom(data);
|
||||
App.Game.AddState(info);
|
||||
App.Game.SyncState(info,true,false);
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace AppGame.Systems
|
||||
//生效全局回调
|
||||
allData.Data.Data.ForEach(child =>
|
||||
{
|
||||
App.Game.AddState(child);
|
||||
App.Game.SyncState(child,true,false);
|
||||
});
|
||||
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using AppGame.Sync;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using DotRecast.Core.Collections;
|
||||
using Game.Logic.System;
|
||||
using Google.Protobuf;
|
||||
using JNGame.Network;
|
||||
using JNGame.Sync.State.Tile;
|
||||
@@ -31,6 +32,7 @@ namespace AppGame.Systems.CServer
|
||||
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
|
||||
AddListener((int)NActionEnum.NSyncTileAllUpdate,OnNSyncTileAllUpdate);
|
||||
AddListener((int)NActionEnum.NSyncTileGetTileInfo,OnNSyncTileGetTileInfo);
|
||||
AddListener((int)NActionEnum.LocalClientDisconnect,OnLocalClientDisconnect);
|
||||
|
||||
OnInit_Game();
|
||||
|
||||
@@ -38,6 +40,7 @@ namespace AppGame.Systems.CServer
|
||||
await base.OnInit();
|
||||
}
|
||||
|
||||
|
||||
public override void OnClose()
|
||||
{
|
||||
isInit = false;
|
||||
@@ -165,5 +168,23 @@ namespace AppGame.Systems.CServer
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 有客户端断开连接
|
||||
/// </summary>
|
||||
private void OnLocalClientDisconnect(JNServerParam args)
|
||||
{
|
||||
|
||||
if (App.Game.Server is null) return;
|
||||
//只有绑定过ID 的客户端才可以执行操作
|
||||
if (!ids.ContainsKey(args.Client)) return;
|
||||
App.Game.Server.GetSystems<DGBasisSystem>().ForEach(child =>
|
||||
{
|
||||
child.OnPlayerExitServer(ids[args.Client]);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -74,7 +74,7 @@ namespace AppGame.Systems
|
||||
private void OnNSyncStateDataUpdate(byte[] data)
|
||||
{
|
||||
var info = JNStateItemData.Parser.ParseFrom(data);
|
||||
App.Game.AddState(info);
|
||||
App.Game.SyncState(info,false,true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,7 +91,7 @@ namespace AppGame.Systems
|
||||
//生效全局状态
|
||||
allData.Data.Data.ForEach(child =>
|
||||
{
|
||||
App.Game.AddState(child);
|
||||
App.Game.SyncState(child,false,true);
|
||||
});
|
||||
|
||||
}
|
||||
|
@@ -112,24 +112,30 @@ namespace AppGame.Systems
|
||||
/// <summary>
|
||||
/// 接收状态数据
|
||||
/// </summary>
|
||||
public void AddState(JNStateItemData frame)
|
||||
public void SyncState(JNStateItemData frame,bool isSyncClient,bool isSyncServer)
|
||||
{
|
||||
var message = new Dictionary<ulong, byte[]>();
|
||||
foreach (var data in frame.Messages)
|
||||
{
|
||||
message.Add(data.Key,data.Value.Data.ToByteArray());
|
||||
}
|
||||
|
||||
client?.GetSystems<ISStateDataSystem>().ForEach(child =>
|
||||
|
||||
if (isSyncClient)
|
||||
{
|
||||
if (child.NetID != frame.NetID) return;
|
||||
child.OnInsertUBytes(message);
|
||||
});
|
||||
server?.GetSystems<ISStateDataSystem>().ForEach(child =>
|
||||
client?.GetSystems<ISStateDataSystem>().ForEach(child =>
|
||||
{
|
||||
if (child.NetID != frame.NetID) return;
|
||||
child.OnInsertUBytes(message);
|
||||
});
|
||||
}
|
||||
if (isSyncServer)
|
||||
{
|
||||
if (child.NetID != frame.NetID) return;
|
||||
child.OnInsertUBytes(message);
|
||||
});
|
||||
server?.GetSystems<ISStateDataSystem>().ForEach(child =>
|
||||
{
|
||||
if (child.NetID != frame.NetID) return;
|
||||
child.OnInsertUBytes(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -8,6 +8,8 @@ namespace Game.JNGFrame.Logic.Entity
|
||||
{
|
||||
|
||||
public override IContext[] allContexts { get; } = { new EDNodeContext(),new EDPlayerContext(),new EDBossContext(), };
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -2,6 +2,8 @@
|
||||
using JNGame.Sync.Frame.Service;
|
||||
using JNGame.Sync.State.Tile;
|
||||
using JNGame.Sync.State.Tile.Entity;
|
||||
using JNGame.Sync.System.Data;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
|
||||
namespace Game.Logic.Entity
|
||||
{
|
||||
@@ -24,7 +26,17 @@ namespace Game.Logic.Entity
|
||||
}
|
||||
return LVector3.Down;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void TileSyncData(ISTileData data)
|
||||
{
|
||||
Transform.Position = data.GetDataPosition();
|
||||
}
|
||||
|
||||
public override void OnTileSlaveExit()
|
||||
{
|
||||
base.OnTileSlaveExit();
|
||||
//从服务器 - 实体移出后立即删除
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
}
|
@@ -34,6 +34,10 @@ namespace Game.JNGFrame.Logic.Entity.Controller
|
||||
|
||||
public override void OnSyncStart()
|
||||
{
|
||||
|
||||
//Player 同步到从服务器
|
||||
TileEntity.IsSyncSlave = true;
|
||||
|
||||
base.OnSyncStart();
|
||||
Move.Speed = LFloat.L10;
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Game.JNGFrame.Logic.Entity.Controller;
|
||||
using Entitas;
|
||||
using Game.JNGFrame.Logic.Entity.Controller;
|
||||
using Game.Logic.Entity.Nodes;
|
||||
using Game.Logic.Entity.Nodes.Component.Components;
|
||||
using JNGame.Sync.State.Tile.Entity;
|
||||
@@ -7,6 +8,7 @@ namespace Game.JNGFrame.Logic.Entity.Contexts
|
||||
{
|
||||
public sealed partial class EDPlayerContext : JNTileContext<EDPlayer>
|
||||
{
|
||||
|
||||
protected override EDPlayer BindComponent(EDPlayer entity)
|
||||
{
|
||||
base.BindComponent(entity);
|
||||
@@ -14,5 +16,6 @@ namespace Game.JNGFrame.Logic.Entity.Contexts
|
||||
entity.AddComponent<EDPlayerController>();
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -13,12 +13,6 @@ namespace Game.Logic.Entity.Nodes
|
||||
public EDMoveComponent Move => CLookup.Query<EDMoveComponent>(this);
|
||||
public EDBossController Controller => CLookup.Query<EDBossController>(this);
|
||||
|
||||
public override void TileSyncData(ISTileData data)
|
||||
{
|
||||
var nodeData = data as EDBossData;
|
||||
Transform.Position = nodeData.Value.Position.ToLVector3();
|
||||
}
|
||||
|
||||
public override JNEntityLookup NewCLookup()
|
||||
{
|
||||
return new EDBoosLookup();
|
||||
|
@@ -12,13 +12,6 @@ namespace Game.Logic.Entity.Nodes
|
||||
|
||||
public EDMoveComponent Move => CLookup.Query<EDMoveComponent>(this);
|
||||
|
||||
|
||||
public override void TileSyncData(ISTileData data)
|
||||
{
|
||||
var nodeData = data as EDNodeData;
|
||||
Transform.Position = nodeData.Value.Position.ToLVector3();
|
||||
}
|
||||
|
||||
public override JNEntityLookup NewCLookup()
|
||||
{
|
||||
return new EDNodeLookup();
|
||||
|
@@ -16,9 +16,9 @@ namespace Game.Logic.Entity.Nodes
|
||||
|
||||
public override void TileSyncData(ISTileData data)
|
||||
{
|
||||
base.TileSyncData(data);
|
||||
var nodeData = data as EDPlayerData;
|
||||
Controller.AuthBind(nodeData.Value.Auth.Value);
|
||||
Transform.Position = nodeData.Value.Position.ToLVector3();
|
||||
}
|
||||
|
||||
public override JNEntityLookup NewCLookup()
|
||||
|
@@ -30,5 +30,11 @@ namespace Game.Logic.System
|
||||
return LVector3.Down;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [状态同步生命周期] 玩家离开服务器 不在游戏线程中执行
|
||||
/// </summary>
|
||||
public virtual void OnPlayerExitServer(int auth){}
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
using DotRecast.Core.Collections;
|
||||
using Game.Input;
|
||||
using Game.JNGFrame.Logic.Entity;
|
||||
using Game.JNGFrame.Logic.Entity.Contexts;
|
||||
using Game.Logic.Entity.Nodes;
|
||||
using JNGame.Math;
|
||||
@@ -40,5 +39,20 @@ namespace Game.Logic.System.Logic
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override void OnPlayerExitServer(int auth)
|
||||
{
|
||||
base.OnPlayerExitServer(auth);
|
||||
|
||||
NodeContext.GetHostEntities().ForEach(child =>
|
||||
{
|
||||
if (child.Controller.Auth == auth)
|
||||
{
|
||||
child.DelayDestroy();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -14,6 +14,9 @@ namespace Game.JNGFrame.View.Entity
|
||||
public GameObject VWorld => App.Resource.VWorld;
|
||||
public GameObject Player => App.Resource.Player;
|
||||
public CinemachineFreeLook FreeLook => App.Resource.FreeLook;
|
||||
|
||||
//本地玩家 视图
|
||||
private GameObject LocalView;
|
||||
|
||||
public VDPlayers(SViewSystem root) : base(root)
|
||||
{
|
||||
@@ -67,9 +70,20 @@ namespace Game.JNGFrame.View.Entity
|
||||
|
||||
public override GameObject NewView(EDPlayerData data)
|
||||
{
|
||||
var gameObject = Object.Instantiate(Player, VWorld.transform);
|
||||
gameObject.name = $"Player_{data.Id}";
|
||||
return gameObject;
|
||||
GameObject view;
|
||||
//如果这个角色是自己 则 直接拿自己的 View
|
||||
if (App.IsClient() && data.Value.Auth == App.ClientID)
|
||||
{
|
||||
if (LocalView is null) LocalView = Object.Instantiate(Player, VWorld.transform);
|
||||
view = LocalView;
|
||||
}
|
||||
else
|
||||
{
|
||||
view = Object.Instantiate(Player, VWorld.transform);
|
||||
}
|
||||
|
||||
view.name = $"Player_{data.Id}";
|
||||
return view;
|
||||
}
|
||||
|
||||
public override EDPlayerData[] GetData()
|
||||
@@ -80,7 +94,10 @@ namespace Game.JNGFrame.View.Entity
|
||||
public override void ViewRemove(GameObject view)
|
||||
{
|
||||
view.transform.DOKill();
|
||||
Object.Destroy(view);
|
||||
if (LocalView != view)
|
||||
{
|
||||
Object.Destroy(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user