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

lab04-03 solution

parent 0f74c9f3
Branches
No related merge requests found
Pipeline #81 failed with stages
in 0 seconds
...@@ -5,7 +5,7 @@ import javafx.geometry.Rectangle2D; ...@@ -5,7 +5,7 @@ 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 implements DrawableSimulable, Collisionable {
private Point2D position; private Point2D position;
private Point2D start; private Point2D start;
...@@ -76,11 +76,17 @@ public class BulletAnimated { ...@@ -76,11 +76,17 @@ public class BulletAnimated {
} }
@Override
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size); return new Rectangle2D(position.getX(), position.getY(), size, size);
} }
@Override
public void hitBy(Collisionable col) {
reload();
}
public boolean overlaps(Dragon dragon) { public boolean overlaps(Dragon dragon) {
return getBoundingBox().intersects(dragon.getBoundingBox()); return getBoundingBox().intersects(dragon.getBoundingBox());
} }
......
...@@ -5,7 +5,7 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -5,7 +5,7 @@ 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 implements DrawableSimulable {
private int direction=-1; private int direction=-1;
private double angle = 0; private double angle = 0;
......
package lab;
import javafx.geometry.Rectangle2D;
public interface Collisionable {
Rectangle2D getBoundingBox();
default boolean intersects(Collisionable col) {
return getBoundingBox().intersects(col.getBoundingBox());
}
void hitBy(Collisionable col);
}
...@@ -4,7 +4,7 @@ import javafx.geometry.Point2D; ...@@ -4,7 +4,7 @@ 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 implements DrawableSimulable, Collisionable{
private Point2D position; private Point2D position;
...@@ -35,18 +35,21 @@ public class Dragon { ...@@ -35,18 +35,21 @@ public class Dragon {
position = new Point2D(newX, newY); position = new Point2D(newX, newY);
} }
@Override
public Rectangle2D getBoundingBox() { public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size); return new Rectangle2D(position.getX(), position.getY(), size, size);
} }
@Override
public void hitBy(Collisionable col) {
if (!(col instanceof Dragon)) {
hit();
}
}
public void hit() { public void hit() {
speed = speed.multiply(-1.); 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,16 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -7,19 +7,16 @@ 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 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)), entities = 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, 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)), cannon, bulletAnimatted };
} }
public Point2D getCanvasPoint(Point2D worldPoint) { public Point2D getCanvasPoint(Point2D worldPoint) {
...@@ -29,22 +26,35 @@ public class World { ...@@ -29,22 +26,35 @@ 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);
bulletAnimatted.draw(gc); for(DrawableSimulable entity: entities) {
for(Dragon dragon: dragons) { entity.draw(gc);
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 (bulletAnimatted.overlaps(dragon)) { if (entity instanceof Collisionable) {
dragon.hit(); Collisionable col = (Collisionable) entity;
bulletAnimatted.reload(); for (DrawableSimulable entity2 : entities) {
if (entity != entity2 && entity2 instanceof Collisionable) {
Collisionable col2 = (Collisionable) entity2;
if (col.intersects(col2)) {
col.hitBy(col2);
col2.hitBy(col);
}
}
}
} }
dragon.simulate(timeDelta); //ověř, že entity je Collisionable:
// pro všechny entity2 z entities:
//ověř že se nejedná of "entity" a že Collisionable
//ověř, že entity a entity2 jsou v kolizi:
//zavolej na obě hitBy
} }
} }
......
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