/// <summary>
/// 查找当前元素匹配的满足消除条件的元素列表
/// </summary>
/// <returns></returns>
public List<Item> FindMatchItems()
{
List<Item> matchItemsList = new List<Item>(); // 最终返回的匹配列表
List<Item> tempMatchList = new List<Item>();
tempMatchList = FindOneMatchItem(tempMatchList, colorIndex, FindSperating.Horizontal);
if (tempMatchList.Count >= miniMatches) // 达到最低的匹配数量,即表示水平满足消除条件
{
foreach (Item item in tempMatchList) // 存入到匹配列表中
{
matchItemsList.Add(item);
}
matchHorItemCount = tempMatchList.Count;
}
tempMatchList.Clear();
tempMatchList = FindOneMatchItem(tempMatchList, colorIndex, FindSperating.Vertical);
if (tempMatchList.Count >= miniMatches) // 达到最低的匹配数量,即表示垂直满足消除条件
{
foreach (Item item in tempMatchList) // 存入到匹配列表中
{
matchItemsList.Add(item);
}
matchVerItemCount = tempMatchList.Count;
}
tempMatchList.Clear();
return matchItemsList;
}
/// <summary>
/// 查找匹配,递归检测,每次检测一个元素是否和交换元素的颜色一致
/// </summary>
/// <param name="matchItemsList"> 储存匹配元素的列表 </param>
/// <param name="sprColor"> 需要匹配的元素颜色 </param>
/// <param name="findSperating"> 查找区分,分水平/垂直查找 </param>
/// <returns></returns>
private List<Item> FindOneMatchItem(List<Item> matchItemsList, int sprColor, FindSperating findSperating)
{
if (colorIndex != 0 && colorIndex == sprColor && !matchItemsList.Contains(this))// 如果颜色一样,则符合条件,如果已经包含了则不再检查
{
matchItemsList.Add(this);
if (findSperating == FindSperating.Horizontal)
{
if (GetTopItem())
GetTopItem().FindOneMatchItem(matchItemsList, sprColor, FindSperating.Horizontal); // 检查上方元素是否匹配
if (GetBottomItem())
GetBottomItem().FindOneMatchItem(matchItemsList, sprColor, FindSperating.Horizontal);
}
else if (findSperating == FindSperating.Vertical)
{
if (GetLeftItem())
GetLeftItem().FindOneMatchItem(matchItemsList, sprColor, FindSperating.Vertical);
if (GetRightItem())
GetRightItem().FindOneMatchItem(matchItemsList, sprColor, FindSperating.Vertical);
}
}
return matchItemsList;
}