142 lines
4.8 KiB
C#
Raw Normal View History

2024-08-22 20:37:39 +08:00
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);
}
}
}