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

Solution 2022 - Mon 10:45

parent 03979e13
No related merge requests found
Pipeline #73 failed with stages
in 0 seconds
......@@ -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 = .1;
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,17 +27,16 @@ 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);
}
@Override
public void draw(GraphicsContext gc) {
gc.save();
Point2D canvasPosition = world.getCanvasPoint(position);
......@@ -48,6 +44,7 @@ public class BulletAnimated {
gc.restore();
}
@Override
public void simulate(double deltaT) {
double timeStep = deltaT * 1000;
if (accelerate && start.distance(position) < cannonLength) {
......@@ -66,9 +63,9 @@ public class BulletAnimated {
}
if (!hitToGround) {
position = position.add(speed);
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.getX() / 2);
}
} else {
reload();
......@@ -77,8 +74,16 @@ public class BulletAnimated {
}
@Override
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
return new Rectangle2D(position.getX(), position.getY(), dimension.getX(), dimension.getY());
}
@Override
public void hitBy(CollisionAble other) {
if (other instanceof Dragon) {
reload();
}
}
public boolean overlaps(Dragon dragon) {
......
......@@ -5,24 +5,19 @@ 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,16 +25,17 @@ public class Cannon {
}
}
@Override
public void draw(GraphicsContext gc) {
gc.save();
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.fillRect(worldPosition.getX(), worldPosition.getY(), dimension.getX(), dimension.getY());
gc.restore();
}
......
package lab;
import javafx.geometry.Rectangle2D;
public interface CollisionAble {
default boolean overlaps(CollisionAble other) {
return getBoundingBox().intersects(other.getBoundingBox());
}
void hitBy(CollisionAble other);
Rectangle2D getBoundingBox();
}
......@@ -4,30 +4,23 @@ 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;
}
@Override
public void draw(GraphicsContext gc) {
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), size, size);
gc.drawImage(Constants.DRAGON_IMAGE, canvasPosition.getX(), canvasPosition.getY(), dimension.getX(), dimension.getY());
}
@Override
public void simulate(double timeDelta) {
double timeDeltaS = timeDelta;
double newX = (position.getX() + speed.getX() * timeDeltaS + world.getWidth()) % world.getWidth();
......@@ -36,8 +29,16 @@ public class Dragon {
}
@Override
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
return new Rectangle2D(position.getX(), position.getY(), dimension.getX(), dimension.getY());
}
@Override
public void hitBy(CollisionAble other) {
if (other instanceof BulletAnimated) {
hit();
}
}
......
package lab;
import javafx.scene.canvas.GraphicsContext;
public interface DrawableSimulable {
void simulate(double deltaT);
void draw(GraphicsContext gc);
}
\ No newline at end of file
......@@ -6,19 +6,19 @@ 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)),
Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
objects = new DrawableSimulable[] { 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)) };
new Dragon(this, new Point2D(50, 270), new Point2D(-50, 20)),
new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40),
cannon };
}
public Point2D getCanvasPoint(Point2D worldPoint) {
......@@ -27,22 +27,27 @@ 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);
}
for (DrawableSimulable i: objects) {
if (i instanceof CollisionAble obj1) {
for (DrawableSimulable ii: objects) {
if (i != ii && ii instanceof CollisionAble obj2) {
if (obj1.overlaps(obj2)) {
obj1.hitBy(obj2);
obj2.hitBy(obj1);
}
}
}
}
dragon.simulate(timeDelta);
}
}
......
package lab;
import javafx.geometry.Point2D;
public abstract class WorldEntity implements DrawableSimulable {
protected Point2D position;
protected final Point2D dimension;
protected final World world;
public WorldEntity(World world, Point2D position, Point2D dimension) {
this.world = world;
this.position = position;
this.dimension = dimension;
}
}
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