private void RunMoveTo(ref MoveTo moveto) { float time = moveto.Time; Vector2 nextposition = moveto.NextPosition; if (time == 0) { Position = nextposition; moveto.IsDone = true; moveto.IsDoing = false; return; } if (!moveto.IsDoing) { moveto.IsDoing = true; var velocityX = (nextposition.X - Position.X) / (time * 60); //60 là fps var velocityY = (nextposition.Y - Position.Y) / (time * 60); m_Velocity = new Vector2(velocityX, velocityY); m_moveDistance = (float)Math.Sqrt( (nextposition.X - Position.X) * (nextposition.X - Position.X) + (nextposition.Y - Position.Y) * (nextposition.Y - Position.Y) ); m_oldPosition = Position; } var distance = (float)Math.Sqrt(((Position.X - m_oldPosition.X) * (Position.X - m_oldPosition.X) + (Position.Y - m_oldPosition.Y) * (Position.Y - m_oldPosition.Y))); if (m_moveDistance >= distance && !moveto.IsDone) { //Kiểm tra lần di chuyển cuối cùng có vượt ra khỏi vị trí đích hay không if ((float)Math.Sqrt(m_Velocity.X * m_Velocity.X + m_Velocity.Y * m_Velocity.Y) >= Math.Abs(m_moveDistance - distance)) { //Nếu có thì gán vận tốc bằng quãng đường còn lại //m_Velocity.X = nextposition.X - Position.X; //m_Velocity.Y = nextposition.Y - Position.Y; Position = new Vector2(nextposition.X, nextposition.Y); moveto.IsDone = true; moveto.IsDoing = false; } else { Position = new Vector2(Position.X + m_Velocity.X, Position.Y + m_Velocity.Y); } } else { moveto.IsDone = true; moveto.IsDoing = false; } }
public virtual void AddMoveTo(MoveTo moveto) { _moveList.Add(moveto); }
private void RunMoveTo(ref MoveTo moveto) { float time = moveto.Time; Vector2 nextposition = moveto.NextPosition; if (time == 0) { Position = nextposition; moveto.IsDone = true; moveto.IsDoing = false; return; } if(!moveto.IsDoing) { moveto.IsDoing = true; var velocityX = (nextposition.X - Position.X) / (time * 60); //60 là fps var velocityY = (nextposition.Y - Position.Y) / (time * 60); m_Velocity = new Vector2(velocityX, velocityY); m_moveDistance = (float)Math.Sqrt( (nextposition.X - Position.X) * (nextposition.X - Position.X) + (nextposition.Y - Position.Y) * (nextposition.Y - Position.Y) ); m_oldPosition = Position; } var distance = (float)Math.Sqrt(((Position.X - m_oldPosition.X) * (Position.X - m_oldPosition.X) + (Position.Y - m_oldPosition.Y) * (Position.Y - m_oldPosition.Y))); if (m_moveDistance >= distance && !moveto.IsDone) { //Kiểm tra lần di chuyển cuối cùng có vượt ra khỏi vị trí đích hay không if ((float)Math.Sqrt(m_Velocity.X * m_Velocity.X + m_Velocity.Y * m_Velocity.Y) >= Math.Abs(m_moveDistance - distance)) { //Nếu có thì gán vận tốc bằng quãng đường còn lại //m_Velocity.X = nextposition.X - Position.X; //m_Velocity.Y = nextposition.Y - Position.Y; Position = new Vector2(nextposition.X, nextposition.Y); moveto.IsDone = true; moveto.IsDoing = false; } else { Position = new Vector2(Position.X + m_Velocity.X, Position.Y + m_Velocity.Y); } } else { moveto.IsDone = true; moveto.IsDoing = false; } }
public virtual void AddMoveTo(MoveTo moveto) { Sprite.AddMoveTo(moveto); }