using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClearedSweet : MonoBehaviour {
//清除的甜品动画
public AnimationClip clearAnimation;
//当前对象是否在被清除
private bool isClearing;
//清除的声音
public AudioClip destoryAudio;
public bool IsClearing
{
get
{
return isClearing;
}
}
protected GameSweet sweet;
private void Awake()
{
sweet = GetComponent<GameSweet>();
}
public virtual void Clear()
{
isClearing = true;
StartCoroutine(ClearCoroutine());
}
private IEnumerator ClearCoroutine()
{
Animator animator = GetComponent<Animator>();
if (animator!=null)
{
animator.Play(clearAnimation.name);
//玩家得分+1 播放清楚声音
GameManager.Instance.playerScore++;
AudioSource.PlayClipAtPoint(destoryAudio, transform.position);
//动画播放完之后进行删除
yield return new WaitForSeconds(clearAnimation.length);
Destroy(gameObject);
}
}
}
GameManager上新建清除方法
ClearSweet()用来清除单个甜品 清除成功返回true,否则返回false
判断当前对象是否满足清除条件:当前对象可以被清除且当前没有正在被清除
private bool ClearSweet(int x,int y)
{
if (sweets[x,y].CanClear() && !sweets[x,y].ClearedComponent.IsClearing)
{
//调用清除方法并将当前位置置空
sweets[x, y].ClearedComponent.Clear();
CreateNewSweet(x,y,SweetsType.EMPTY);
return true;
}
return false;
}
ClearAllMatchSweet() 用来清除全部完成匹配的甜品对象
增加一个needRefill 是否需要填充的bool值 当有消除时 needRefill = true,反之为false
private bool ClearAllMatchedSweet()
{
bool needRefill = false;
for (int y = 0; y < yRow; y++)
{
for (int x = 0; x < xColumn; x++)
{
//遍历所有可以被消除的对象
if (sweets[x, y].CanClear())
{
//获取匹配完成列表中的对象
List<GameSweet> matchList = MatchSweets(sweets[x, y],x,y);
//消除列表中的对象
if(matchList != null)
{
for (int i = 0; i < matchList.Count; i++)
{
if (ClearSweet(matchList[i].X, matchList[i].Y))
{
needRefill = false;
}
}
}
}
}
}
return needRefill;
}
在交换位置后的位置ClearAllMatchSweet方法
同时ClearAllMatchSweet() 在Fill()中进行调用
调用前
调用后
//全部填充的方法
public IEnumerator AllFill()
{
bool needRefill = true
//当前有消除就再次调用填充
while (needRefill)
{
yield return new WaitForSeconds(fillTime);
while (Fill())
{
yield return new WaitForSeconds(fillTime);
//清除所有我们已经匹配好的甜品
needRefill = ClearAllMatchedSweet();
}
}
}