174 lines
5.5 KiB
C#
174 lines
5.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Cinemachine;
|
|
using AibisDream.Kit;
|
|
using AibisDream.Utility;
|
|
using System.Linq;
|
|
using AibisDream.Framework;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class BlockPuzzleSystem : Singleton<BlockPuzzleSystem>
|
|
{
|
|
[Header("物体索引")]
|
|
// 物体索引
|
|
[SerializeField] public BlockPuzzleGrid grid;
|
|
[SerializeField] private Transform shapeRoot;
|
|
[SerializeField] public BlockShapeRack shapeRack;
|
|
[SerializeField] private Canvas canvas;
|
|
|
|
|
|
[Header("临时按钮")]
|
|
// 临时按钮
|
|
[SerializeField] private Button submitButton;
|
|
[SerializeField] private Button clearButton;
|
|
|
|
[Header("数据")]
|
|
[SerializeField] private BlockPuzzleData puzzleData;
|
|
[SerializeField] private bool initializeOnStart = true;
|
|
|
|
private BlockPuzzleValidator validator;
|
|
|
|
// 事件
|
|
public event Action<ValidateResult> OnSubmit;
|
|
public event Action<ValidateResult> OnPreview;
|
|
|
|
private void Start()
|
|
{
|
|
if (initializeOnStart)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
CameraKit.Instance?.RegisterCamera(CameraEnum.BlockPuzzle, GetComponentInChildren<ICinemachineCamera>());
|
|
canvas.worldCamera = Camera.main;
|
|
submitButton.onClick.AddListener(SubmitGameResult);
|
|
clearButton.onClick.AddListener(ResetGame);
|
|
}
|
|
|
|
public override void OnSingletonDestroy()
|
|
{
|
|
CameraKit.Instance?.UnRegisterCamera(CameraEnum.BlockPuzzle);
|
|
submitButton.onClick.RemoveListener(SubmitGameResult);
|
|
clearButton.onClick.RemoveListener(ResetGame);
|
|
}
|
|
|
|
public void Initialize(BlockPuzzleData puzzleData)
|
|
{
|
|
this.puzzleData = puzzleData;
|
|
ClearGame();
|
|
Initialize();
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
// 初始化网格
|
|
grid.Initialize(puzzleData.gridWidth, puzzleData.gridHeight, puzzleData.cellSize);
|
|
// 初始化网格上图形
|
|
foreach (var shapeData in puzzleData.gridShapeDataInfos)
|
|
{
|
|
var shape = shapeRack.CreateBlockShape(shapeData.shapeKey);
|
|
shape.Rotate(shapeData.rotationAngle);
|
|
grid.TrySnapToGrid(shape, shapeData.rootCell);
|
|
shape.GetComponent<ShapeDragger>().IsDraggable = shapeData.isDraggable;
|
|
}
|
|
|
|
grid.GridVisualizer.UpdateAllCellsVisual();
|
|
grid.OnShapePlaced += PreCheckGameState;
|
|
|
|
// 初始化图形货架
|
|
shapeRack.Init(puzzleData.shapeRackDataInfos);
|
|
|
|
// 加载验证器
|
|
validator = ResourceKit.LoadAssetSync<BlockPuzzleValidator>(puzzleData.validatorName);
|
|
|
|
Debug.Log("网格系统初始化完成");
|
|
}
|
|
|
|
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);
|
|
if (validateResult.isPass)
|
|
{
|
|
StorageSystem.Instance.SetValue("$block_puzzle_pass", validateResult.isExtraConditionPass);
|
|
DialogController.Instance.StartDialogNode("方块拼图完成");
|
|
}
|
|
}
|
|
|
|
public void ResetGame()
|
|
{
|
|
ClearGame();
|
|
Initialize();
|
|
}
|
|
|
|
public void ClearGame()
|
|
{
|
|
grid.Clear();
|
|
shapeRack.Clear();
|
|
shapeRoot.DestroyAllChildren();
|
|
}
|
|
|
|
// ---------------- 编辑器状态逻辑 ----------------
|
|
|
|
/// <summary>
|
|
/// 仅加载配置项,不应用到游戏
|
|
/// </summary>
|
|
public void LoadConfig()
|
|
{
|
|
if (BlockPuzzleKit.TryLoadBlockPuzzleData(puzzleData.puzzleName, out var blockPuzzleData))
|
|
{
|
|
puzzleData = blockPuzzleData;
|
|
Debug.Log("加载配置项成功");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("加载配置项失败");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将配置项应用到编辑器
|
|
/// </summary>
|
|
public void ApplyConfig()
|
|
{
|
|
ClearGame();
|
|
|
|
grid.InitializeInEditor(puzzleData.gridWidth, puzzleData.gridHeight, puzzleData.cellSize);
|
|
shapeRack.InitInEditor(puzzleData.shapeRackDataInfos);
|
|
|
|
foreach (var shapeData in puzzleData.gridShapeDataInfos)
|
|
{
|
|
var shape = shapeRack.CreateBlockShape(shapeData.shapeKey);
|
|
shape.Rotate(shapeData.rotationAngle);
|
|
grid.TrySnapToGrid(shape, shapeData.rootCell);
|
|
}
|
|
}
|
|
|
|
public void SaveConfig()
|
|
{
|
|
var gridShapeDataInfos = grid.ShapeDict?.Values.ToArray();
|
|
var shapeRackDataInfos = shapeRack.blockShapeDataList?.ToArray();
|
|
|
|
puzzleData.gridShapeDataInfos = gridShapeDataInfos;
|
|
puzzleData.shapeRackDataInfos = shapeRackDataInfos;
|
|
|
|
BlockPuzzleKit.SaveBlockPuzzleData(puzzleData, puzzleData.puzzleName);
|
|
}
|
|
|
|
public void CreateShapeInEditor(string shapeKey)
|
|
{
|
|
shapeRack.CreateBlockShape(shapeKey);
|
|
}
|
|
}
|
|
}
|