From 9cfc804755f92955a0d9f895d21098080ef4ee16 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Tue, 21 Sep 2021 19:50:43 +0200
Subject: [PATCH] lab04

---
 pom.xml                               |  2 +-
 src/main/java/lab/BulletAnimated.java |  9 +++++
 src/main/java/lab/Dragon.java         | 52 +++++++++++++++++++++++++++
 src/main/java/lab/World.java          | 14 ++++++++
 4 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/lab/Dragon.java

diff --git a/pom.xml b/pom.xml
index 31f71f2..ccf17e2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>vsb-cs-java1</groupId>
-	<artifactId>lab03</artifactId>
+	<artifactId>lab04</artifactId>
 	<version>0.0.1-SNAPHOST</version>
 	<packaging>jar</packaging>
 	<properties>
diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java
index 0d953f5..27eae34 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;
 
@@ -76,6 +77,14 @@ public class BulletAnimated {
 		
 	}
 
+	public Rectangle2D getBoundingBox() {
+		return new Rectangle2D(position.getX(), position.getY(), size, size);
+	}
+	
+	public boolean overlaps(Dragon dragon) {
+		return getBoundingBox().intersects(dragon.getBoundingBox());
+	}
+	
 	public void reload() {
 		position = start;
 		speed = initialSpeed;
diff --git a/src/main/java/lab/Dragon.java b/src/main/java/lab/Dragon.java
new file mode 100644
index 0000000..216628a
--- /dev/null
+++ b/src/main/java/lab/Dragon.java
@@ -0,0 +1,52 @@
+package lab;
+
+import javafx.geometry.Point2D;
+import javafx.geometry.Rectangle2D;
+import javafx.scene.canvas.GraphicsContext;
+
+public class Dragon {
+	
+	private Point2D position;
+	
+	private Point2D speed;
+	
+	private final World world;
+		
+	private final double size = 45;
+
+	public Dragon(World world, Point2D position, Point2D speed) {
+		super();
+		this.world = world;
+		this.position = position;
+		this.speed = speed;
+	}
+
+	
+	public void draw(GraphicsContext gc) {
+		Point2D canvasPosition = world.getCanvasPoint(position);
+		gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size);
+	}
+
+
+	public void simulate(double timeDelta) {
+		double timeDeltaS = timeDelta;
+		double newX = (position.getX() + speed.getX() * timeDeltaS + world.getWidth()) % world.getWidth(); 
+		double newY = (position.getY() + speed.getY() * timeDeltaS + world.getHeight()) % world.getHeight();
+		position = new Point2D(newX, newY);
+	}
+
+
+	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 e8c6219..ce631f9 100644
--- a/src/main/java/lab/World.java
+++ b/src/main/java/lab/World.java
@@ -9,6 +9,7 @@ public class World {
 	private double height;
 	private BulletAnimated bulletAnimatted;
 	private Cannon cannon;
+	private Dragon[] dragons;
 
 	public World(double width, double height) {
 		super();
@@ -16,6 +17,9 @@ public class World {
 		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);
+		dragons = new Dragon[] { new Dragon(this, new Point2D(50, 200), new Point2D(100, 5)),
+				new Dragon(this, new Point2D(50, 230), new Point2D(60, 5)),
+				new Dragon(this, new Point2D(50, 270), new Point2D(-50, 20)) };
 	}
 
 	public Point2D getCanvasPoint(Point2D worldPoint) {
@@ -27,11 +31,21 @@ public class World {
 		gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
 		cannon.draw(gc);
 		bulletAnimatted.draw(gc);
+		for(Dragon dragon: dragons) {
+			dragon.draw(gc);
+		}
 	}
 
 	public void simulate(double timeDelta) {
 		bulletAnimatted.simulate(timeDelta);
 		cannon.simulate(timeDelta);
+		for(Dragon dragon: dragons) {
+			if (bulletAnimatted.overlaps(dragon)) {
+				dragon.hit();
+				bulletAnimatted.reload();
+			}
+			dragon.simulate(timeDelta);
+		}
 	}
 
 	public double getWidth() {
-- 
GitLab