/* Copyright 2010, 2013 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.PonkOut;
import ponkOut.graphics.GraphicsObjectsManager;
import ponkOut.graphics.Material;
public class GameBall extends Ball {
/** speed of ball after detached from paddle */
protected static final float INITIAL_SPEED = 10.0f;
/** is ball attached to a paddle */
private boolean attached;
/**
* the last paddle which had contact with ball; if attached the paddle the
* ball is attached to
*/
private Paddle lastPaddle;
public GameBall(PlayerPaddle paddleAttached, float radius, GraphicsObjectsManager goManager) {
super(Material.getPresetMaterial(Material.PresetMaterial.BRASS), radius, goManager);
attached = true;
lastPaddle = paddleAttached;
paddleAttached.attachBall(this);
// place ball at a small distance from paddle
Vector2f paddlePos = paddleAttached.getPosition();
Vector2f dir = new Vector2f();
paddlePos.negate(dir);
dir.normalise();
dir.scale(1.5f * radius + 1.5f * paddleAttached.getRadius());
Vector2f.add(paddlePos, dir, dir);
setPosition(dir);
}
public GameBall(Vector2f position, Vector2f velocity, float radius, GraphicsObjectsManager goManager) {
super(Material.getPresetMaterial(Material.PresetMaterial.BRASS), radius, goManager);
attached = false;
lastPaddle = null;
setPosition(position);
setVelocity(velocity);
}
public GameBall(PlayerPaddle paddleAttached, GraphicsObjectsManager goManager) {
this(paddleAttached, PonkOut.COMMON_RADIUS, goManager);
}
public GameBall(Vector2f position, Vector2f velocity, GraphicsObjectsManager goManager) {
this(position, velocity, PonkOut.COMMON_RADIUS, goManager);
}
public GameBall(Vector2f position, float radius, GraphicsObjectsManager goManager) {
this(position, new Vector2f(0.0f, 0.0f), radius, goManager);
}
public GameBall(Vector2f position, GraphicsObjectsManager goManager) {
this(position, new Vector2f(0.0f, 0.0f), PonkOut.COMMON_RADIUS, goManager);
}
@Override
public boolean isAttached() {
return attached;
}
@Override
public void detach() {
if (attached) {
((PlayerPaddle) lastPaddle).detachBall();
attached = false;
}
}
/** throws ball away from paddle */
public void serve() {
if (attached) {
// add random velocity away from paddle
Vector2f away = new Vector2f();
Vector2f.sub(getPosition(), lastPaddle.getPosition(), away);
away.normalise();
Vector2f.add(away, new Vector2f((float) Math.random() - 0.5f, (float) Math.random() - 0.5f), away);
away.normalise();
away.scale(INITIAL_SPEED);
Vector2f v = getVelocity();
Vector2f.add(v, away, v);
setVelocity(v);
detach();
}
}
@Override
public boolean isMovable() {
return true;
}
@Override
public Paddle getLastPaddle() {
return lastPaddle;
}
@Override
public void setLastPaddle(Paddle paddle) {
lastPaddle = paddle;
}
}