public bool placeGrids(RuntimeGrids grids, int x, int y, bool preview = false) { if (!isPlacePointValid(grids, x, y)) { return(false); } var pp = new Vector2(x, y); var rGrid = grids.rootGrid; var offsetX = (int)pp.x - rGrid.x; var offsetY = (int)pp.y - rGrid.y; // 遍历每个方块 foreach (var grid in grids.realGrids) { var rx = grid.x + offsetX; // 实际地图坐标 var ry = grid.y + offsetY; if (preview) { previewPos.Add(new Vector2(rx, ry)); } else { changeGrid(rx, ry, grids.belong); changeGrid(rx, ry, newGridType()); _refreshRequest = true; } } return(true); }
/// <summary> /// 获取放置点 /// </summary> /// <param name="grids"></param> /// <param name="x"></param> /// <returns></returns> Vector2?getPlacePoint(RuntimeGrids grids, int x) { Vector2?res = null; switch (grids.operer) { case Grid.Belong.Player: for (int y = mapY - 1; y >= 0; --y) { if (!isPlacePointValid(grids, x, y)) { break; } res = new Vector2(x, y); } break; case Grid.Belong.Enemy: for (int y = 0; y < mapY; ++y) { if (!isPlacePointValid(grids, x, y)) { break; } res = new Vector2(x, y); } break; } if (res == null && x > 0) // 找不到放置点 { return(getPlacePoint(grids, x - 1)); } return(res); }
/// <summary> /// 放置格子 /// </summary> /// <param name="grids">格子组</param> /// <param name="x">位置</param> /// <param name="belong">性质</param> public void placeGrids(RuntimeGrids grids, int x, bool preview = false) { var _pp = getPlacePoint(grids, x); if (_pp == null) { return; } var pp = (Vector2)_pp; var rGrid = grids.rootGrid; var offsetX = (int)pp.x - rGrid.x; var offsetY = (int)pp.y - rGrid.y; // 遍历每个方块 foreach (var grid in grids.realGrids) { var rx = grid.x + offsetX; // 实际地图坐标 var ry = grid.y + offsetY; if (preview) { previewPos.Add(new Vector2(rx, ry)); } else { changeGrid(rx, ry, grids.belong); changeGrid(rx, ry, newGridType()); _refreshRequest = true; } } }
/// <summary> /// 判断放置点是否合法 /// </summary> /// <param name="grids"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool isPlacePointValid(RuntimeGrids grids, int x, int y) { var rGrid = grids.rootGrid; var offsetX = x - rGrid.x; var offsetY = y - rGrid.y; // 遍历每个方块 foreach (var grid in grids.realGrids) { var rx = grid.x + offsetX; // 实际地图坐标 var ry = grid.y + offsetY; if (!isPointValid(rx, ry, grids.belong)) { return(false); } } return(true); }