JisolGame/JNFrame2/Assets/JNGame/Sync/App/Tile/JNSSTileClientService.cs

122 lines
3.5 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
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);
}
}
}
}