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

solution-2022_Wed_9-00

parent 505c8e48
No related merge requests found
......@@ -5,13 +5,11 @@ import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated {
public class BulletAnimated extends WorldEntity implements Collisionable {
private Point2D position;
private Point2D start;
private Point2D speed;
private Point2D initialSpeed;
private double size;
private double mass = 2;
private double strenghtOfCannon = 100;
private double cannonLength = 100;
......@@ -22,7 +20,6 @@ public class BulletAnimated {
private double dragCoefficient = 0.47;
private Image image;
private World world;
private Cannon cannon;
public BulletAnimated(World world, Cannon cannon) {
......@@ -30,24 +27,17 @@ public class BulletAnimated {
}
public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) {
super(world, start, new Point2D(size, size));
this.start = start;
this.position = this.start;
this.initialSpeed = speed;
this.speed = speed;
this.size = size;
this.world = world;
this.cannon = cannon;
image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"), size, size,
true, true);
}
public void draw(GraphicsContext gc) {
gc.save();
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
gc.restore();
}
@Override
public void simulate(double deltaT) {
if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle();
......@@ -65,17 +55,41 @@ public class BulletAnimated {
}
if (!hitToGround) {
position = position.add(speed.multiply(deltaT*100));
if (!accelerate && position.getY() <= size / 2) {
if (!accelerate && position.getY() <= dimension.getY() / 2) {
hitToGround = true;
position = new Point2D(position.getX(), size / 2);
position = new Point2D(position.getX(), dimension.getY() / 2);
}
} else {
reload();
}
}
@Override
public Rectangle2D getBB() {
return getBoundingBox();
}
@Override
public boolean isInCollisionWith(Collisionable col) {
if (!(col instanceof Dragon)) {
return false;
}
return Collisionable.super.isInCollisionWith(col);
}
@Override
public void hitBy(Collisionable col) {
reload();
}
@Override
protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
}
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
return new Rectangle2D(position.getX(), position.getY() - dimension.getY(), dimension.getX(), dimension.getY());
}
public boolean overlaps(Dragon dragon) {
......
......@@ -5,24 +5,16 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
public class Cannon {
public class Cannon extends WorldEntity{
private int direction=-1;
private double angle = 0;
private Point2D position;
private Point2D size;
private World world;
public Cannon(World world, Point2D position, Point2D size) {
super();
this.world = world;
this.position = position;
this.size = size;
super(world, position, size);
}
@Override
public void simulate(double timeStep) {
angle = angle + direction*80*timeStep;
if(angle <=-90 || angle >= 0) {
......@@ -30,20 +22,19 @@ public class Cannon {
}
}
public void draw(GraphicsContext gc) {
gc.save();
public double getAngle() {
return (angle * -1) / 180 * Math.PI;
}
@Override
protected void drawInternal(GraphicsContext gc) {
Point2D worldPosition = world.getCanvasPoint(position);
gc.setFill(Color.BROWN);
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()+size.getX(), worldPosition.getY()+size.getY()/2, 40, 40);
gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2)));
gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+dimension.getY(), dimension.getX()+20, dimension.getY()/2);
gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+dimension.getY()/2, 40, 40);
gc.fillOval(worldPosition.getX()+dimension.getX(), worldPosition.getY()+dimension.getY()/2, 40, 40);
gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+dimension.getY()/2)));
gc.setFill(Color.BLACK);
gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY());
gc.restore();
}
public double getAngle() {
return (angle * -1) / 180 * Math.PI;
gc.fillRect(worldPosition.getX(), worldPosition.getY(), dimension.getX(), dimension.getY());
}
}
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
Rectangle2D getBB();
default boolean isInCollisionWith(Collisionable col) {
return getBB().intersects(col.getBB());
}
void hitBy(Collisionable col);
}
......@@ -4,30 +4,17 @@ import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
public class Dragon {
private Point2D position;
public class Dragon extends WorldEntity implements Collisionable{
private Point2D speed;
private final World world;
private final double size = 45;
public Dragon(World world, Point2D position, Point2D speed) {
super();
this.world = world;
this.position = position;
super(world, position, new Point2D(45, 45));
this.speed = speed;
}
public void draw(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size);
}
@Override
public void simulate(double timeDelta) {
double timeDeltaS = timeDelta;
double newX = (position.getX() + speed.getX() * timeDeltaS + world.getWidth()) % world.getWidth();
......@@ -35,9 +22,26 @@ public class Dragon {
position = new Point2D(newX, newY);
}
@Override
public Rectangle2D getBB() {
return getBoundingBox();
}
@Override
public boolean isInCollisionWith(Collisionable col) {
if (!(col instanceof BulletAnimated)) {
return false;
}
return Collisionable.super.isInCollisionWith(col);
}
@Override
public void hitBy(Collisionable col) {
hit();
}
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
return new Rectangle2D(position.getX(), position.getY() - dimension.getY(), dimension.getX(), dimension.getY());
}
......@@ -45,6 +49,12 @@ public class Dragon {
speed = speed.multiply(-1.);
}
@Override
protected void drawInternal(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), dimension.getX(), dimension.getY());
}
......
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,20 @@ import javafx.scene.canvas.GraphicsContext;
public class World {
private double width;
private double height;
private BulletAnimated bulletAnimatted;
private Cannon cannon;
private Dragon[] dragons;
private DrawableSimulable[] objects;
public World(double width, double height) {
super();
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(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)) };
Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
BulletAnimated bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
objects = new DrawableSimulable[] {
cannon,
bulletAnimatted, 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) {
......@@ -27,22 +28,30 @@ public class World {
public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, width, height);
cannon.draw(gc);
bulletAnimatted.draw(gc);
for(Dragon dragon: dragons) {
dragon.draw(gc);
for (DrawableSimulable obj : objects) {
obj.draw(gc);
}
}
public void simulate(double timeDelta) {
bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
for(Dragon dragon: dragons) {
if (bulletAnimatted.overlaps(dragon)) {
dragon.hit();
bulletAnimatted.reload();
for (DrawableSimulable obj : objects) {
obj.simulate(timeDelta);
}
//solve collisions
for (DrawableSimulable obj1 : objects) {
if (obj1 instanceof Collisionable col1) {
for (DrawableSimulable obj2 : objects) {
if (obj1 != obj2 && obj2 instanceof Collisionable col2) {
if (col1.isInCollisionWith(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 {
protected final World world;
protected Point2D position;
protected Point2D dimension;
protected WorldEntity(World world, Point2D position, Point2D dimension) {
this.world = world;
this.position = position;
this.dimension = dimension;
}
@Override
public void draw(GraphicsContext gc) {
gc.save();
drawInternal(gc);
gc.restore();
}
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