Package pong.server.model

Source Code of pong.server.model.Player

package pong.server.model;

import pong.common.GlobalSettings;
import pong.common.PlayerMovementInformation;
import pong.common.Position;

/**
* This class stores information about a player: whether he is connected, how he is moving and his
* position.
*
* @author Lorenzo Gatto
*
*/
public class Player {
  private Boolean connected;
  private PlayerMovementInformation movement;
  private Position position;

  /**
   * This creates a disconnected player an a (0, 0) position, without information on movement.
   */
  public Player() {
    super();
    this.connected = false;
    this.movement = null;
    this.position = new Position();
  }

  /**
   * This makes the player go to the default position for the round beginning.
   *
   * @param playerIndex
   *            the player index
   */
  public synchronized void initializeForRoundPlaying(final int playerIndex) {
    this.movement = new PlayerMovementInformation();
    if (playerIndex == 0) {
      this.position.setX(GlobalSettings.RACKETS_DISTANCE_EDGES);
    } else {
      this.position.setX(GlobalSettings.FIELD_WIDTH - GlobalSettings.RACKET_WIDTH
          - GlobalSettings.RACKETS_DISTANCE_EDGES);
    }
    this.position
        .setY(GlobalSettings.FIELD_HEIGHT / 2.0f - GlobalSettings.RACKET_HEIGHT / 2.0f);
  }

  /**
   * Check if the player is still connected.
   *
   * @return connection status
   */
  public synchronized boolean isConnected() {
    return this.connected;
  }

  /**
   * Set the player to be connected or disconnected.
   *
   * @param connected
   *            connection status
   */
  public synchronized void setConnected(final Boolean connected) {
    this.connected = connected;
  }

  /**
   * Get the player position.
   *
   * @return the position of the upper-left corner in units.
   */
  public synchronized Position getPosition() {
    return this.position;
  }

  /**
   * Set the player position.
   *
   * @param position
   *            the position of the upper-left corner in units.
   */
  public synchronized void setPosition(final Position position) {
    this.position = position;
  }

  /**
   * Set information about the player movement.
   *
   * @param movInf
   *            a {@link pong.common.PlayerMovementInformation}
   */
  public synchronized void setMovementInformation(final PlayerMovementInformation movInf) {
    this.movement = movInf;
  }

  /**
   * Is the player trying to go up?
   *
   * @return whether the UP button is pressed or not
   */
  public synchronized boolean isGoingUp() {
    return this.movement.isGoingUp();
  }

  /**
   * Is the player trying to go down?
   *
   * @return whether the DOWN button is pressed or not
   */
  public synchronized boolean isGoingDown() {
    return this.movement.isGoingDown();
  }
}
TOP

Related Classes of pong.server.model.Player

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.