292 lines
9.3 KiB
C#
292 lines
9.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using AibisDream.Kit;
|
|
using AibisDream.Utility;
|
|
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class BlockPuzzle : Singleton<BlockPuzzle>
|
|
{
|
|
// 物体索引
|
|
[SerializeField] public BlockPuzzleGrid grid;
|
|
[SerializeField] private Transform shapeRoot;
|
|
[SerializeField] private BlockPuzzleValidator validator;
|
|
[SerializeField] public BlockShapeRack shapeRack;
|
|
|
|
// 临时按钮
|
|
[SerializeField] private Button submitButton;
|
|
[SerializeField] private Button clearButton;
|
|
|
|
// 事件
|
|
public event Action<ValidateResult> OnSubmit;
|
|
public event Action<ValidateResult> OnPreview;
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
|
|
submitButton.onClick.AddListener(SubmitGameResult);
|
|
clearButton.onClick.AddListener(ResetGame);
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
// 初始化网格
|
|
grid.Initialize();
|
|
grid.OnShapePlaced += PreCheckGameState;
|
|
// 初始化网格上的Shape预设
|
|
|
|
// 初始化图形货架
|
|
// shapeRack.Initialize();
|
|
}
|
|
|
|
private void PreCheckGameState()
|
|
{
|
|
var shapes = grid.GetShapes();
|
|
var validateResult = validator.Validate(shapes);
|
|
OnPreview?.Invoke(validateResult);
|
|
}
|
|
|
|
private void SubmitGameResult()
|
|
{
|
|
var shapes = grid.GetShapes();
|
|
var validateResult = validator.Validate(shapes);
|
|
OnSubmit?.Invoke(validateResult);
|
|
}
|
|
|
|
private void ResetGame()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public static class BlockPuzzleKit
|
|
{
|
|
/// <summary>
|
|
/// 保存单个 BlockShapeData 到文件,确保 Key 唯一(如果 Key 已存在则更新,不存在则添加)
|
|
/// </summary>
|
|
/// <param name="data">要保存的数据</param>
|
|
/// <param name="key">数据的键(用于字典存储)</param>
|
|
/// <param name="path">保存路径,如果为空则使用默认路径</param>
|
|
public static void SaveBlockShapeData(BlockShapeData data, string key, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
// 确保目录存在
|
|
string directory = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
// 读取现有字典(如果文件存在)
|
|
Dictionary<string, BlockShapeData> dataDict;
|
|
if (File.Exists(path))
|
|
{
|
|
try
|
|
{
|
|
dataDict = JsonUtil.ReadBeanDict<BlockShapeData>(path) ?? new Dictionary<string, BlockShapeData>();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"读取现有 BlockShapeData 文件失败,将创建新文件: {e.Message}");
|
|
dataDict = new Dictionary<string, BlockShapeData>();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dataDict = new Dictionary<string, BlockShapeData>();
|
|
}
|
|
|
|
// 更新或添加数据(确保 Key 唯一)
|
|
dataDict[key] = data;
|
|
|
|
// 保存整个字典
|
|
JsonUtil.SaveBean(dataDict, path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存 BlockShapeData 数组到文件
|
|
/// </summary>
|
|
/// <param name="dataArray">要保存的数据数组</param>
|
|
/// <param name="path">保存路径,如果为空则使用默认路径</param>
|
|
public static void SaveBlockShapeDataArray(BlockShapeData[] dataArray, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
// 确保目录存在
|
|
string directory = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
JsonUtil.SaveArray(dataArray, path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存 BlockShapeData 字典到文件
|
|
/// </summary>
|
|
/// <param name="dataDict">要保存的数据字典</param>
|
|
/// <param name="path">保存路径,如果为空则使用默认路径</param>
|
|
public static void SaveBlockShapeDataDict(Dictionary<string, BlockShapeData> dataDict, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
// 确保目录存在
|
|
string directory = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
JsonUtil.SaveBean(dataDict, path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从文件加载单个 BlockShapeData
|
|
/// </summary>
|
|
/// <param name="key">数据的键</param>
|
|
/// <param name="path">文件路径,如果为空则使用默认路径</param>
|
|
/// <param name="data">加载的数据</param>
|
|
/// <returns>是否加载成功</returns>
|
|
public static bool TryLoadBlockShapeData(string key, out BlockShapeData data, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
data = default;
|
|
return false;
|
|
}
|
|
|
|
var dict = JsonUtil.ReadBeanDict<BlockShapeData>(path);
|
|
if (dict != null && dict.ContainsKey(key))
|
|
{
|
|
data = dict[key];
|
|
return true;
|
|
}
|
|
|
|
data = default;
|
|
return false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"加载 BlockShapeData 失败: {e.Message}");
|
|
data = default;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从文件加载 BlockShapeData 数组
|
|
/// </summary>
|
|
/// <param name="path">文件路径,如果为空则使用默认路径</param>
|
|
/// <param name="dataArray">加载的数据数组</param>
|
|
/// <returns>是否加载成功</returns>
|
|
public static bool TryLoadBlockShapeDataArray(out BlockShapeData[] dataArray, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
dataArray = null;
|
|
return false;
|
|
}
|
|
|
|
dataArray = JsonUtil.ReadBeanArray<BlockShapeData>(path);
|
|
return dataArray != null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"加载 BlockShapeData 数组失败: {e.Message}");
|
|
dataArray = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从文件加载 BlockShapeData 字典
|
|
/// </summary>
|
|
/// <param name="path">文件路径,如果为空则使用默认路径</param>
|
|
/// <param name="dataDict">加载的数据字典</param>
|
|
/// <returns>是否加载成功</returns>
|
|
public static bool TryLoadBlockShapeDataDict(out Dictionary<string, BlockShapeData> dataDict, string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
dataDict = null;
|
|
return false;
|
|
}
|
|
|
|
dataDict = JsonUtil.ReadBeanDict<BlockShapeData>(path);
|
|
return dataDict != null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"加载 BlockShapeData 字典失败: {e.Message}");
|
|
dataDict = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有已保存的 BlockShapeData 的键列表
|
|
/// </summary>
|
|
/// <param name="path">文件路径,如果为空则使用默认路径</param>
|
|
/// <returns>键列表,如果文件不存在则返回空数组</returns>
|
|
public static string[] GetAllBlockShapeDataKeys(string path = null)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = ConstRef.BlockShapeDataPath;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
return new string[0];
|
|
}
|
|
|
|
return JsonUtil.ReadKeys(path);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"获取 BlockShapeData 键列表失败: {e.Message}");
|
|
return new string[0];
|
|
}
|
|
}
|
|
}
|
|
}
|