Skip to content
Snippets Groups Projects
Commit 41757ada authored by koz01's avatar koz01
Browse files

Solution_Wed_10-45

parent ba626e1a
No related merge requests found
Pipeline #67 failed with stages
in 0 seconds
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 getBB() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
}
public boolean isInCannon() {
return accelerate;
}
}
package lab;
import static lab.Constants.DRAGON_IMAGE;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class Dragon {
private static final double SIZE_OF_DRAGON = 50;
private final World world;
private Point2D position;
private Point2D velocity;
private Point2D dimension;
private final Image image;
public Dragon(World world, Point2D position, Point2D velocity) {
this(world, position, velocity, new Point2D(SIZE_OF_DRAGON, SIZE_OF_DRAGON));
}
public Dragon(World world, Point2D position, Point2D velocity, Point2D dimension) {
this.world = world;
this.position = position;
this.velocity = velocity;
this.dimension = dimension;
this.image = DRAGON_IMAGE;
}
public void draw(GraphicsContext gc) {
Point2D p = world.getCanvasPoint(position);
gc.drawImage(image, p.getX(), p.getY(), dimension.getX(), dimension.getY());
}
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 getBB() {
return new Rectangle2D(position.getX(), position.getY(), dimension.getX(), dimension.getY());
}
public void hit() {
velocity = new Point2D(-velocity.getX(), -velocity.getY());
}
}
......@@ -8,12 +8,19 @@ public class World {
private double height;
private BulletAnimated bulletAnimatted;
private Cannon cannon;
private final 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);
dragons = new Dragon[] {
new Dragon(this, new Point2D(110, 90), new Point2D(70, 80)),
new Dragon(this, new Point2D(70, 90), new Point2D(70, -80)),
new Dragon(this, new Point2D(160, 70), new Point2D(170, 20)),
new Dragon(this, new Point2D(110, 90), new Point2D(-70, 80)),
new Dragon(this, new Point2D(110, 90), new Point2D(-50, -80))};
}
public Point2D getCanvasPoint(Point2D worldPoint) {
......@@ -24,11 +31,27 @@ public class World {
gc.clearRect(0, 0, width, height);
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) {
dragon.simulate(timeDelta);
}
if (bulletAnimatted.isInCannon()) {
return;
}
for (Dragon dragon : dragons) {
if (dragon.getBB().intersects(bulletAnimatted.getBB())) {
bulletAnimatted.reload();;
dragon.hit();
}
}
}
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