甜品消消乐 14 消除

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();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值