mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
177 lines
4.9 KiB
C#
177 lines
4.9 KiB
C#
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>
|
||
/// 随机数大小(100000000000UL * RandomSize)
|
||
/// </summary>
|
||
public int RandomSize { get; protected set; } = 1;
|
||
|
||
/// <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(100000000000UL * (ulong)RandomSize,(100000000000UL * ((ulong)RandomSize + 1) - 1));
|
||
return random;
|
||
}
|
||
|
||
protected virtual JNTileContexts CreateTileContexts()
|
||
{
|
||
return new JNTileContexts();
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
|
||
}
|
||
|
||
public int GetTileIndex(LVector3 pos)
|
||
{
|
||
return JNSSTileTool.GetTileIndex(Tiles, TileSize, pos);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断位置是否在区块内
|
||
/// </summary>
|
||
/// <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 < Min.x || position.x >= Max.x)
|
||
{
|
||
return false; // X坐标不在范围内
|
||
}
|
||
|
||
// 检查Y坐标是否在范围内
|
||
if (position.z < Min.y || position.z >= Max.y)
|
||
{
|
||
return false; // Y坐标不在范围内
|
||
}
|
||
|
||
// 如果X和Y坐标都在范围内,则返回true
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据TileId 获取最大最小范围
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public (LVector2 Max,LVector2 Min) GetTileContains(int index)
|
||
{
|
||
return JNSSTileTool.GetTileContains(Tiles,TileSize,index);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取九宫格Index
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public List<int> GetTileGridIndex(int index)
|
||
{
|
||
return JNSSTileTool.GetTileGridIndex(Tiles,TileSize,index);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取 TileId 权限 (返回多少 Service 则管理所属Tile)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected abstract UniTask<int> FetchTileId();
|
||
|
||
/// <summary>
|
||
/// 获取当前连接的区块
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public virtual int[] GetLinkTiles()
|
||
{
|
||
return Array.Empty<int>();
|
||
}
|
||
|
||
}
|
||
} |