www.pudn.com > Bluegammon蓝牙的应用编程.rar > WinnerPieceMoveAnim.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon.gui.animation; 
 
import javax.microedition.lcdui.Graphics; 
 
import bluegammon.gui.BoardCanvas; 
import bluegammon.logic.BoardMediator; 
 
/** 
 * Animation that flies away with a piece. Used from WinnerAnim 
 * when there is a winner of the game. 
 *  
 * @see bluegammon.gui.animation.WinnerAnim 
 * @author Peter Andersson 
 */ 
public class WinnerPieceMoveAnim extends Animation 
{ 
  /** Winner animation */ 
  protected WinnerAnim m_winAnim; 
  /** Number of frames in this animation */ 
  protected int m_frames; 
  /** True for white piece, false for black */ 
  protected boolean m_white; 
  /** Piece source x coordinate */ 
  protected int m_x0; 
  /** Piece source y coordinate */ 
  protected int m_y0; 
  /** Piece destination x coordinate */ 
  protected int m_x1; 
  /** Piece destination y coordinate */ 
  protected int m_y1; 
  /** Current frame */ 
  protected int m_frame = 0; 
  /** Current piece coordinates */ 
  protected float m_x, m_y; 
  /** Piece source position */ 
  protected int m_source; 
 
  /** 
   * Creates a piece movement animation for the winning animation. 
   *  
   * @param winAnim  The WinnerAnim. 
   * @param white    True for white piece, false for black. 
   * @param source   Source index of piece to animate. 
   * @param destX    Destination x-coordinate of piece animation. 
   * @param destY    Destination y-coordinate of piece animation. 
   * @param piecesOnSource  Number of pieces on source row. 
   */ 
  public WinnerPieceMoveAnim(WinnerAnim winAnim, boolean white, int source, 
      int destX, int destY, int piecesOnSource) 
  { 
    BoardCanvas canvas = BoardCanvas.getInstance(); 
    m_winAnim = winAnim; 
    m_white = white; 
    m_source = source; 
 
    // Get source and destination coordinates 
    m_x0 = canvas.getPieceX(source, piecesOnSource, white); 
    m_y0 = canvas.getPieceY(source, piecesOnSource, white); 
    m_x1 = destX; 
    m_y1 = destY; 
    m_x = m_x0; 
    m_y = m_y0; 
    m_frames = 30; 
  } 
 
  /** 
   * Called when animation starts, removes start piece from canvas - piece now 
   * on move. 
   */ 
  public void onStart() 
  { 
    BoardCanvas.getInstance().removePiece(m_white, m_source); 
  } 
 
  public void paint(Graphics g) 
  { 
    float frameRatio = (float)m_frame / (float)m_frames; 
    float dz = -BoardCanvas.PIECE_Z * frameRatio; 
    float angle = 270f * frameRatio; 
    // Draw piece 
    BoardCanvas.getInstance().drawPiece( 
        (int)m_x, (int)m_y, angle, true, dz, m_white, g); 
  } 
 
  public void next() 
  { 
    // Calculate new piece position 
    BoardCanvas canvas = BoardCanvas.getInstance(); 
 
    // Linear move 
    m_x = ((float)(m_x1 - m_x0) * m_frame) / (float)(m_frames) + m_x0; 
    m_y = ((float)(m_y1 - m_y0) * m_frame) / (float)(m_frames) + m_y0; 
 
    // Repaint 
    canvas.requestRepaint(); 
    m_frame++; 
  } 
 
  public boolean isFinished() 
  { 
    return !BoardMediator.isGameFinished() || m_frame > m_frames; 
  } 
 
  /** 
   * Called when animation exits, adds this disappering piece to the main winner 
   * animation. 
   */ 
  public void onExit() 
  { 
    m_winAnim.piecePlus(); 
  } 
 
  public long getInterval() 
  { 
    return 20; 
  } 
}