/**
  *The specific implementation of the abstract match specification
  *A match is a specific instance of playing a game
  */
public class BricklesMatch extends ArcadeGameMatch{
	PuckSupply puckSupply;
	Puck currentPuck;
 
	/**
	  *Constructor instantiates the timer that will drive the game
	  */
	public BricklesMatch(ArcadeGame parentGame){
		super(parentGame);
	  	_timer = new Timer(((BricklesGame)parentGame).getView());
	}

	/**
	  *No-op for tick at this level
	  */
	public void tick(){
	
	}

	/**
	  *This behavior is needed whenever a Piece is to be removed from a running game
	  *@param aPiece The Piece to be deleted
	  */
	public void deleted(ArcadeGamePiece aPiece){
		if((Puck)aPiece == currentPuck){
			currentPuck = this.getNewPuck();
			((BricklesPlayingField)_fieldPtr).setPuck(currentPuck);
		 	_timer.register((MovablePiece)currentPuck);
		}
	}

	/**
	  *Starts the match
	  *A specific impementation of the GameControl interface
	  *post: match is in the running state
	  */
	public void start(){
		_fieldPtr = (BricklesPlayingField)(((BricklesGame)_game).newPlayingField((ArcadeGameMatch)this));
		puckSupply = new PuckSupply(_fieldPtr);
		currentPuck = this.getNewPuck();
	 	((BricklesPlayingField)_fieldPtr).setPuck(currentPuck);
	  	_timer.register((MovablePiece)currentPuck);
            _timer.register(this.getCurrentPaddle());
	  	_timer.start();
	}


	/**
	  *This method abstracts the source of pucks
	  *@return a new Puck
	  *post: a new Puck is returned OR the game is lost and the method does not return
	  */
	public Puck getNewPuck(){
		try{
			currentPuck = puckSupply.getPuck();
		}
		catch(OutOfPucksException ope){
			_game.lost(); //game is lost
		}
		return currentPuck;
	}

	/**
	  *Accessor to the curent Puck
	  *@return the current Puck
	  */
	public Puck getCurrentPuck(){
		return currentPuck;
	}

	/**
	  *Accessor to the curent Paddle
	  *@return the current Paddle
	  */
	public Paddle getCurrentPaddle(){
		return ((BricklesPlayingField)_fieldPtr).getPaddle();
	}

	/**
	  *Accessor to the curent BrickPile
	  *@return the current BrickPile
	  */
	public BrickPile getBrickPile(){
		return ((BricklesPlayingField)_fieldPtr).getBrickPile();
	}

}
