diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index d15f455068a863d18d38fc94253345605447d9a4..02124bcde46618641fc34de14bd9801b562a2da6 100644 --- a/src/main/java/lab/BulletAnimated.java +++ b/src/main/java/lab/BulletAnimated.java @@ -1,6 +1,7 @@ package lab; import javafx.geometry.Point2D; +import javafx.geometry.Rectangle2D; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; @@ -80,4 +81,8 @@ public class BulletAnimated { accelerate = true; } + public Rectangle2D getBoundingBox() { + return new Rectangle2D(position.getX(), position.getY(), size, size); + } + } diff --git a/src/main/java/lab/Dragon.java b/src/main/java/lab/Dragon.java new file mode 100644 index 0000000000000000000000000000000000000000..f733faac537493022918ef69fb5e7507763776d1 --- /dev/null +++ b/src/main/java/lab/Dragon.java @@ -0,0 +1,47 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.geometry.Rectangle2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; + +public class Dragon { + + private static final int SIZE = 40; + + private final Image image = new Image(Dragon.class.getResourceAsStream("dragon.gif")); + + private final World world; + + private Point2D position; + + private Point2D speed; + + + + public Dragon(World world, Point2D position, Point2D speed) { + this.world = world; + this.position = position; + this.speed = speed; + } + + public void draw(GraphicsContext gc) { + Point2D temp = world.getCanvasPoint(position, SIZE); + gc.drawImage(image, temp.getX(), temp.getY(), SIZE, SIZE); + } + + public void simulate(double deltaT) { + position = position.add(speed.multiply(deltaT)); + position = new Point2D((position.getX() + world.getWidth()) % world.getWidth() + , (position.getY() + world.getHeight()) % world.getHeight()); + } + + public Rectangle2D getBoundingBox() { + return new Rectangle2D(position.getX(), position.getY(), SIZE, SIZE); + } + + public void hit() { + speed = speed.multiply(-1); + + } +} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index 5215746393d68e3bf6f7074efc729a214492b359..70398d33f931c41b0a9c92fcfdd36b89f649062a 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -1,6 +1,7 @@ package lab; import javafx.geometry.Point2D; +import javafx.geometry.Rectangle2D; import javafx.scene.canvas.GraphicsContext; public class World { @@ -8,12 +9,23 @@ public class World { private double height; private BulletAnimated bulletAnimatted; private Cannon cannon; + private Dragon []dragons; + private double actualTime; + private double lastHit; public World(double width, double height) { this.width = width; this.height = height; cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40); + //init array + dragons = new Dragon[] { + new Dragon(this, new Point2D(50, 20), new Point2D(70, 50)) + ,new Dragon(this, new Point2D(150, 20), new Point2D(-140, 50)) + ,new Dragon(this, new Point2D(100, 70), new Point2D(7, 50)) + ,new Dragon(this, new Point2D(115, 900), new Point2D(170, 50)) + + ,new Dragon(this, new Point2D(60, 20), new Point2D(70, 50))}; } public Point2D getCanvasPoint(Point2D worldPoint, double heightOfEntity) { @@ -24,13 +36,31 @@ public class World { gc.clearRect(0, 0, width, height); cannon.draw(gc); bulletAnimatted.draw(gc); + //for each dragon from dragons call draw + for(Dragon dragon: dragons) { + dragon.draw(gc); + } } public void simulate(double timeDelta) { + actualTime += timeDelta; bulletAnimatted.simulate(timeDelta); cannon.simulate(timeDelta); + //for each dragon from dragons call simulate + for(Dragon dragon: dragons) { + dragon.simulate(timeDelta); + } + if (actualTime - lastHit > 1) { + Rectangle2D bbOfBullet = bulletAnimatted.getBoundingBox(); + for(Dragon dragon: dragons) { + if (dragon.getBoundingBox().intersects(bbOfBullet)) { + dragon.hit(); + lastHit = actualTime; + } + } + } } - + public double getWidth() { return width; }