Skip to content
Snippets Groups Projects
BulletAnimated.java 3.26 KiB
Newer Older
Jan Kožusznik's avatar
Jan Kožusznik committed
package lab;

import javafx.geometry.Point2D;
Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.geometry.Rectangle2D;
Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated  implements DrawableSimulable, Collisionable{
Jan Kožusznik's avatar
Jan Kožusznik committed

Jan Kožusznik's avatar
Jan Kožusznik committed
	private static final double STRENGTH_CANNON_COEFICIENT = 4.;
	
Jan Kožusznik's avatar
Jan Kožusznik committed
	private Point2D position;
	private Point2D start;
	private Point2D speed;
	private Point2D initialSpeed;
	private double size;
	private double mass = 2;
	private double cannonLength = 100;
Jan Kožusznik's avatar
Jan Kožusznik committed
	private boolean accelerate = false;
Jan Kožusznik's avatar
Jan Kožusznik committed
	private boolean hitToGround = false;

	private double crossSectionalArea;
	private double dragCoefficient = 0.47;
	
	private Image image;
	private World world;
	private Cannon cannon;
Jan Kožusznik's avatar
Jan Kožusznik committed
	private boolean fired;
Jan Kožusznik's avatar
Jan Kožusznik committed

Jan Kožusznik's avatar
Jan Kožusznik committed
	private HitListener hitListener = new EmptyHitListener();
	
Jan Kožusznik's avatar
Jan Kožusznik committed
	public BulletAnimated(World world, Cannon cannon) {
		this(world, cannon, new Point2D(0, 0), new Point2D(0, 0), 10);
	}

	public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) {
		this.start = start;
		this.position = this.start;
Jan Kožusznik's avatar
Jan Kožusznik committed
		this.initialSpeed = Point2D.ZERO;
Jan Kožusznik's avatar
Jan Kožusznik committed
		this.speed = speed;
		this.size = size;
		this.world = world;
		this.cannon = cannon;
		image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"), size, size,
				true, true);
	}

	public void draw(GraphicsContext gc) {
		gc.save();
		Point2D canvasPosition = world.getCanvasPoint(position);
		gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
		gc.restore();
	}

	public void simulate(double deltaT) {
Jan Kožusznik's avatar
Jan Kožusznik committed
		if (!fired) {
			return;
		}
Jan Kožusznik's avatar
Jan Kožusznik committed
		if (accelerate && start.distance(position) < cannonLength) {
			double cannonAngle = cannon.getAngle(); 
Jan Kožusznik's avatar
Jan Kožusznik committed
			double strenghtOfCannon = cannon.getStrength() * STRENGTH_CANNON_COEFICIENT/100.;
Jan Kožusznik's avatar
Jan Kožusznik committed
			speed = speed
					.add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon)
							.multiply(deltaT / mass));
Jan Kožusznik's avatar
Jan Kožusznik committed
		} else if (!hitToGround) {
			accelerate = false;
			Point2D airResistanceforce = new Point2D(
					-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * dragCoefficient * Math.pow(speed.getX(), 2),
					-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * 0.47 * Math.pow(speed.getY(), 2));
			Point2D acceleration = new Point2D(-airResistanceforce.getX() * mass,
koz01's avatar
koz01 committed
					(-Constants.GRAVITATIONAL_ACCELERATION/20 + airResistanceforce.getY()) * mass);
			speed = speed.add(acceleration.multiply(deltaT));
Jan Kožusznik's avatar
Jan Kožusznik committed
		}
		if (!hitToGround) {
			position = position.add(speed.multiply(deltaT*1000));
Jan Kožusznik's avatar
Jan Kožusznik committed
			if (!accelerate && position.getY() <= size / 2) {
				hitToGround = true;
				position = new Point2D(position.getX(), size / 2);
			}
		} else {
			reload();
		}
		
	}

Jan Kožusznik's avatar
Jan Kožusznik committed
	public Rectangle2D getBoundingBox() {
		return new Rectangle2D(position.getX(), position.getY(), size, size);
	}
	
	public boolean overlaps(Dragon dragon) {
		return getBoundingBox().intersects(dragon.getBoundingBox());
	}
	
	public void hitBy(Collisionable other) {
Jan Kožusznik's avatar
Jan Kožusznik committed
		hitListener.hit();
Jan Kožusznik's avatar
Jan Kožusznik committed
	public void reload() {
		position = start;
Jan Kožusznik's avatar
Jan Kožusznik committed
		speed = Point2D.ZERO;
Jan Kožusznik's avatar
Jan Kožusznik committed
		hitToGround = false;
Jan Kožusznik's avatar
Jan Kožusznik committed
		accelerate = false;
		fired = false;
	}

	public void fire() {
		if (fired) {
			return;
		}
		fired = true;
Jan Kožusznik's avatar
Jan Kožusznik committed
		accelerate = true;
Jan Kožusznik's avatar
Jan Kožusznik committed
		speed = initialSpeed;
Jan Kožusznik's avatar
Jan Kožusznik committed
	}
koz01's avatar
koz01 committed

	public void setHitListener(HitListener hitListenerImpl) {
		hitListener = hitListenerImpl;
	}
Jan Kožusznik's avatar
Jan Kožusznik committed
	
}