Package ponkOut.logic

Source Code of ponkOut.logic.PlayerPaddle

/* Copyright 2010, 2012 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.logic;

import org.lwjgl.util.vector.Vector2f;

import ponkOut.Settings;
import ponkOut.graphics.GraphicsObjectsManager;
import ponkOut.graphics.PaddleGO;
import ponkOut.input.InputManager;

public class PlayerPaddle extends Paddle {
  /** player 0 or 1 */
  private int player;
  private boolean isBallAttached;
  private GameBall attachedBall;

  public PlayerPaddle(int player, Place place, float length, GraphicsObjectsManager goManager) {
    super(place, length, PaddleGO.getMaterial(Settings.getInstance().getPaddleMaterial(player)), goManager);

    this.player = player;

    isBallAttached = false;
    attachedBall = null;
  }

  /**
   * create paddle with standard length
   */
  public PlayerPaddle(int player, Place place, GraphicsObjectsManager goManager) {
    this(player, place, 3.0f, goManager);
  }

  public void setLength(float length) {
    this.length = length;
  }

  public void attachBall(GameBall ball) {
    isBallAttached = true;
    attachedBall = ball;
  }

  public void detachBall() {
    if (isBallAttached) {
      isBallAttached = false;
      attachedBall.detach();
      attachedBall = null;
    }
  }

  public void serve() {
    if (isBallAttached) {
      isBallAttached = false;
      attachedBall.serve();
      attachedBall = null;
    }
  }

  @Override
  public void applyInput(float elapsedTime) {
    Vector2f movement = InputManager.getInstance().getMovement(player, elapsedTime);

    switch (place) {
    case TOP:
    case BOTTOM:
      setVelocity(new Vector2f(movement.x, 0.0f));
      break;

    case LEFT:
    case RIGHT:
      setVelocity(new Vector2f(0.0f, movement.y));
      break;
    }

    if (isBallAttached) {
      attachedBall.setVelocity(getVelocity());
      if (InputManager.getInstance().isButtonDown(player)) {
        serve();
      }
    }
  }

  @Override
  public boolean isMovable() {
    return true;
  }

  @Override
  public boolean isBallAttached() {
    return isBallAttached;
  }

  @Override
  public GameBall getAttachedBall() {
    return attachedBall;
  }
}
TOP

Related Classes of ponkOut.logic.PlayerPaddle

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.