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

lab04-02-solution

parent 0f74c9f3
No related merge requests found
Pipeline #77 failed with stages
in 0 seconds
...@@ -5,9 +5,8 @@ import javafx.geometry.Rectangle2D; ...@@ -5,9 +5,8 @@ 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 start; private Point2D start;
private Point2D speed; private Point2D speed;
private Point2D initialSpeed; private Point2D initialSpeed;
...@@ -22,7 +21,6 @@ public class BulletAnimated { ...@@ -22,7 +21,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,27 +28,20 @@ public class BulletAnimated { ...@@ -30,27 +28,20 @@ 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(start, world);
this.start = start; this.start = 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();
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
gc.restore();
}
public void simulate(double deltaT) { public void simulate(double deltaT) {
double timeStep = deltaT * 1000; double timeStep = deltaT * 1000;
if (accelerate && start.distance(position) < cannonLength) { if (accelerate && start.distance(getPosition()) < cannonLength) {
double cannonAngle = cannon.getAngle(); double cannonAngle = cannon.getAngle();
speed = speed speed = speed
.add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon) .add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon)
...@@ -65,10 +56,10 @@ public class BulletAnimated { ...@@ -65,10 +56,10 @@ public class BulletAnimated {
speed = speed.add(acceleration.multiply(timeStep / 1000)); speed = speed.add(acceleration.multiply(timeStep / 1000));
} }
if (!hitToGround) { if (!hitToGround) {
position = position.add(speed); setPosition(getPosition().add(speed));
if (!accelerate && position.getY() <= size / 2) { if (!accelerate && getPosition().getY() <= size / 2) {
hitToGround = true; hitToGround = true;
position = new Point2D(position.getX(), size / 2); setPosition(new Point2D(getPosition().getX(), size / 2));
} }
} else { } else {
reload(); reload();
...@@ -78,7 +69,12 @@ public class BulletAnimated { ...@@ -78,7 +69,12 @@ public class BulletAnimated {
} }
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size); return new Rectangle2D(getPosition().getX(), getPosition().getY(), size, size);
}
@Override
public void hitBy(Collisionable other) {
reload();
} }
public boolean overlaps(Dragon dragon) { public boolean overlaps(Dragon dragon) {
...@@ -86,10 +82,15 @@ public class BulletAnimated { ...@@ -86,10 +82,15 @@ public class BulletAnimated {
} }
public void reload() { public void reload() {
position = start; setPosition(start);
speed = initialSpeed; speed = initialSpeed;
hitToGround = false; hitToGround = false;
accelerate = true; accelerate = true;
} }
@Override
protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = getWorld().getCanvasPoint(getPosition());
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
}
} }
...@@ -5,21 +5,15 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -5,21 +5,15 @@ 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 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(position, world);
this.world = world;
this.position = position;
this.size = size; this.size = size;
} }
...@@ -30,9 +24,14 @@ public class Cannon { ...@@ -30,9 +24,14 @@ public class Cannon {
} }
} }
public void draw(GraphicsContext gc) {
gc.save(); public double getAngle() {
Point2D worldPosition = world.getCanvasPoint(position); return (angle * -1) / 180 * Math.PI;
}
@Override
protected void drawInternal(GraphicsContext gc) {
Point2D worldPosition = getWorld().getCanvasPoint(getPosition());
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,10 +39,5 @@ public class Cannon { ...@@ -40,10 +39,5 @@ 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() {
return (angle * -1) / 180 * Math.PI;
} }
} }
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
void hitBy(Collisionable other);
default boolean intersects(Collisionable other) {
return getBoundingBox().intersects(other.getBoundingBox());
}
Rectangle2D getBoundingBox();
}
...@@ -4,49 +4,48 @@ import javafx.geometry.Point2D; ...@@ -4,49 +4,48 @@ 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 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(position, world);
this.world = world;
this.position = position;
this.speed = speed; this.speed = speed;
} }
public void draw(GraphicsContext gc) { protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position); Point2D canvasPosition = getWorld().getCanvasPoint(getPosition());
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size); gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size);
} }
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 = (getPosition().getX() + speed.getX() * timeDeltaS + getWorld().getWidth()) % getWorld().getWidth();
double newY = (position.getY() + speed.getY() * timeDeltaS + world.getHeight()) % world.getHeight(); double newY = (getPosition().getY() + speed.getY() * timeDeltaS + getWorld().getHeight()) % getWorld().getHeight();
position = new Point2D(newX, newY); setPosition(new Point2D(newX, newY));
} }
@Override
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size); return new Rectangle2D(getPosition().getX(), getPosition().getY(), size, size);
}
public void hit() {
speed = speed.multiply(-1.);
} }
@Override
public void hitBy(Collisionable other) {
if (!(other instanceof Dragon)) {
hit();
}
}
public void hit() {
speed = speed.multiply(-1.);
}
} }
package lab;
import javafx.scene.canvas.GraphicsContext;
public interface DrawableSimulable {
void draw(GraphicsContext gc);
void simulate(double deltaT);
}
...@@ -7,19 +7,17 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -7,19 +7,17 @@ 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 DrawableSimulable[] entities;
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 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); BulletAnimated bullet = 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)), entities = new DrawableSimulable[] {cannon, bullet, 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))};
} }
public Point2D getCanvasPoint(Point2D worldPoint) { public Point2D getCanvasPoint(Point2D worldPoint) {
...@@ -29,22 +27,26 @@ public class World { ...@@ -29,22 +27,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.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
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) {
if (bulletAnimatted.overlaps(dragon)) { Collisionable col1 = (Collisionable) entity;
dragon.hit(); for (DrawableSimulable entity2: entities) {
bulletAnimatted.reload(); if (entity2 != entity && entity2 instanceof Collisionable) {
Collisionable col2 = (Collisionable) entity2;
if (col1.intersects(col2)) {
col1.hitBy(col2);
col2.hitBy(col1);
}
}
}
} }
dragon.simulate(timeDelta);
} }
} }
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class WorldEntity implements DrawableSimulable{
private Point2D position;
private final World world;
public WorldEntity(Point2D position, World world) {
this.position = position;
this.world = world;
}
@Override
public void draw(GraphicsContext gc) {
gc.save();
drawInternal(gc);
gc.restore();
}
protected abstract void drawInternal(GraphicsContext gc);
protected Point2D getPosition() {
return position;
}
protected void setPosition(Point2D position) {
this.position = position;
}
protected World getWorld() {
return world;
}
}
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