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/课程的笔记。
很好的课程!推荐去看。