mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交Unity 联机Pro
This commit is contained in:
3
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity.meta
Normal file
3
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea749c86d5e64d3d95b47485fb1e93b8
|
||||
timeCreated: 1722324674
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 248fceb6707f455e85a5c9e3994518af
|
||||
timeCreated: 1722334056
|
@@ -0,0 +1,29 @@
|
||||
using JNGame.Sync.Entity.Component;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
|
||||
namespace JNGame.Sync.State.Tile.Entity.Component
|
||||
{
|
||||
/// <summary>
|
||||
/// 拥有区块的组件
|
||||
/// </summary>
|
||||
public class JNTileComponent : JNComponent,IJNTileCycle
|
||||
{
|
||||
|
||||
public bool IsHost {
|
||||
get
|
||||
{
|
||||
if (Entity is IJNTileEntity entity)
|
||||
{
|
||||
return entity.IsHost;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnTileEnter(){}
|
||||
|
||||
public virtual void OnTileExit(){}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca6de04e46e04e6ebb4f4bb7de345e66
|
||||
timeCreated: 1722334062
|
79
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity/JNTileContext.cs
Normal file
79
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity/JNTileContext.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JNGame.Sync.Entity;
|
||||
using JNGame.Sync.Frame.Entity;
|
||||
using JNGame.Sync.Frame.Entity.Components;
|
||||
|
||||
namespace JNGame.Sync.State.Tile.Entity
|
||||
{
|
||||
|
||||
public class JNTileContext<T> : JNContext<T> where T : JNTileEntity, new()
|
||||
{
|
||||
|
||||
private JNSSTileServerService SyncTile => base.Sync as JNSSTileServerService;
|
||||
|
||||
public override void OnSyncUpdate()
|
||||
{
|
||||
foreach (var entity in base.GetEntities())
|
||||
{
|
||||
|
||||
//生命周期
|
||||
bool isContains = SyncTile.IsContains(entity.Position);
|
||||
bool isHost = entity.IsHost;
|
||||
entity.IsHost = isContains;
|
||||
|
||||
//区块进入生命周期
|
||||
if (!isHost && isContains)
|
||||
{
|
||||
entity.OnTileEnter();
|
||||
}
|
||||
//区块移出生命周期
|
||||
if (isHost && !isContains)
|
||||
{
|
||||
entity.OnTileExit();
|
||||
}
|
||||
|
||||
//判断实体是否在所属区块 在则 更新
|
||||
if (entity.IsHost)
|
||||
{
|
||||
entity.OnSyncUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public T TileSyncCreate(long id)
|
||||
{
|
||||
var entity = NewEntity();
|
||||
entity.OnInit(this,id);
|
||||
entity.IsHost = false;
|
||||
BindComponent(entity);
|
||||
BindLifeCycle(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 有权限的实体
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public T[] GetHostEntities()
|
||||
{
|
||||
if (_entitiesCache is null) return Array.Empty<T>();
|
||||
var items = new List<T>();
|
||||
foreach (var item in _entitiesCache)
|
||||
{
|
||||
if (item.IsHost)
|
||||
{
|
||||
items.Add(item);
|
||||
}
|
||||
}
|
||||
return items.ToArray();
|
||||
}
|
||||
|
||||
protected override T BindInitialize(T entity)
|
||||
{
|
||||
entity.IsHost = true;
|
||||
return base.BindInitialize(entity);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a7efa2f5ec14741bf38d07ca72008fc
|
||||
timeCreated: 1722325979
|
@@ -0,0 +1,15 @@
|
||||
using Entitas;
|
||||
using JNGame.Sync.Entity;
|
||||
|
||||
namespace JNGame.Sync.State.Tile.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 区块实体容器集合
|
||||
/// </summary>
|
||||
public class JNTileContexts : JNContexts
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 469128164a4343638bbf231f78a97ea0
|
||||
timeCreated: 1722325937
|
55
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity/JNTileEntity.cs
Normal file
55
JNFrame2/Assets/JNGame/Sync/App/Tile/Entity/JNTileEntity.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using JNGame.Sync.Entity;
|
||||
using JNGame.Sync.Frame.Entity.Components;
|
||||
using JNGame.Sync.State.Tile.Entity.Component;
|
||||
using JNGame.Sync.System.Data;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
|
||||
namespace JNGame.Sync.State.Tile.Entity
|
||||
{
|
||||
|
||||
public interface IJNTileEntity
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 是否所属当前区块
|
||||
/// </summary>
|
||||
public bool IsHost { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 区块同步属性(通过网络同步过来的实体数据)
|
||||
/// </summary>
|
||||
public void TileSyncData(ISTileData data);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 支持区块的实体类 拥有区块生命周期
|
||||
/// </summary>
|
||||
public abstract class JNTileEntity : JNEntity,IJNTileCycle,IJNTileEntity
|
||||
{
|
||||
|
||||
public bool IsHost { get; set; } = false;
|
||||
|
||||
public abstract void TileSyncData(ISTileData data);
|
||||
|
||||
//区块生命周期
|
||||
public virtual void OnTileEnter()
|
||||
{
|
||||
//给组件生命周期
|
||||
foreach (var component in GetComponents())
|
||||
{
|
||||
(component as JNTileComponent)?.OnTileEnter();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnTileExit()
|
||||
{
|
||||
//给组件生命周期
|
||||
foreach (var component in GetComponents())
|
||||
{
|
||||
(component as JNTileComponent)?.OnTileExit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18ef2e720b964a5a89d45a4ee27ef234
|
||||
timeCreated: 1722324716
|
17
JNFrame2/Assets/JNGame/Sync/App/Tile/IJNTileCycle.cs
Normal file
17
JNFrame2/Assets/JNGame/Sync/App/Tile/IJNTileCycle.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace JNGame.Sync.State.Tile
|
||||
{
|
||||
public interface IJNTileCycle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 进入当前区块
|
||||
/// </summary>
|
||||
public void OnTileEnter();
|
||||
|
||||
/// <summary>
|
||||
/// 退出当前区块
|
||||
/// </summary>
|
||||
public void OnTileExit();
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ffa11c9e0cfd4fd18a10a15e1a52e282
|
||||
timeCreated: 1722325335
|
122
JNFrame2/Assets/JNGame/Sync/App/Tile/JNSSTileClientService.cs
Normal file
122
JNFrame2/Assets/JNGame/Sync/App/Tile/JNSSTileClientService.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using JNGame.Math;
|
||||
|
||||
namespace JNGame.Sync.State.Tile
|
||||
{
|
||||
/// <summary>
|
||||
/// 瓦片状态同步客户端
|
||||
/// </summary>
|
||||
public abstract class JNSSTileClientService : JNSStateClientService
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 当前显示的Tile
|
||||
/// </summary>
|
||||
protected List<int> TileShow = new ();
|
||||
|
||||
/// <summary>
|
||||
/// 区块索引组
|
||||
/// </summary>
|
||||
protected abstract int[][] Tiles { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 区块大小
|
||||
/// </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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取九宫格Index
|
||||
/// </summary>
|
||||
/// <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;
|
||||
|
||||
}
|
||||
|
||||
public void AddTileShow(int index)
|
||||
{
|
||||
if (!(TileShow.Contains(index)))
|
||||
{
|
||||
TileShow.Add(index);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveTileShow(int index)
|
||||
{
|
||||
if (TileShow.Contains(index))
|
||||
{
|
||||
TileShow.Remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a0304ca48534abb92b8e6537ae8397d
|
||||
timeCreated: 1722493209
|
215
JNFrame2/Assets/JNGame/Sync/App/Tile/JNSSTileServerService.cs
Normal file
215
JNFrame2/Assets/JNGame/Sync/App/Tile/JNSSTileServerService.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using JNGame.Math;
|
||||
using JNGame.Sync.Entity;
|
||||
using JNGame.Sync.Frame.Service;
|
||||
using JNGame.Sync.State.Tile.Entity;
|
||||
using UnityEngine;
|
||||
|
||||
namespace JNGame.Sync.State.Tile
|
||||
{
|
||||
/// <summary>
|
||||
/// 瓦片状态同步 : 用于开放世界类型的游戏 将 世界分多个Tile 不同的Service管理
|
||||
/// </summary>
|
||||
public abstract partial class JNSSTileServerService : JNSStateServerService
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 区块索引组
|
||||
/// </summary>
|
||||
protected abstract int[][] Tiles { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 区块大小
|
||||
/// </summary>
|
||||
protected abstract int TileSize { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 区块ID
|
||||
/// 用于管理当前 Service 负责的区块ID
|
||||
/// </summary>
|
||||
public int TID { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 区块最大最小位置
|
||||
/// </summary>
|
||||
public LVector2 MinContains{ get; private set; }
|
||||
public LVector2 MaxContains{ get; private set; }
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
OnInit();
|
||||
}
|
||||
|
||||
protected virtual async Task OnInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
//获取权限
|
||||
this.TID = await FetchTileId();
|
||||
|
||||
//更新范围
|
||||
UpdateContains();
|
||||
base.Initialize();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError(e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override JNContexts CreateContexts()
|
||||
{
|
||||
return CreateTileContexts();
|
||||
}
|
||||
|
||||
public sealed override JNRandomSystem CreateRandom()
|
||||
{
|
||||
//根据区块设置Id 起始值
|
||||
var random = base.CreateRandom();
|
||||
random.SetIdValue(100000000000L * TID);
|
||||
return random;
|
||||
}
|
||||
|
||||
protected virtual JNTileContexts CreateTileContexts()
|
||||
{
|
||||
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>
|
||||
/// 更新区块范围
|
||||
/// </summary>
|
||||
public void UpdateContains()
|
||||
{
|
||||
MinContains = new LVector2();
|
||||
MaxContains = new LVector2();
|
||||
|
||||
try
|
||||
{
|
||||
//更新区块最大最小位置
|
||||
(LVector2 max, LVector2 min) = GetTileContains(TID);
|
||||
MinContains = min;
|
||||
MaxContains = max;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// ignored
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断位置是否在区块内
|
||||
/// </summary>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsContains(LVector3 position)
|
||||
{
|
||||
// 假设LVector2是一个包含X和Y属性的结构体或类
|
||||
// 检查X坐标是否在范围内
|
||||
if (position.x < MinContains.x || position.x >= MaxContains.x)
|
||||
{
|
||||
return false; // X坐标不在范围内
|
||||
}
|
||||
|
||||
// 检查Y坐标是否在范围内
|
||||
if (position.z < MinContains.y || position.z >= MaxContains.y)
|
||||
{
|
||||
return false; // Y坐标不在范围内
|
||||
}
|
||||
|
||||
// 如果X和Y坐标都在范围内,则返回true
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据TileId 获取最大最小范围
|
||||
/// </summary>
|
||||
/// <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();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取九宫格Index
|
||||
/// </summary>
|
||||
/// <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;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 TileId 权限 (返回多少 Service 则管理所属Tile)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract UniTask<int> FetchTileId();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前连接的区块
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual int[] GetLinkTiles()
|
||||
{
|
||||
return Array.Empty<int>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3489829ddd184475a96364d243ea97b8
|
||||
timeCreated: 1722240437
|
Reference in New Issue
Block a user