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
No related merge requests found
Pipeline #81 failed with stages
in 0 seconds
......@@ -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;
......@@ -76,11 +76,17 @@ public class BulletAnimated {
}
@Override
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
}
@Override
public void hitBy(Collisionable col) {
reload();
}
public boolean overlaps(Dragon dragon) {
return getBoundingBox().intersects(dragon.getBoundingBox());
}
......
......@@ -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 col) {
return getBoundingBox().intersects(col.getBoundingBox());
}
void hitBy(Collisionable col);
}
......@@ -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;
......@@ -35,18 +35,21 @@ public class Dragon {
position = new Point2D(newX, newY);
}
@Override
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
}
@Override
public void hitBy(Collisionable col) {
if (!(col instanceof Dragon)) {
hit();
}
}
public void hit() {
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;
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));
BulletAnimated bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
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, 270), new Point2D(-50, 20)) };
new Dragon(this, new Point2D(50, 270), new Point2D(-50, 20)), cannon, bulletAnimatted };
}
public Point2D getCanvasPoint(Point2D worldPoint) {
......@@ -29,22 +26,35 @@ 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);
cannon.simulate(timeDelta);
for(Dragon dragon: dragons) {
if (bulletAnimatted.overlaps(dragon)) {
dragon.hit();
bulletAnimatted.reload();
for(DrawableSimulable entity: entities) {
entity.simulate(timeDelta);
if (entity instanceof Collisionable) {
Collisionable col = (Collisionable) entity;
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