Skip to content
Snippets Groups Projects
Commit db973f5a authored by jez04's avatar jez04
Browse files

feat: :tada: solution lab04

parent 3fdb1696
Branches
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.paint.Color; import javafx.scene.paint.Color;
public class Bullet { public class Bullet extends WorldEntity implements Collisionable{
private static final double SIZE = 20; private static final double SIZE = 20;
private final Point2D acceleration; protected final Point2D acceleration;
protected Point2D velocity;
private final World world;
private Point2D position;
private Point2D velocity;
public Bullet(World world, Point2D position, Point2D velocity, Point2D acceleration) { public Bullet(World world, Point2D position, Point2D velocity, Point2D acceleration) {
this.world = world; super(world, position);
this.position = position;
this.velocity = velocity; this.velocity = velocity;
this.acceleration = acceleration; this.acceleration = acceleration;
} }
public void draw(GraphicsContext gc) { @Override
public void drawInternal(GraphicsContext gc) {
gc.setFill(Color.SILVER); gc.setFill(Color.SILVER);
gc.fillOval(position.getX(), position.getY(), SIZE, SIZE); gc.fillOval(position.getX(), position.getY(), SIZE, SIZE);
} }
@Override
public void simulate(double deltaT) { public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT)); position = position.add(velocity.multiply(deltaT));
velocity = velocity.add(acceleration.multiply(deltaT)); velocity = velocity.add(acceleration.multiply(deltaT));
} }
protected Point2D getPosition() { @Override
return position; public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), SIZE, SIZE);
}
@Override
public boolean intersect(Rectangle2D another) {
return getBoundingBox().intersects(another);
} }
@Override
public void hitBy(Collisionable another) {
}
} }
...@@ -5,40 +5,35 @@ import javafx.geometry.Rectangle2D; ...@@ -5,40 +5,35 @@ 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 Bullet {
private static final double SIZE = 40; private static final double SIZE = 40;
private final World world; private final Point2D initVelocity;
private Cannon cannon;
private Image image = new Image(this.getClass().getResourceAsStream("fireball-transparent.gif")); private Image image = new Image(this.getClass().getResourceAsStream("fireball-transparent.gif"));
public BulletAnimated(World world, Point2D position, Point2D velocity, Point2D acceleration) { public BulletAnimated(World world, Cannon cannon, Point2D position, Point2D velocity, Point2D acceleration) {
this.world = world; super(world, position, velocity, acceleration);
this.position = position; this.initVelocity = velocity;
this.velocity = velocity; this.cannon = cannon;
this.acceleration = acceleration;
} }
public void draw(GraphicsContext gc) { @Override
public void drawInternal(GraphicsContext gc) {
gc.drawImage(image, getPosition().getX(), getPosition().getY(), SIZE, SIZE); gc.drawImage(image, getPosition().getX(), getPosition().getY(), SIZE, SIZE);
gc.strokeRect(position.getX(), position.getY(), SIZE, SIZE); gc.strokeRect(position.getX(), position.getY(), SIZE, SIZE);
} }
private final Point2D acceleration; @Override
public void hitBy(Collisionable another) {
private Point2D position; if (another instanceof Ufo) {
private Point2D velocity; reload();
}
public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT));
velocity = velocity.add(acceleration.multiply(deltaT));
}
protected Point2D getPosition() {
return position;
} }
public Rectangle2D getBoundingBox() { public void reload() {
return new Rectangle2D(position.getX(), position.getY(), SIZE,SIZE); position = cannon.getPosition();
velocity = initVelocity;
} }
} }
\ No newline at end of file
...@@ -6,31 +6,27 @@ import javafx.scene.paint.Color; ...@@ -6,31 +6,27 @@ import javafx.scene.paint.Color;
import javafx.scene.transform.Affine; import javafx.scene.transform.Affine;
import javafx.scene.transform.Transform; import javafx.scene.transform.Transform;
public class Cannon { public class Cannon extends WorldEntity{
private static final double LENGTH = 60; private static final double LENGTH = 60;
private static final double WIDTH = 15; private static final double WIDTH = 15;
private final World world;
private Point2D position;
private double angle; private double angle;
private double angleDelta = -25; private double angleDelta = -25;
public Cannon(World world, Point2D position, double angle) { public Cannon(World world, Point2D position, double angle) {
this.world = world; super(world, position);
this.position = position;
this.angle = angle; this.angle = angle;
} }
public void draw(GraphicsContext gc) { @Override
gc.save(); public void drawInternal(GraphicsContext gc) {
gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY() + WIDTH / 2))); gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY() + WIDTH / 2)));
gc.setFill(Color.BROWN); gc.setFill(Color.BROWN);
gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH); gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH);
gc.restore();
} }
@Override
public void simulate(double deltaT) { public void simulate(double deltaT) {
// do nothing yet
angle += angleDelta * deltaT; angle += angleDelta * deltaT;
if (angle >= 0 || angle <= -90) { if (angle >= 0 || angle <= -90) {
angleDelta = -angleDelta; angleDelta = -angleDelta;
......
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
Rectangle2D getBoundingBox();
boolean intersect(Rectangle2D another);
void hitBy(Collisionable another);
}
package lab;
import javafx.scene.canvas.GraphicsContext;
public interface DrawableSimulable {
void draw(GraphicsContext gc);
void simulate(double deltaT);
}
...@@ -7,44 +7,54 @@ import javafx.geometry.Rectangle2D; ...@@ -7,44 +7,54 @@ 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 Ufo { public class Ufo extends WorldEntity implements Collisionable {
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
private Image image = new Image(this.getClass().getResourceAsStream("ufo-small.gif")); private Image image = new Image(this.getClass().getResourceAsStream("ufo-small.gif"));
private final World world;
private Point2D position;
private Point2D velocity; private Point2D velocity;
public Ufo(World world) { public Ufo(World world) {
this(world, this(world, new Point2D(RANDOM.nextDouble(world.getWidth()), RANDOM.nextDouble(0, world.getHeight() * 0.3)),
new Point2D(RANDOM.nextDouble(world.getWidth()),
RANDOM.nextDouble(0, world.getHeight()*0.3)),
new Point2D(RANDOM.nextDouble(70, 150), 0)); new Point2D(RANDOM.nextDouble(70, 150), 0));
} }
public Ufo(World world, Point2D position, Point2D velocity) { public Ufo(World world, Point2D position, Point2D velocity) {
this.world = world; super(world, position);
this.position = position;
this.velocity = velocity; this.velocity = velocity;
} }
public void draw(GraphicsContext gc) { @Override
public void drawInternal(GraphicsContext gc) {
gc.drawImage(image, getPosition().getX(), getPosition().getY()); gc.drawImage(image, getPosition().getX(), getPosition().getY());
} }
public void changeDirection(){ public void changeDirection() {
velocity = velocity.multiply(-1); velocity = velocity.multiply(-1);
} }
@Override
public void simulate(double deltaT) { public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT)); position = position.add(velocity.multiply(deltaT));
position = new Point2D(position.getX()%world.getWidth(), position.getY()); position = new Point2D(position.getX() % world.getWidth(), position.getY());
if(position.getX() < -image.getWidth()) {
position = new Point2D(world.getWidth(), position.getY());
}
} }
protected Point2D getPosition() { @Override
return position;
}
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), image.getWidth(), image.getHeight()); return new Rectangle2D(position.getX(), position.getY(), image.getWidth(), image.getHeight());
}
@Override
public boolean intersect(Rectangle2D another) {
return getBoundingBox().intersects(another);
}
@Override
public void hitBy(Collisionable another) {
if(another instanceof BulletAnimated || another instanceof Bullet) {
changeDirection();
}
} }
} }
...@@ -11,20 +11,19 @@ public class World { ...@@ -11,20 +11,19 @@ public class World {
private final double height; private final double height;
private final Bullet bullet; private DrawableSimulable[] entities;
private final BulletAnimated bullet2;
private final Cannon cannon;
private Ufo[] ufos;
public World(double width, double height) { public World(double width, double height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
bullet = new Bullet(this, new Point2D(0, height), new Point2D(30, -30), new Point2D(0, 9.81)); entities = new DrawableSimulable[8];
bullet2 = new BulletAnimated(this, new Point2D(0, height), new Point2D(50, -80), new Point2D(0, 9.81)); Cannon cannon = new Cannon(this, new Point2D(0, height - 20), -45);
cannon = new Cannon(this, new Point2D(0, height-20), -45); entities[0] = cannon;
ufos = new Ufo[5]; entities[1] = new Bullet(this, new Point2D(0, height), new Point2D(30, -30), new Point2D(0, 9.81));
for (int i = 0; i < ufos.length; i++) { entities[2] = new BulletAnimated(this, cannon, new Point2D(0, height), new Point2D(50, -80),
ufos[i] = new Ufo(this); new Point2D(0, 9.81));
for (int i = 3; i < entities.length; i++) {
entities[i] = new Ufo(this);
} }
} }
...@@ -32,25 +31,26 @@ public class World { ...@@ -32,25 +31,26 @@ public class World {
gc.clearRect(0, 0, width, height); gc.clearRect(0, 0, width, height);
gc.save(); gc.save();
bullet.draw(gc); for (DrawableSimulable entity : entities) {
bullet2.draw(gc); entity.draw(gc);
cannon.draw(gc);
for (int i = 0; i < ufos.length; i++) {
ufos[i].draw(gc);
} }
gc.restore(); gc.restore();
} }
public void simulate(double deltaT) { public void simulate(double deltaT) {
bullet.simulate(deltaT); for (DrawableSimulable entity : entities) {
bullet2.simulate(deltaT); entity.simulate(deltaT);
cannon.simulate(deltaT);
for (int i = 0; i < ufos.length; i++) {
ufos[i].simulate(deltaT);
} }
for (Ufo ufo : ufos) { for (int i = 0; i < entities.length; i++) {
if(ufo.getBoundingBox().intersects(bullet2.getBoundingBox())) { if (entities[i] instanceof Collisionable c1) {
ufo.changeDirection(); for (int j = i + 1; j < entities.length; j++) {
if (entities[j] instanceof Collisionable c2) {
if (c1.intersect(c2.getBoundingBox())) {
c1.hitBy(c2);
c2.hitBy(c1);
}
}
}
} }
} }
} }
...@@ -62,5 +62,5 @@ public class World { ...@@ -62,5 +62,5 @@ public class World {
public double getHeight() { public double getHeight() {
return height; return height;
} }
} }
\ No newline at end of file
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class WorldEntity implements DrawableSimulable{
protected final World world;
protected Point2D position;
public WorldEntity(World world, Point2D position) {
this.world = world;
this.position = position;
}
@Override
public final void draw(GraphicsContext gc) {
gc.save();
drawInternal(gc);
gc.restore();
}
public abstract void drawInternal(GraphicsContext gc);
public Point2D getPosition() {
return position;
}
}
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