using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class GridTool { public Data[,] array; public readonly int wide; public readonly int high; /// 初始化网格 public GridTool(int wide, int high, Func generate) { this.wide = wide; this.high = high; array = new Data[wide, high]; Loop((x, y) => { array[x, y] = generate(x, y); }); } /// 循环网格获取x和y public void Loop(Action action) { for (int y = 0; y < high; y++) { for (int x = 0; x < wide; x++) { action?.Invoke(x, y); } } } /// 校验xy是否超限 public bool TryXY(int x, int y) { return x >= 0 && x < wide && y >= 0 && y < high; } /// 强制写入数据,超过边界时写在边界 public Data Get(int x, int y) { x = Mathf.Clamp(x, 0, wide - 1); y = Mathf.Clamp(y, 0, high - 1); return array[x, y]; } /// 强制读取数据,超过边界时读取边界 public void Set(int x, int y, Data data) { x = Mathf.Clamp(x, 0, wide - 1); y = Mathf.Clamp(y, 0, high - 1); array[x, y] = data; } /// 校验xy是否超限 读取正确范围内的数据 public bool TryGet(int x, int y, out Data data) { data = Get(x, y); return TryXY(x, y); } /// 校验xy是否超限 写入正确范围内的数据 public bool TrySet(int x, int y, Data data) { if (TryXY(x, y)) { array[x, y] = data; return true; } else { return false; } } }