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

lab04-solution: Pondělí 14:15

parent 9cfc8047
Branches
No related merge requests found
......@@ -5,7 +5,7 @@ import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated {
public class BulletAnimated implements DrawableSimulable, Collisionable{
private Point2D position;
private Point2D start;
......@@ -85,6 +85,10 @@ public class BulletAnimated {
return getBoundingBox().intersects(dragon.getBoundingBox());
}
public void hitBy(Collisionable other) {
reload();
}
public void reload() {
position = start;
speed = initialSpeed;
......
......@@ -5,7 +5,7 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
public class Cannon {
public class Cannon implements DrawableSimulable {
private int direction=-1;
private double angle = 0;
......
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
Rectangle2D getBoundingBox();
default boolean intersects(Collisionable other) {
return getBoundingBox().intersects(other.getBoundingBox());
}
void hitBy(Collisionable other);
}
......@@ -4,7 +4,7 @@ import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
public class Dragon {
public class Dragon implements DrawableSimulable, Collisionable{
private Point2D position;
......@@ -21,13 +21,13 @@ public class Dragon {
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);
}
@Override
public void simulate(double timeDelta) {
double timeDeltaS = timeDelta;
double newX = (position.getX() + speed.getX() * timeDeltaS + world.getWidth()) % world.getWidth();
......@@ -45,6 +45,14 @@ public class Dragon {
speed = speed.multiply(-1.);
}
@Override
public void hitBy(Collisionable other) {
if (!(other instanceof Dragon)) {
hit();
}
}
......
package lab;
import javafx.scene.canvas.GraphicsContext;
public interface DrawableSimulable {
void draw(GraphicsContext gc);
void simulate(double deltaT);
}
......@@ -7,17 +7,16 @@ 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 []entities;
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));
entities = new DrawableSimulable[] { cannon,
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)) };
}
......@@ -29,23 +28,37 @@ public class World {
public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
cannon.draw(gc);
bulletAnimatted.draw(gc);
for(Dragon dragon: dragons) {
dragon.draw(gc);
for(DrawableSimulable entity: entities) {
entity.draw(gc);
}
}
public void simulate(double timeDelta) {
bulletAnimatted.simulate(timeDelta);
for(DrawableSimulable entity: entities) {
entity.simulate(timeDelta);
if (entity instanceof Collisionable) {
Collisionable thisCollinsable = (Collisionable) entity;
for(DrawableSimulable entity2 : entities) {
if (entity != entity2 && entity2 instanceof Collisionable) {
Collisionable thatCollinsable = (Collisionable) entity2;
if (thisCollinsable.intersects(thatCollinsable)) {
thisCollinsable.hitBy(thatCollinsable);
thatCollinsable.hitBy(thisCollinsable);
}
}
}
}
}
/*bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
for(Dragon dragon: dragons) {
if (bulletAnimatted.overlaps(dragon)) {
dragon.hit();
bulletAnimatted.reload();
}
dragon.simulate(timeDelta);
}
}*/
}
public double getWidth() {
......
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