An error occurred while loading the file. Please try again.
-
jez04 authored993308a9
Boat.java 1.97 KiB
package lab.game;
import java.util.Random;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import lab.Config;
public class Boat extends WorldEntity implements Collisionable {
private static final Random RANDOM = new Random();
private Point2D speed;
private Image image;
public Boat(Scene scene, Point2D position) {
super(scene, position, 100);
image = new Image(Boat.class.getResourceAsStream("ship-boat.gif"));
speed = new Point2D(0, 0);
}
@Override
public void drawInternal(GraphicsContext gc) {
gc.drawImage(image, position.getX(), position.getY());
Rectangle2D rec = getBoundingBox();
gc.strokeRect(rec.getMinX(), rec.getMinY(), rec.getWidth(), rec.getHeight());
}
@Override
public void simulate(double deltaTime) {
position = position.add(speed.multiply(deltaTime / 1_000_000_000));
speed = speed.multiply(0.99);
}
@Override
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(),
position.getY() + image.getHeight() * (1 - Config.getInstance().getBoatCollisionHeight()),
image.getWidth(), image.getHeight() * Config.getInstance().getBoatCollisionHeight());
}
@Override
public boolean intersect(Rectangle2D another) {
return getBoundingBox().intersects(another);
}
@Override
public void hitBy(Collisionable another) {
if (another instanceof LochNess lochNess) {
int direction = RANDOM.nextBoolean() ? -1 : 1;
speed = new Point2D(-Config.getInstance().getBoatHitPulseX(),
direction * RANDOM.nextDouble(Config.getInstance().getBoatHitPulseYMin(),
Config.getInstance().getBoatHitPulseYMax()));
}
}
public void setPosInPercentage(double doubleValue) {
double colisionHeight = getBoundingBox().getHeight();
double posFromBottom = (scene.getSize().getHeight()-colisionHeight)*doubleValue/100;
position = new Point2D(position.getX(),
(scene.getSize().getHeight()-image.getHeight() -posFromBottom));
}
}