package class8_1_SimplifiedTourists_BinaryIO_inClass_start; import javafx.geometry.Point2D; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Person extends MobileEntity { public static final int PERSON_MAX_SPEED = 2; private Goal goal; public Person(Goal goal, double x, double y) { super(x,y); setName("You"); stepSize = new Point2D(0,0); this.goal = goal; } @Override protected void step() { Point2D direction = goal.getTarget().subtract(getLocation()); if(Math.hypot(direction.getX(), direction.getY()) < PERSON_MAX_SPEED) { // last step to goal stepSize = direction; } else { // approaching goal stepSize = direction.multiply(PERSON_MAX_SPEED / Math.hypot(direction.getX(), direction.getY())); } super.step(); } public void write(String filename) { try (DataOutputStream stream = new DataOutputStream(new FileOutputStream(filename))) { stream.writeInt((int)(getLocation().getX())); stream.writeInt((int)(getLocation().getY())); // try (PrintWriter writer = new PrintWriter(stream)) { // write text! // writer.println(getLocation().getX() + " " + // getLocation().getY()); // int x = 5 / 0; // } } catch (FileNotFoundException e) { // TODO: System.out.println("fILE NOT FOUND: "+filename); } catch (IOException e) { // TODO: System.out.println("IO Exception! "+e.getMessage()+" "+e.toString()); } } /** * The circular shape on the map that the person approaches. */ public static class Goal { public static final int RADIUS = 100; private final Circle personTarget; public Goal() { personTarget = new Circle(RADIUS); personTarget.setStroke(Color.GREEN); personTarget.setFill(Color.GREEN.deriveColor(1, 1, 1, 0.2)); personTarget.relocate(0, 0); } public Point2D getTarget() { return new Point2D(personTarget.getCenterX(),personTarget.getCenterY()).add(RADIUS, RADIUS); } } }