提交bug 艰难先这样

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-22 20:37:39 +08:00
parent 5858bd2407
commit 8932528f5e
118 changed files with 5271 additions and 4939 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity;
using JNGame.Sync.Frame.Entity.Components;
using UnityEngine;
namespace JNGame.Sync.State.Tile.Entity
{
@@ -44,6 +45,17 @@ namespace JNGame.Sync.State.Tile.Entity
public T TileSyncCreate(long id)
{
//判断是否有这个Id实体
foreach (var data in GetEntities())
{
if (data.Id == id)
{
Debug.Log("重复Id实体创建");
return null;
}
}
var entity = NewEntity();
entity.OnInit(this,id);
entity.IsHost = false;

View File

@@ -26,55 +26,10 @@ namespace JNGame.Sync.State.Tile
/// 区块大小
/// </summary>
protected abstract int TileSize { get; }
public bool IsTileIndex((int X, int Y) xTuple)
{
if (xTuple.X >= 0 && xTuple.Y >= 0)
{
return xTuple.Y < Tiles.Length && xTuple.X < Tiles[0].Length;
}
return false;
}
public int GetTileIndex(LVector3 pos)
{
(int x, int y) = GetXYIndex(pos);
return Tiles[y][x];
}
public (int X, int Y) GetXYIndex(LVector3 pos)
{
// 遍历数组
for (int y = 0; y < Tiles.Length; y++)
{
for (int x = 0; x < Tiles[y].Length; x++)
{
// 检查当前元素是否非零
if (Tiles[y][x] != 0)
{
//判断是否所在区块
var min = new LVector2(x.ToLFloat() * TileSize,y.ToLFloat() * TileSize);
var max = new LVector2((x + 1).ToLFloat() * TileSize,(y + 1).ToLFloat() * TileSize);
// 假设LVector2是一个包含X和Y属性的结构体或类
// 检查X坐标是否在范围内
if (pos.x < min.x || pos.x >= max.x)
{
continue; // X坐标不在范围内
}
// 检查Y坐标是否在范围内
if (pos.z < min.y || pos.z >= max.y)
{
continue; // Y坐标不在范围内
}
return (x,y);
}
}
}
return (0,0);
return JNSSTileTool.GetTileIndex(Tiles, TileSize, pos);
}
/// <summary>
@@ -83,23 +38,7 @@ namespace JNGame.Sync.State.Tile
/// <returns></returns>
public List<int> GetTileGridIndex(LVector3 pos)
{
(int x, int y) = GetXYIndex(pos);
List<int> grid = new List<int>();
// 填充九宫格
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int tempX = x + i;
int tempY = y + j; // 注意这里j+1+1是因为数组第二维存储的是y坐标
if (IsTileIndex((tempX,tempY))) grid.Add(Tiles[tempY][tempX]);
}
}
return grid;
return JNSSTileTool.GetTileGridIndex(Tiles, TileSize,pos);
}
public void AddTileShow(int index)

View File

@@ -70,7 +70,7 @@ namespace JNGame.Sync.State.Tile
{
//根据区块设置Id 起始值
var random = base.CreateRandom();
random.SetIdValue(100000000000L * TID);
random.SetIdValue(100000000000L * TID,(100000000000L * (TID + 1) - 1));
return random;
}
@@ -78,15 +78,6 @@ namespace JNGame.Sync.State.Tile
{
return new JNTileContexts();
}
public bool IsTileIndex((int X, int Y) xTuple)
{
if (xTuple.X >= 0 && xTuple.Y >= 0)
{
return xTuple.Y < Tiles.Length && xTuple.X < Tiles[0].Length;
}
return false;
}
/// <summary>
/// 更新区块范围
@@ -110,6 +101,11 @@ namespace JNGame.Sync.State.Tile
}
}
public int GetTileIndex(LVector3 pos)
{
return JNSSTileTool.GetTileIndex(Tiles, TileSize, pos);
}
/// <summary>
/// 判断位置是否在区块内
@@ -117,16 +113,20 @@ namespace JNGame.Sync.State.Tile
/// <param name="pos"></param>
/// <returns></returns>
public bool IsContains(LVector3 position)
{
return IsContains(position,MaxContains,MinContains);
}
public bool IsContains(LVector3 position,LVector3 Max,LVector3 Min)
{
// 假设LVector2是一个包含X和Y属性的结构体或类
// 检查X坐标是否在范围内
if (position.x < MinContains.x || position.x >= MaxContains.x)
if (position.x < Min.x || position.x >= Max.x)
{
return false; // X坐标不在范围内
}
// 检查Y坐标是否在范围内
if (position.z < MinContains.y || position.z >= MaxContains.y)
if (position.z < Min.y || position.z >= Max.y)
{
return false; // Y坐标不在范围内
}
@@ -141,35 +141,7 @@ namespace JNGame.Sync.State.Tile
/// <returns></returns>
public (LVector2 Max,LVector2 Min) GetTileContains(int index)
{
(int X, int Y) = GetTileIDXY(index);
var min = new LVector2(X.ToLFloat() * TileSize,Y.ToLFloat() * TileSize);
var max = new LVector2((X + 1).ToLFloat() * TileSize,(Y + 1).ToLFloat() * TileSize);
return (max,min);
}
/// <summary>
/// 获取TileID X Y
/// </summary>
/// <returns></returns>
public (int X, int Y) GetTileIDXY(int index)
{
// 遍历数组
for (int y = 0; y < Tiles.Length; y++)
{
for (int x = 0; x < Tiles[y].Length; x++)
{
// 检查当前元素是否非零
if (Tiles[y][x] != 0 && Tiles[y][x] == index)
{
// 返回找到的坐标
return (x,y);
}
}
}
throw new Exception();
return JNSSTileTool.GetTileContains(Tiles,TileSize,index);
}
/// <summary>
@@ -178,22 +150,7 @@ namespace JNGame.Sync.State.Tile
/// <returns></returns>
public List<int> GetTileGridIndex(int index)
{
List<int> grid = new List<int>();
(int X, int Y) = GetTileIDXY(index);
// 填充九宫格
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int tempX = X + i;
int tempY = Y + j; // 注意这里j+1+1是因为数组第二维存储的是y坐标
if (IsTileIndex((tempX,tempY))) grid.Add(Tiles[tempY][tempX]);
}
}
return grid;
return JNSSTileTool.GetTileGridIndex(Tiles,TileSize,index);
}
/// <summary>

View File

@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using JNGame.Math;
namespace JNGame.Sync.State.Tile
{
public class JNSSTileTool
{
public static bool IsTileIndex(int[][] Tiles,(int X, int Y) xTuple)
{
if (xTuple.X >= 0 && xTuple.Y >= 0)
{
return xTuple.Y < Tiles.Length && xTuple.X < Tiles[0].Length;
}
return false;
}
/// <summary>
/// 获取TileID X Y
/// </summary>
/// <returns></returns>
public static (int X, int Y) GetTileIDXY(int[][] Tiles,int index)
{
// 遍历数组
for (int y = 0; y < Tiles.Length; y++)
{
for (int x = 0; x < Tiles[y].Length; x++)
{
// 检查当前元素是否非零
if (Tiles[y][x] != 0 && Tiles[y][x] == index)
{
// 返回找到的坐标
return (x,y);
}
}
}
throw new Exception();
}
/// <summary>
/// 根据TileId 获取最大最小范围
/// </summary>
/// <returns></returns>
public static (LVector2 Max,LVector2 Min) GetTileContains(int[][] Tiles, int TileSize,int index)
{
(int X, int Y) = GetTileIDXY(Tiles,index);
var min = new LVector2(X.ToLFloat() * TileSize,Y.ToLFloat() * TileSize);
var max = new LVector2((X + 1).ToLFloat() * TileSize,(Y + 1).ToLFloat() * TileSize);
return (max,min);
}
/// <summary>
/// 获取九宫格Index
/// </summary>
/// <returns></returns>
public static List<int> GetTileGridIndex(int[][] Tiles, int TileSize, (int X, int Y) xTuple)
{
List<int> grid = new List<int>();
// 填充九宫格
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int tempX = xTuple.X + i;
int tempY = xTuple.Y + j; // 注意这里j+1+1是因为数组第二维存储的是y坐标
if (IsTileIndex(Tiles,(tempX,tempY))) grid.Add(Tiles[tempY][tempX]);
}
}
return grid;
}
public static List<int> GetTileGridIndex(int[][] Tiles, int TileSize, int index)
{
return GetTileGridIndex(Tiles,TileSize,GetTileIDXY(Tiles,index));
}
public static List<int> GetTileGridIndex(int[][] Tiles,int TileSize,LVector3 pos)
{
return GetTileGridIndex(Tiles,TileSize,GetXYIndex(Tiles,TileSize,pos));
}
/// <summary>
/// 获取Index
/// </summary>
/// <param name="Tiles"></param>
/// <param name="TileSize"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static int GetTileIndex(int[][] Tiles,int TileSize,LVector3 pos)
{
(int x, int y) = JNSSTileTool.GetXYIndex(Tiles,TileSize,pos);
return Tiles[y][x];
}
/// <summary>
/// 获取XY
/// </summary>
/// <param name="Tiles"></param>
/// <param name="TileSize"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static (int X, int Y) GetXYIndex(int[][] Tiles,int TileSize,LVector3 pos)
{
// 遍历数组
for (int y = 0; y < Tiles.Length; y++)
{
for (int x = 0; x < Tiles[y].Length; x++)
{
// 检查当前元素是否非零
if (Tiles[y][x] != 0)
{
//判断是否所在区块
var min = new LVector2(x.ToLFloat() * TileSize,y.ToLFloat() * TileSize);
var max = new LVector2((x + 1).ToLFloat() * TileSize,(y + 1).ToLFloat() * TileSize);
// 假设LVector2是一个包含X和Y属性的结构体或类
// 检查X坐标是否在范围内
if (pos.x < min.x || pos.x >= max.x)
{
continue; // X坐标不在范围内
}
// 检查Y坐标是否在范围内
if (pos.z < min.y || pos.z >= max.y)
{
continue; // Y坐标不在范围内
}
return (x,y);
}
}
}
return (0,0);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 44baac66240645d887c8996de748dd0c
timeCreated: 1724315108

View File

@@ -58,6 +58,10 @@ namespace JNGame.Sync.Entity
{
_id = GetSystem<JNRandomSystem>().NextId();
}
else
{
GetSystem<JNRandomSystem>().AdaptId(id);
}
}
public abstract JNEntityLookup NewCLookup();

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using DotRecast.Core.Collections;
using NotImplementedException = System.NotImplementedException;
namespace JNGame.Sync.System.Data
{
@@ -41,7 +42,16 @@ namespace JNGame.Sync.System.Data
//网络Id (用于确定网络通讯时找到这个数据系统)
public int NetID { get; }
/// <summary>
/// 插入字节
/// </summary>
public void OnInsertUBytes(Dictionary<long, byte[]> bytes);
/// <summary>
/// 获取全部字节
/// </summary>
public Dictionary<long, byte[]> GetDataBytes();
}
/// <summary>
@@ -118,34 +128,57 @@ namespace JNGame.Sync.System.Data
//提交数据更新
OnUByteUpdate(bytes);
}
/// <summary>
/// 获取全部字节
/// </summary>
public Dictionary<long, byte[]> GetDataBytes()
{
var data = new Dictionary<long, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
data[child.Key] = child.Value.GetByte();
});
}
return data;
}
/// <summary>
/// 将UByte提交更新
/// </summary>
public virtual void OnUByteUpdate(Dictionary<long, byte[]> bytes)
{
foreach (var info in bytes)
lock (Data)
{
if (SDByteOperate.IsDelete(info.Value))
foreach (var info in bytes)
{
Data.Remove(info.Key);
}
else
{
if (Data.TryGetValue(info.Key, out var value))
if (SDByteOperate.IsDelete(info.Value))
{
value.UByte(info.Value);
Data.Remove(info.Key);
}
else
{
Data[info.Key] = NewObject(info.Key,info.Value);
}
if (Data.TryGetValue(info.Key, out var value))
{
value.UByte(info.Value);
}
else
{
Data[info.Key] = NewObject(info.Key,info.Value);
}
}
}
}
}
/// <summary>
@@ -196,6 +229,6 @@ namespace JNGame.Sync.System.Data
{
UBytes[id] = SDByteOperate.DELETE;
}
}
}

View File

@@ -1,25 +1,68 @@
using System.Collections.Generic;
using DotRecast.Core.Collections;
using JNGame.Math;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Components;
using JNGame.Sync.State.Tile;
using JNGame.Sync.State.Tile.Entity;
using NotImplementedException = System.NotImplementedException;
namespace JNGame.Sync.System.Data
{
public interface ISTileDataSystem
{
/// <summary>
/// 清除指定区域数据
/// </summary>
public void ClearTileData(int index);
/// <summary>
/// 获取有权限的全部字节
/// </summary>
public Dictionary<long, byte[]> GetHostDataBytes();
/// <summary>
/// 获取指定区块的全部字节
/// </summary>
public Dictionary<long, byte[]> GetTileDataBytes(int index);
}
public abstract class ISTileData : ISStateData
{
public abstract bool IsHost { get; }
public JNTileEntity Entity;
/// <summary>
/// 绑定实体到数据 (销毁数据时顺带销毁实体)
/// </summary>
/// <param name="entity"></param>
public void BindEntity(JNTileEntity entity)
{
Entity = entity;
}
/// <summary>
/// 获取数据位置(用于区块清除)
/// </summary>
public abstract LVector3 GetDataPosition();
}
/// <summary>
/// 支持区块的数据类
/// </summary>
public abstract class STileDataSystem<T,E> : SStateDataSystem<T> where T : ISTileData,new() where E : JNTileEntity, new()
public abstract class STileDataSystem<T,E> : SStateDataSystem<T>,ISTileDataSystem where T : ISTileData,new() where E : JNTileEntity, new()
{
public abstract JNTileContext<E> NodeContext { get; }
protected STileDataSystem(SStateDataEnum type) : base(type)
{
@@ -40,7 +83,12 @@ namespace JNGame.Sync.System.Data
protected virtual void OnDataSyncContext()
{
var lIsTileData = new Dictionary<long, T>(Data);
Dictionary<long, T> lIsTileData;
lock (Data)
{
lIsTileData = new Dictionary<long, T>(Data);
}
NodeContext.GetEntities().ForEach(child =>
{
@@ -60,7 +108,9 @@ namespace JNGame.Sync.System.Data
foreach (var keyValue in lIsTileData)
{
var entity = NodeContext.TileSyncCreate(keyValue.Key);
entity.TileSyncData(keyValue.Value);
entity?.TileSyncData(keyValue.Value);
//将实体绑定到数据中
keyValue.Value.BindEntity(entity);
}
}
@@ -76,6 +126,94 @@ namespace JNGame.Sync.System.Data
if (!data.IsHost) return;
base.Add(data);
}
/// <summary>
/// 判断数据是否在区块内
/// </summary>
public bool IsTileInside(int tileId,T data)
{
var index = -1;
if (Sync is JNSSTileClientService clientService)
{
index = clientService.GetTileIndex(data.GetDataPosition());
}
if (Sync is JNSSTileServerService serverService)
{
index = serverService.GetTileIndex(data.GetDataPosition());
}
return index == tileId;
}
public void ClearTileData(int index)
{
lock (Data)
{
//需要删除的数据Id
var ids = new List<long>();
Data.ForEach(child =>
{
if (IsTileInside(index,child.Value)) ids.Add(child.Key);
});
//删除数据和实体
ids.ForEach(child =>
{
//销毁实体
Data[child].Entity?.Destroy();
//销毁数据
Data.Remove(child);
});
}
}
public Dictionary<long, byte[]> GetHostDataBytes()
{
var data = new Dictionary<long, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (child.Value.IsHost)
{
data[child.Key] = child.Value.GetByte();
}
});
}
return data;
}
public Dictionary<long, byte[]> GetTileDataBytes(int index)
{
var data = new Dictionary<long, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (IsTileInside(index,child.Value))
{
data[child.Key] = child.Value.GetByte();
}
});
}
return data;
}
}
}

View File

@@ -17,6 +17,8 @@ namespace JNGame.Sync.Frame.Service
//Id
private long _id = 0;
private long _idMin = long.MinValue;
private long _idMax = long.MaxValue;
public JNRandomSystem(int seed)
{
@@ -44,13 +46,30 @@ namespace JNGame.Sync.Frame.Service
return ++_id;
}
public void SetIdValue(long id)
public void SetIdValue(long min,long max)
{
if (_id < id)
if (_id < min)
{
_id = id;
_id = min;
}
_idMin = min;
_idMax = max;
}
/// <summary>
/// 适配Id 用于 历史Id和新Id重复问题
/// </summary>
/// <param name="id"></param>
/// <exception cref="NotImplementedException"></exception>
public void AdaptId(long id)
{
if (_idMin <= id && id >= _idMax)
{
if (id > _id)
{
_id = id;
}
}
}
}
}