Skip to content
Snippets Groups Projects
Commit 9cfc8047 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

lab04

parent 0577580b
Branches
No related merge requests found
......@@ -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>
......
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;
......
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.);
}
}
......@@ -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() {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment