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

Solution

parent 505c8e48
No related merge requests found
...@@ -5,7 +5,7 @@ import javafx.geometry.Rectangle2D; ...@@ -5,7 +5,7 @@ import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image; import javafx.scene.image.Image;
public class BulletAnimated { public class BulletAnimated extends WorldEntity implements Collisionable {
private Point2D position; private Point2D position;
private Point2D start; private Point2D start;
...@@ -22,7 +22,6 @@ public class BulletAnimated { ...@@ -22,7 +22,6 @@ public class BulletAnimated {
private double dragCoefficient = 0.47; private double dragCoefficient = 0.47;
private Image image; private Image image;
private World world;
private Cannon cannon; private Cannon cannon;
public BulletAnimated(World world, Cannon cannon) { public BulletAnimated(World world, Cannon cannon) {
...@@ -30,24 +29,24 @@ public class BulletAnimated { ...@@ -30,24 +29,24 @@ public class BulletAnimated {
} }
public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) { public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) {
super(world);
this.start = start; this.start = start;
this.position = this.start; this.position = this.start;
this.initialSpeed = speed; this.initialSpeed = speed;
this.speed = speed; this.speed = speed;
this.size = size; this.size = size;
this.world = world;
this.cannon = cannon; this.cannon = cannon;
image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"), size, size, image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"), size, size,
true, true); true, true);
} }
public void draw(GraphicsContext gc) { @Override
gc.save(); protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position); Point2D canvasPosition = getWorld().getCanvasPoint(position);
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY()); gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
gc.restore();
} }
@Override
public void simulate(double deltaT) { public void simulate(double deltaT) {
if (accelerate && start.distance(position) < cannonLength) { if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle(); double cannonAngle = cannon.getAngle();
...@@ -89,5 +88,12 @@ public class BulletAnimated { ...@@ -89,5 +88,12 @@ public class BulletAnimated {
hitToGround = false; hitToGround = false;
accelerate = true; accelerate = true;
} }
@Override
public void hitBy(Collisionable collisionable) {
if (collisionable instanceof Dragon) {
reload();
}
}
} }
...@@ -5,24 +5,20 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -5,24 +5,20 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.transform.Affine; import javafx.scene.transform.Affine;
public class Cannon { public class Cannon extends WorldEntity {
private int direction=-1; private int direction=-1;
private double angle = 0; private double angle = 0;
private Point2D position; private Point2D position;
private Point2D size; private Point2D size;
private World world;
public Cannon(World world, Point2D position, Point2D size) { public Cannon(World world, Point2D position, Point2D size) {
super(); super(world);
this.world = world;
this.position = position; this.position = position;
this.size = size; this.size = size;
} }
@Override
public void simulate(double timeStep) { public void simulate(double timeStep) {
angle = angle + direction*80*timeStep; angle = angle + direction*80*timeStep;
if(angle <=-90 || angle >= 0) { if(angle <=-90 || angle >= 0) {
...@@ -30,9 +26,9 @@ public class Cannon { ...@@ -30,9 +26,9 @@ public class Cannon {
} }
} }
public void draw(GraphicsContext gc) { @Override
gc.save(); protected void drawInternal(GraphicsContext gc) {
Point2D worldPosition = world.getCanvasPoint(position); Point2D worldPosition = getWorld().getCanvasPoint(position);
gc.setFill(Color.BROWN); gc.setFill(Color.BROWN);
gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2); gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2);
gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40); gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40);
...@@ -40,7 +36,6 @@ public class Cannon { ...@@ -40,7 +36,6 @@ public class Cannon {
gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2))); gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2)));
gc.setFill(Color.BLACK); gc.setFill(Color.BLACK);
gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY()); gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY());
gc.restore();
} }
public double getAngle() { public double getAngle() {
......
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
Rectangle2D getBoundingBox();
void hitBy(Collisionable collisionable);
default void checkCollision(Object that) {
if (that instanceof Collisionable ca2
&& this.getBoundingBox().intersects(ca2.getBoundingBox())) {
ca2.hitBy(this);
}
}
}
...@@ -4,48 +4,53 @@ import javafx.geometry.Point2D; ...@@ -4,48 +4,53 @@ import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D; import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public class Dragon { public class Dragon extends WorldEntity implements Collisionable {
private Point2D position; private Point2D position;
private Point2D speed; private Point2D speed;
private final World world;
private final double size = 45; private final double size = 45;
public Dragon(World world, Point2D position, Point2D speed) { public Dragon(World world, Point2D position, Point2D speed) {
super(); super(world);
this.world = world;
this.position = position; this.position = position;
this.speed = speed; this.speed = speed;
} }
@Override
public void draw(GraphicsContext gc) { protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position); Point2D canvasPosition = getWorld().getCanvasPoint(position);
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size); gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size);
} }
@Override
public void simulate(double timeDelta) { public void simulate(double timeDelta) {
double timeDeltaS = timeDelta; double timeDeltaS = timeDelta;
double newX = (position.getX() + speed.getX() * timeDeltaS + world.getWidth()) % world.getWidth(); double newX = (position.getX() + speed.getX() * timeDeltaS + getWorld().getWidth()) % getWorld().getWidth();
double newY = (position.getY() + speed.getY() * timeDeltaS + world.getHeight()) % world.getHeight(); double newY = (position.getY() + speed.getY() * timeDeltaS + getWorld().getHeight()) % getWorld().getHeight();
position = new Point2D(newX, newY); position = new Point2D(newX, newY);
} }
@Override
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size); return new Rectangle2D(position.getX(), position.getY(), size, size);
} }
@Override
public void hitBy(Collisionable collisionable) {
if (collisionable instanceof BulletAnimated) {
hit();
}
}
public void hit() { public void hit() {
speed = speed.multiply(-1.); speed = speed.multiply(-1.);
} }
......
package lab;
import javafx.scene.canvas.GraphicsContext;
public interface DrawableSimulable {
void draw(GraphicsContext gc);
void simulate(double deltaT);
}
\ No newline at end of file
...@@ -6,19 +6,30 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -6,19 +6,30 @@ import javafx.scene.canvas.GraphicsContext;
public class World { public class World {
private double width; private double width;
private double height; private double height;
private BulletAnimated bulletAnimatted; /*private BulletAnimated bulletAnimatted;
private Cannon cannon; private Cannon cannon;
private Dragon[] dragons; private Dragon[] dragons;
*/
private DrawableSimulable[] entities;
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);
dragons = new Dragon[] { new Dragon(this, new Point2D(50, 200), new Point2D(100, 5)), 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, 230), new Point2D(60, 5)),
new Dragon(this, new Point2D(50, 270), new Point2D(-50, 20)) }; new Dragon(this, new Point2D(50, 270), new Point2D(-50, 20)) };
*/
Cannon cannon;
entities = new DrawableSimulable[] {
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)),
new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40),
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) { public Point2D getCanvasPoint(Point2D worldPoint) {
...@@ -27,22 +38,21 @@ public class World { ...@@ -27,22 +38,21 @@ public class World {
public void draw(GraphicsContext gc) { public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, width, height); gc.clearRect(0, 0, width, height);
cannon.draw(gc); for (DrawableSimulable entity: entities) {
bulletAnimatted.draw(gc); entity.draw(gc);
for(Dragon dragon: dragons) {
dragon.draw(gc);
} }
} }
public void simulate(double timeDelta) { public void simulate(double timeDelta) {
bulletAnimatted.simulate(timeDelta); for (DrawableSimulable entity: entities) {
cannon.simulate(timeDelta); entity.simulate(timeDelta);
for(Dragon dragon: dragons) { if (entity instanceof Collisionable ca) {
if (bulletAnimatted.overlaps(dragon)) { for (DrawableSimulable that: entities) {
dragon.hit(); if (entity != that) {
bulletAnimatted.reload(); ca.checkCollision(that);
}
}
} }
dragon.simulate(timeDelta);
} }
} }
......
package lab;
import javafx.scene.canvas.GraphicsContext;
public abstract class WorldEntity implements DrawableSimulable{
private final World world;
public WorldEntity(World world) {
this.world = world;
}
@Override
public final void draw(GraphicsContext gc) {
gc.save();
drawInternal(gc);
gc.restore();
}
protected World getWorld() {
return world;
}
protected abstract void drawInternal(GraphicsContext gc);
}
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