109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using AibisDream.Utility;
|
|
|
|
namespace AibisDream
|
|
{
|
|
[CreateAssetMenu(fileName = "BlockPuzzleValidator", menuName = "Block Puzzle/BlockPuzzleValidator")]
|
|
public class BlockPuzzleValidator : ScriptableObject
|
|
{
|
|
[Tooltip("基础条件, 不满足无法提交")]
|
|
public ValidateItem[] baseConditions;
|
|
[Tooltip("额外条件, 不满足也可以提交")]
|
|
public ValidateItem[] extraConditions;
|
|
|
|
/// <summary>
|
|
/// 验证是否可以完成拼图
|
|
/// </summary>
|
|
/// <param name="shapeInGrid"></param>
|
|
/// <returns></returns>
|
|
public ValidateResult Validate(List<BlockShape> shapeInGrid)
|
|
{
|
|
var result = new ValidateResult();
|
|
var shapePropertyValues = new Dictionary<string, int>();
|
|
// 统计每个Shape的属性值
|
|
foreach (var shape in shapeInGrid)
|
|
{
|
|
foreach (var property in shape.BlockShapeData.shapeProperties)
|
|
{
|
|
if (shapePropertyValues.ContainsKey(property.property))
|
|
{
|
|
shapePropertyValues[property.property] += property.value;
|
|
}
|
|
else
|
|
{
|
|
shapePropertyValues.Add(property.property, property.value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 先校验基础条件
|
|
foreach (var condition in baseConditions)
|
|
{
|
|
var itemResult = condition.Validate(shapePropertyValues);
|
|
|
|
result.baseConditionResults.Add((condition.name, itemResult));
|
|
result.isPass = result.isPass && itemResult;
|
|
}
|
|
|
|
// 再校验额外条件
|
|
foreach (var condition in extraConditions)
|
|
{
|
|
var itemResult = condition.Validate(shapePropertyValues);
|
|
|
|
result.extraConditionResults.Add((condition.name, itemResult));
|
|
result.isExtraConditionPass = result.isExtraConditionPass && itemResult;
|
|
}
|
|
|
|
result.shapePropertiesResult = shapePropertyValues
|
|
.Select(item => new ShapeProperty { property = item.Key, value = item.Value })
|
|
.OrderBy(item => item.property)
|
|
.ToArray();
|
|
|
|
return result;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct ValidateItem
|
|
{
|
|
[Tooltip("名称, 仅用于显示")]
|
|
public string name;
|
|
|
|
public string property;
|
|
public Vector2Int range;
|
|
public bool isOutRange;
|
|
|
|
public bool Validate(Dictionary<string, int> shapePropertyValues)
|
|
{
|
|
int totalValue = 0;
|
|
if (shapePropertyValues.ContainsKey(property))
|
|
{
|
|
totalValue = shapePropertyValues[property];
|
|
}
|
|
|
|
return CommonUtil.IsInRange(totalValue, range.x, range.y) != isOutRange;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ValidateResult
|
|
{
|
|
public bool isPass;
|
|
public bool isExtraConditionPass;
|
|
|
|
public ShapeProperty[] shapePropertiesResult;
|
|
|
|
public List<(string name, bool isPass)> baseConditionResults;
|
|
public List<(string name, bool isPass)> extraConditionResults;
|
|
|
|
public ValidateResult()
|
|
{
|
|
isPass = true;
|
|
isExtraConditionPass = true;
|
|
|
|
baseConditionResults = new List<(string name, bool isPass)>();
|
|
extraConditionResults = new List<(string name, bool isPass)>();
|
|
}
|
|
}
|
|
} |