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

## MOVE动画

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;
}



## 顺势掉落功能

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]))
{
}
m_allGamePieces[column, j] = null;
break;
}
}
}
}
return movingPiece;
}

04-11
09-02 2237

09-05 2万+
03-04 688
09-13 5756