unity学习:三消基础逻辑及其代码2

MOVE动画

用了Lerp的功能。

public void Move(int destX,int destY,float timeToMove)
    {
        if (!m_isMoving)
        {
            StartCoroutine(MoveRoutine(new Vector3(destX, destY, 0), timeToMove));
        }
    }
IEnumerator MoveRoutine(Vector3 destination, float timeToMove)
    {
        Vector3 startPosition = transform.position;
        bool reachDestination = false;
        float elapsedTime = 0f;
        m_isMoving = true;
        while (!reachDestination)
        {
            if (Vector3.Distance(transform.position, destination) < 0.01)
            {
                reachDestination = true;                
                if (m_board != null)
                {
                    m_board.PlaceGamePiece(this, (int)destination.x, (int)destination.y);
                }
                break;
            }            
            elapsedTime += Time.deltaTime;
            float t = Mathf.Clamp(elapsedTime / timeToMove,0f,1f);            
            switch (interPolation)
            {
                case InterpType.Linear:
                    break;
                case InterpType.EaseOut:
                    t = Mathf.Sin(t * Mathf.PI * 0.5f);
                    break;
                case InterpType.EaseIn:
                    t = 1 - Mathf.Cos(t * Mathf.PI * 0.5f);
                    break;
                case InterpType.SmoothStep:
                    t = t * t * (3 - 2 * t);
                    break;
                case InterpType.SmootherStep:
                    t = t * t * t * (t * (t * 6 - 15) + 10);
                    break;
            }            transform.position = Vector3.Lerp(startPosition, destination, t);            //wait until next 帧
            yield return null;
        }
        m_isMoving = false;
    }

顺势掉落功能

这部分的原理是从三消地图的底部开始一列一列轮询。每一列轮询是从最底部开始往上。如果发现了一个空的,那么就找到在它的直线往上的最近那个宝石,让这个宝石MOVE到这个空的位置上,然后把宝石的位置设置为空。一直连环处理就能实现顺势掉落。

List<GamePiece> CollapseColumn(int column, float collapseTime = 0.1f)
{
    List<GamePiece> movingPiece = new List<GamePiece>();
    for (int i = 0; i < height - 1; i++)
    {
        //如果找到了有某一缺了一个。
        if (m_allGamePieces[column, i] == null)
        {
            
            for (int j = i + 1; j < height; j++)
            {
                if (m_allGamePieces[column, j] != null)
                {
                    //如何实现根据内容越来越快。
                    m_allGamePieces[column, j].Move(column, i, collapseTime*(j-i));
                    m_allGamePieces[column, i] = m_allGamePieces[column, j];
                    m_allGamePieces[column, i].SetCoord(column, i);
                    if (!movingPiece.Contains(m_allGamePieces[column, i]))
                    {
                        movingPiece.Add(m_allGamePieces[column, i]);
                    }
                    m_allGamePieces[column, j] = null;
                    break;
                }
            }
        }
    }
    return movingPiece;
}

以上内容为https://www.udemy.com/course/make-a-puzzle-match-game-in-unity/课程的笔记。
很好的课程!推荐去看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值