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

Solution

parent 4d4c762b
No related merge requests found
package lab; package lab;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image; import javafx.scene.image.Image;
...@@ -82,4 +83,7 @@ public class BulletAnimated { ...@@ -82,4 +83,7 @@ public class BulletAnimated {
accelerate = true; accelerate = true;
} }
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
}
} }
...@@ -13,7 +13,11 @@ public final class Constants { ...@@ -13,7 +13,11 @@ public final class Constants {
public static final Image DRAGON_IMAGE; public static final Image DRAGON_IMAGE;
public static final Image DESERT_IMAGE;
static{ static{
DRAGON_IMAGE = new Image(Constants.class.getResourceAsStream("dragon.gif")); DRAGON_IMAGE = new Image(Constants.class.getResourceAsStream("dragon.gif"));
DESERT_IMAGE = new Image(Constants.class.getResourceAsStream("desert-1654439__340.jpeg"));
} }
} }
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 DRAGON_SIZE = 50;
private Point2D pos;
private Point2D vel;
private Image image;
private World world;
public Dragon(World w, Point2D pos, Point2D vel) {
this.pos = pos;
this.vel = vel;
this.image = Constants.DRAGON_IMAGE;
this.world = w;
}
public void draw(GraphicsContext gc) {
Point2D p = world.getCanvasPoint(pos);
gc.drawImage(image, p.getX(), p.getY(), DRAGON_SIZE, DRAGON_SIZE);
}
public void simulate(double deltaT) {
pos = pos.add(vel.multiply(deltaT));
pos = new Point2D((pos.getX() +world.getWidth())% world.getWidth(), (pos.getY()+world.getHeight()) % world.getHeight());
}
public Rectangle2D getBoundingBox() {
return new Rectangle2D(pos.getX(), pos.getY(), DRAGON_SIZE, DRAGON_SIZE);
}
public void hit() {
vel = vel.multiply(-1);
}
}
package lab; package lab;
import java.util.Random;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
...@@ -9,13 +11,24 @@ public class World { ...@@ -9,13 +11,24 @@ public class World {
private double height; private double height;
private BulletAnimated bulletAnimatted; private BulletAnimated bulletAnimatted;
private Cannon cannon; private Cannon cannon;
private Dragon []dragons;
public World(double width, double height) { public World(double width, double height) {
super(); super();
this.width = width; this.width = width;
this.height = height; this.height = height;
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); 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); bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
Random rnd = new Random();
dragons = new Dragon[5];
for (int i = 0; i < dragons.length; i++) {
int x = rnd.nextInt((int) width);
int y = rnd.nextInt((int) height);
int v_x = rnd.nextInt(10) - 5;
int v_y = rnd.nextInt(10) - 5;
dragons[i] = new Dragon(this, new Point2D(x, y), new Point2D(v_x*10, v_y*10));
}
} }
public Point2D getCanvasPoint(Point2D worldPoint) { public Point2D getCanvasPoint(Point2D worldPoint) {
...@@ -24,14 +37,26 @@ public class World { ...@@ -24,14 +37,26 @@ public class World {
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D(); GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); gc.drawImage(Constants.DESERT_IMAGE, 0, 0, width, height);
cannon.draw(gc); cannon.draw(gc);
bulletAnimatted.draw(gc); bulletAnimatted.draw(gc);
for (Dragon d: dragons) {
d.draw(gc);
}
} }
public void simulate(int timeDelta) { public void simulate(int timeDelta) {
bulletAnimatted.simulate(timeDelta); bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta); cannon.simulate(timeDelta);
for (Dragon d: dragons) {
d.simulate(timeDelta/1000.0);
}
for (Dragon d: dragons) {
if (d.getBoundingBox().intersects(bulletAnimatted.getBoundingBox())) {
d.hit();
bulletAnimatted.reload();
}
}
} }
public double getWidth() { public double getWidth() {
......
src/main/resources/lab/desert-1654439__340.jpeg

16.5 KiB

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