From 4810e11fff63b32ccbe2cc829af500095ee9990b Mon Sep 17 00:00:00 2001
From: koz01 <koz01@PCFEIB207-060.msad.vsb.cz>
Date: Wed, 12 Oct 2022 08:40:35 +0200
Subject: [PATCH] solution_Wed_7-15

---
 src/main/java/lab/BulletAnimated.java |  9 +++++++
 src/main/java/lab/Dragon.java         | 12 ++++++++-
 src/main/java/lab/World.java          | 38 ++++++++++++++++++++++++---
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java
index 4ec882b..086eb9a 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;
 
@@ -75,4 +76,12 @@ public class BulletAnimated {
 		accelerate = true;
 	}
 	
+	public Rectangle2D getboundingBox() {
+		return new Rectangle2D(position.getX(), position.getY(), size, size);
+	}
+
+	public boolean isInCannon() {
+		return accelerate;
+	}
+	
 }
diff --git a/src/main/java/lab/Dragon.java b/src/main/java/lab/Dragon.java
index e44374b..396c8b7 100644
--- a/src/main/java/lab/Dragon.java
+++ b/src/main/java/lab/Dragon.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;
 
@@ -11,7 +12,7 @@ public class Dragon {
 	
 	private Point2D position;
 	
-	private final Point2D velocity;
+	private Point2D velocity;
 	
 	private final Point2D dimension;
 	
@@ -37,5 +38,14 @@ public class Dragon {
 	
 	public void simulate(double deltaT) {
 		position = position.add(velocity.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(), dimension.getX(), dimension.getY());
+	}
+
+	public void hit() {
+		velocity = new Point2D(-velocity.getX(), -velocity.getY());
 	}
 }
diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java
index 7ae56d2..ef5fd37 100644
--- a/src/main/java/lab/World.java
+++ b/src/main/java/lab/World.java
@@ -8,14 +8,29 @@ public class World {
 	private double height;
 	private BulletAnimated bulletAnimatted;
 	private Cannon cannon;
-	private Dragon dragon;
+	private Dragon []dragons;
 	
 	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);
-		dragon = new Dragon(this, new Point2D(100, 100), new Point2D(40, 40));
+		/*
+		  dragons = new Dragon[5];
+		 
+		dragons[0] = new Dragon(this, new Point2D(100, 100), new Point2D(40, 40));
+		dragons[1] = new Dragon(this, new Point2D(120, 100), new Point2D(-40, 40));
+		dragons[2] = new Dragon(this, new Point2D(120, 50), new Point2D(-70, 140));
+		dragons[3] = new Dragon(this, new Point2D(20, 100), new Point2D(-40, 100));
+		dragons[4] = new Dragon(this, new Point2D(120, 100), new Point2D(-40, 80));
+		*/
+		dragons = new Dragon[] {
+				new Dragon(this, new Point2D(100, 100), new Point2D(40, 40)),
+				new Dragon(this, new Point2D(120, 100), new Point2D(-40, 40)),
+				new Dragon(this, new Point2D(120, 50), new Point2D(-70, 140)),
+				new Dragon(this, new Point2D(20, 100), new Point2D(-40, 100)),
+				new Dragon(this, new Point2D(120, 100), new Point2D(-40, 80))
+		};
 	}
 
 	public Point2D getCanvasPoint(Point2D worldPoint) {
@@ -26,13 +41,28 @@ public class World {
 		gc.clearRect(0, 0, width, height);
 		cannon.draw(gc);
 		bulletAnimatted.draw(gc);
-		dragon.draw(gc);
+		for (Dragon dragon: dragons) {
+			dragon.draw(gc);
+		}
 	}
 
 	public void simulate(double timeDelta) {
 		bulletAnimatted.simulate(timeDelta);
 		cannon.simulate(timeDelta);
-		dragon.simulate(timeDelta);
+		for (Dragon dragon: dragons) {
+			dragon.simulate(timeDelta);
+		}
+		if (bulletAnimatted.isInCannon()) {
+			return;
+		}
+		
+		for (Dragon dragon: dragons) {
+			if (dragon.getboundingBox().intersects(bulletAnimatted.getboundingBox())) {
+				dragon.hit();
+				bulletAnimatted.reload();
+			}
+		}
+		
 	}
 
 	public double getWidth() {
-- 
GitLab