using System; using System.Collections.Generic; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using JNGame.Math; namespace JNGame.Sync.State.Tile { /// /// 瓦片状态同步客户端 /// public abstract class JNSSTileClientService : JNSStateClientService { /// /// 当前显示的Tile /// protected List TileShow = new (); /// /// 区块索引组 /// protected abstract int[][] Tiles { get; } /// /// 区块大小 /// 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); } /// /// 获取九宫格Index /// /// public List GetTileGridIndex(LVector3 pos) { (int x, int y) = GetXYIndex(pos); List grid = new List(); // 填充九宫格 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); } } } }