## Class Model

- java.lang.Object
- - Model

-     public class Model
      extends Object

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `int` | `B` | First,a collection of useful values for calculating sizes and layouts etc.
    `GameObj` | `ball` | The game 'model' - these represent the state of the game and are used by the View to display it
    `int` | `BALL_MOVE` | Distance to move bat on each keypress
    `int` | `BALL_SIZE` | Height of menu bar space at the top
    `GameObj` | `bat` |
    `int` | `BAT_MOVE` |
    `int` | `BRICK_HEIGHT` | Brick size
    `int` | `BRICK_WIDTH` | Ball side
    `GameObj[]` | `bricks` |
    `(package private) Controller` | `controller` |
    `boolean` | `fast` | Set to "finished" to end the game
    `String` | `gameState` | variables that control the game
    `int` | `height` | Width of game
    `int` | `HIT_BOTTOM` | Score for hitting a brick
    `int` | `HIT_BRICK` | Units to move the ball on each step
    `int` | `M` | Border round the edge of the panel
    `GameObj` | `Outline` |
    `int` | `score` |
    `(package private) View` | `view` | The other parts of the model-view-controller setup
    `int` | `width` | initialisation parameters for the model

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `Model​(int w, int h)` | CONSTRUCTOR - needs to know how big the window will be

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `addToScore​(int n)` | update the score
    `GameObj` | `getBall()` | return ball object
    `GameObj` | `getBat()` | Return bat object
    `GameObj[]` | `getBricks()` | return bricks
    `Boolean` | `getFast()` | Return game speed - false is normal speed, true is fast
    `String` | `getGameState()` | Return game running state
    `int` | `getScore()` | return score
    `void` | `initialiseGame()` | Initialise the game - reset the score and create the game objects
    `void` | `modelChanged()` | This is how the Model talks to the View Whenever the Model changes, this method calls the update method in the View.
    `void` | `moveBat​(int direction)` | move the bat one step - -1 is left, +1 is right
    `void` | `runGame()` | The main animation loop
    `void` | `setFast​(Boolean value)` | Change game speed - false is normal speed, true is fast
    `void` | `setGameState​(String value)` | Methods for accessing and updating values these are all synchronized so that the can be called by the main thread or the animation thread safely Change game state - set to "running" or "finished"
    `void` | `startGame()` | // Animating the game The game is animated by using a 'thread'.
    `void` | `updateGame()` | updating the game - this happens about 50 times a second to give the impression of movement

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### B

          public int B

      First,a collection of useful values for calculating sizes and layouts etc.

    - #### M

          public int M

      Border round the edge of the panel

    - #### BALL_SIZE

          public int BALL_SIZE

      Height of menu bar space at the top

    - #### BRICK_WIDTH

          public int BRICK_WIDTH

      Ball side

    - #### BRICK_HEIGHT

          public int BRICK_HEIGHT

      Brick size

    - #### BAT_MOVE

          public int BAT_MOVE

    - #### BALL_MOVE

          public int BALL_MOVE

      Distance to move bat on each keypress

    - #### HIT_BRICK

          public int HIT_BRICK

      Units to move the ball on each step

    - #### HIT_BOTTOM

          public int HIT_BOTTOM

      Score for hitting a brick

    - #### view

          View view

      The other parts of the model-view-controller setup

    - #### controller

          Controller controller

    - #### ball

          public GameObj ball

      The game 'model' - these represent the state of the game and are used by the View to display it

    - #### bricks

          public GameObj[] bricks

    - #### bat

          public GameObj bat

    - #### score

          public int score

    - #### Outline

          public GameObj Outline

    - #### gameState

          public String gameState

      variables that control the game

    - #### fast

          public boolean fast

      Set to "finished" to end the game

    - #### width

          public int width

      initialisation parameters for the model

    - #### height

          public int height

      Width of game

  - ### Constructor Detail

    - #### Model

          public Model​(int w,
                       int h)

      CONSTRUCTOR - needs to know how big the window will be

  - ### Method Detail

    - #### startGame

          public void startGame()

      // Animating the game The game is animated by using a 'thread'. Threads allow the program to do two (or more) things at the same time. In this case the main program is doing the usual thing (View waits for input, sends it to Controller, Controller sends to Model, Model updates), but a second thread runs in a loop, updating the position of the ball, checking if it hits anything (and changing direction if it does) and then telling the View the Model changed. When we use more than one thread, we have to take care that they don't interfere with each other (for example, one thread changing the value of a variable at the same time the other is reading it). We do this by SYNCHRONIZING methods. For any object, only one synchronized method can be running at a time - if another thread tries to run the same or another synchronized method on the same object, it will stop and wait for the first one to finish. Start the animation thread

    - #### initialiseGame

          public void initialiseGame()

      Initialise the game - reset the score and create the game objects

    - #### runGame

          public void runGame()

      The main animation loop

    - #### updateGame

          public void updateGame()

      updating the game - this happens about 50 times a second to give the impression of movement

    - #### modelChanged

          public void modelChanged()

      This is how the Model talks to the View Whenever the Model changes, this method calls the update method in the View. It needs to run in the JavaFX event thread, and Platform.runLater is a utility that makes sure this happens even if called from the runGame thread

    - #### setGameState

          public void setGameState​(String value)

      Methods for accessing and updating values these are all synchronized so that the can be called by the main thread or the animation thread safely Change game state - set to "running" or "finished"

    - #### getGameState

          public String getGameState()

      Return game running state

    - #### setFast

          public void setFast​(Boolean value)

      Change game speed - false is normal speed, true is fast

    - #### getFast

          public Boolean getFast()

      Return game speed - false is normal speed, true is fast

    - #### getBat

          public GameObj getBat()

      Return bat object

    - #### getBall

          public GameObj getBall()

      return ball object

    - #### getBricks

          public GameObj[] getBricks()

      return bricks

    - #### getScore

          public int getScore()

      return score

    - #### addToScore

          public void addToScore​(int n)

      update the score

    - #### moveBat

          public void moveBat​(int direction)

      move the bat one step - -1 is left, +1 is right