对于object自己来讲,有一个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);
}
//transform.position = destination;
//SetCoord((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 time
yield return null;
}
m_isMoving = false;
}
这个move的函数在何时调用?在我们在格子面板上触发交换时调用。
void SwitchTiles(Tile clickTile, Tile targetTile)
{
GamePiece clickedPiece = m_allGamePieces[clickTile.xIndex, clickTile.yIndex];
GamePiece targetPiece = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];
clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
targetPiece.Move(clickTile.xIndex, clickTile.yIndex, swapTime);
m_clickTile = null;
m_targetTile = null;
}