www.pudn.com > jbombman.rar > Bomb.java


package example.jbombman; 
 
public class Bomb extends Thread { 
	private Board oBoard; 
	private BoardView oBoardView; 
	private Player oPlayer; 
	private Enemy oEnemy; 
	 
	private final int iExplodingTime = 4000; 
	private final int iDisapearTime = 1000; 
	 
	private int iX, iY; 
	private volatile boolean stopThread = false; 
	 
	public Bomb( Board board, BoardView boardview, Player player, Enemy enemy, 
					int x, int y ) { 
		oBoard = board; 
		oBoardView = boardview; 
		oPlayer = player; 
		oEnemy = enemy; 
		 
		iX = x; 
		iY = y; 
		 
		oBoard.chBoard[iX][iY] = 'U'; 
	} 
	 
	public void stopThread() { 
		stopThread = true; 
	} 
 
	private void explode( int x, int y ) { 
		System.out.println( "Bomb explode: " + x + ", " + y ); 
		if( oBoard.isElement( 'P', x, y )||oBoard.isElement( 'U', x, y ) ) 
			oPlayer.die(); 
		else if ( oBoard.isElement( 'E', x, y ) ) 
			oEnemy.die( x, y ); 
		 
		if ( oBoard.isElement( 'N', x, y )||oBoard.isElement( 'W', x, y ) 
				||oBoard.isElement( 'P', x, y )||oBoard.isElement( 'E', x, y ) 
				||oBoard.isElement( 'U', x, y )||oBoard.isElement( 'B', x, y ) ) 
			oBoard.chBoard[x][y] = 'X'; 
	} 
	 
	private void clear( int x, int y ) { 
		if( oBoard.isElement( 'X', x, y ) ) 
			oBoard.setElement( 'N', x, y ); 
	} 
 
	public void run() { 
        while (!stopThread) { 
            try { 
                sleep(iExplodingTime); 
            	System.out.println( "Explode" ); 
				explode( iX, iY ); 
				explode( iX-1, iY ); 
				explode( iX+1, iY ); 
				explode( iX, iY-1 ); 
				explode( iX, iY+1 ); 
				oBoardView.repaintCells(iX-1, iY-1, 3, 3); 
				 
				sleep(iDisapearTime); 
            	System.out.println( "Clear" ); 
				oBoard.chBoard[iX][iY] = 'N'; 
				clear( iX-1, iY ); 
				clear( iX+1, iY ); 
				clear( iX, iY-1 ); 
				clear( iX, iY+1 ); 
				oBoardView.repaintCells(iX-1, iY-1, 3, 3); 
				 
				stopThread = true; 
				oPlayer.clearBomb(); 
            }  
            catch (InterruptedException ie) { 
            } 
        }//while 
	} 
}