From b8a7d9c02dc16b5e4eb50fd1ef3db46cddbf7e54 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 8 Apr 2015 17:24:29 +0200 Subject: [PATCH] change --- Agenda/Event.java | 4 +- Applicatie/Visitor.java | 267 ++++++++++++++++------------------------ 2 files changed, 111 insertions(+), 160 deletions(-) diff --git a/Agenda/Event.java b/Agenda/Event.java index c873c43..90acfb5 100644 --- a/Agenda/Event.java +++ b/Agenda/Event.java @@ -34,8 +34,8 @@ public class Event implements Serializable { endHour, endMinute); this.startHour = startHour; this.startMinute = startMinute; - this.stopHour = stopHour; - this.stopMinute = stopMinute; + this.stopHour = endHour; + this.stopMinute = endMinute; this.stage = stage; this.artist = artist; this.description = description; diff --git a/Applicatie/Visitor.java b/Applicatie/Visitor.java index f71d436..34f4624 100644 --- a/Applicatie/Visitor.java +++ b/Applicatie/Visitor.java @@ -19,8 +19,7 @@ import Objects.Entrance; import Objects.Path; import Objects.Stage; -public class Visitor -{ +public class Visitor { Point2D position; double rotation; @@ -35,8 +34,9 @@ public class Visitor int target; int finalTarget; - public Visitor(String filename, Point2D position, Agenda agenda, ArrayList objects, ArrayList waypoints, GregorianCalendar date) - { + public Visitor(String filename, Point2D position, Agenda agenda, + ArrayList objects, ArrayList waypoints, + GregorianCalendar date) { this.position = position; this.filename = filename; this.rotation = 0; @@ -53,15 +53,13 @@ public class Visitor finalTarget = 4; } - public void draw(Graphics2D g2) - { + public void draw(Graphics2D g2) { AffineTransform tx = getTransform(); Image image = Images.getImage(filename); g2.drawImage(image, tx, null); } - private AffineTransform getTransform() - { + private AffineTransform getTransform() { Image image = Images.getImage(filename); AffineTransform tx = new AffineTransform(); tx.scale(scale, scale); @@ -70,26 +68,22 @@ public class Visitor return tx; } - public DrawObject getEntrance() - { - for (DrawObject o : objects) - { - if (o instanceof Entrance) - { + public DrawObject getEntrance() { + for (DrawObject o : objects) { + if (o instanceof Entrance) { return o; } } return null; } - public void update(ArrayList objects, int currentTime, ArrayList visitors, ArrayList paths, Panel panel) - { + public void update(ArrayList objects, int currentTime, + ArrayList visitors, ArrayList paths, Panel panel) { DrawObject target = getEntrance(); - for (Action a : actions) - { - if (currentTime >= a.getStartTime() && currentTime < a.getStoptime()) - { - target = a.getTargetObject(); + for (Action a : actions) { + if (currentTime >= a.getStartTime() + && currentTime < a.getStoptime()) { + finalTarget = a.getWaypoint().getSelf(); } } @@ -98,22 +92,19 @@ public class Visitor move(target, panel); } - public void move(DrawObject tar, Panel panel) - { + public void move(DrawObject tar, Panel panel) { Point2D targetPoint = panel.getWaypoint(target).getPosition(); - double newRot = Math.atan2(targetPoint.getY() - position.getY(), targetPoint.getX() - position.getX()); + double newRot = Math.atan2(targetPoint.getY() - position.getY(), + targetPoint.getX() - position.getX()); int difx = (int) (targetPoint.getX() - position.getX()); int dify = (int) (targetPoint.getY() - position.getY()); int distance = (int) Math.sqrt((difx * difx) + (dify * dify)); - if (rotation > newRot && distance > 10) - { + if (rotation > newRot && distance > 10) { rotation -= 0.15; - } - else if (rotation < newRot && distance > 10) - { + } else if (rotation < newRot && distance > 10) { rotation += 0.15; } @@ -123,30 +114,26 @@ public class Visitor float directionX = (float) Math.cos(rotation); float directionY = (float) Math.sin(rotation); - if (distance > 10) - { - position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed)); + if (distance > 10) { + position = new Point2D.Double( + (position.getX() + directionX * speed), + (position.getY() + directionY * speed)); } boolean possible = true; - for (DrawObject object : objects) - { - if (object instanceof Entrance || object instanceof Waypoint) - { + for (DrawObject object : objects) { + if (object instanceof Entrance || object instanceof Waypoint) { continue; } - if (hasCollisionDO(object)) - { + if (hasCollisionDO(object)) { possible = false; } } - if (possible == false) - { + if (possible == false) { position = oldPosition; rotation += 0.2; } - if (checkIfOnWaypoint(panel)) - { + if (checkIfOnWaypoint(panel)) { // for(int i : panel.getWaypoint(target).getOptions()) // { // if(i == finalTarget) @@ -154,18 +141,15 @@ public class Visitor // target = finalTarget; // } // } -// ArrayList options = new ArrayList(); + // ArrayList options = new ArrayList(); HashMap options = new HashMap(); - for (Waypoint w : panel.getWaypoints()) - { + for (Waypoint w : panel.getWaypoints()) { options.put(w.getSelf(), w.getOptions()); } - for(Map.Entry e : options.entrySet()) - { + for (Map.Entry e : options.entrySet()) { System.out.println(e.getKey()); System.out.println("====================="); - for(int i : e.getValue()) - { + for (int i : e.getValue()) { System.out.println(i); } System.out.println("----------"); @@ -174,47 +158,41 @@ public class Visitor } - public boolean checkIfOnWaypoint(Panel panel) - { - if (panel.getWaypoint(target).contains(position)) - { + public boolean checkIfOnWaypoint(Panel panel) { + if (panel.getWaypoint(target).contains(position)) { return true; } return false; } - public void moveToTarget(DrawObject target, ArrayList objects, ArrayList visitors, ArrayList paths) - { + public void moveToTarget(DrawObject target, ArrayList objects, + ArrayList visitors, ArrayList paths) { Point2D tar = target.getPosition(); boolean hasPathBelow = false; - for (Path p : paths) - { - if (p.containsPoint(position)) - { + for (Path p : paths) { + if (p.containsPoint(position)) { hasPathBelow = true; break; } Shape containsShape = p.containsPointShape(position); - if (containsShape != null) - { - tar = new Point2D.Double(containsShape.getBounds().getCenterX(), containsShape.getBounds().getCenterY()); + if (containsShape != null) { + tar = new Point2D.Double( + containsShape.getBounds().getCenterX(), containsShape + .getBounds().getCenterY()); } } - if (hasPathBelow) - { - double newRot = Math.atan2(tar.getY() - position.getY(), tar.getX() - position.getX()); + if (hasPathBelow) { + double newRot = Math.atan2(tar.getY() - position.getY(), tar.getX() + - position.getX()); int difx = (int) (tar.getX() - position.getX()); int dify = (int) (tar.getY() - position.getY()); int distance = (int) Math.sqrt((difx * difx) + (dify * dify)); - if (rotation > newRot && distance > 10) - { + if (rotation > newRot && distance > 10) { rotation -= 0.15; - } - else if (rotation < newRot && distance > 10) - { + } else if (rotation < newRot && distance > 10) { rotation += 0.15; } @@ -224,39 +202,30 @@ public class Visitor float directionX = (float) Math.cos(rotation); float directionY = (float) Math.sin(rotation); - if (distance > 10) - { - position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed)); + if (distance > 10) { + position = new Point2D.Double((position.getX() + directionX + * speed), (position.getY() + directionY * speed)); } boolean possible = true; - for (DrawObject object : objects) - { - if (hasCollisionDO(object)) - { + for (DrawObject object : objects) { + if (hasCollisionDO(object)) { possible = false; } } - for (Visitor object : visitors) - { - if (hasCollision(object) && object != this) - { + for (Visitor object : visitors) { + if (hasCollision(object) && object != this) { possible = false; } } - if (possible == false) - { + if (possible == false) { position = oldPosition; rotation += 0.2; } - } - else - { + } else { HashMap values = new HashMap(); - for (Path p : paths) - { - for (Shape s : p.getPath()) - { + for (Path p : paths) { + for (Shape s : p.getPath()) { Rectangle b = s.getBounds(); double maxx = b.getMaxX(); double minx = b.getMinX(); @@ -270,16 +239,14 @@ public class Visitor points[3] = new Point2D.Double(maxx, maxy); Double lastdistance = null; - for (Point2D.Double point : points) - { - double distance = Point2D.distance(point.getX(), point.getY(), position.getX(), position.getY()); - if (lastdistance == null) - { + for (Point2D.Double point : points) { + double distance = Point2D.distance(point.getX(), + point.getY(), position.getX(), position.getY()); + if (lastdistance == null) { lastdistance = distance; continue; } - if (distance < lastdistance) - { + if (distance < lastdistance) { lastdistance = distance; } } @@ -289,35 +256,31 @@ public class Visitor } Double lastdistance = null; Shape lastShape = null; - for (Map.Entry e : values.entrySet()) - { + for (Map.Entry e : values.entrySet()) { double distance = e.getKey(); - if (lastdistance == null) - { + if (lastdistance == null) { lastdistance = distance; continue; } - if (distance < lastdistance) - { + if (distance < lastdistance) { lastdistance = distance; } } lastShape = values.get(lastdistance); - Point2D.Double target2 = new Point2D.Double(lastShape.getBounds().getCenterX(), lastShape.getBounds().getCenterY()); + Point2D.Double target2 = new Point2D.Double(lastShape.getBounds() + .getCenterX(), lastShape.getBounds().getCenterY()); - double newRot = Math.atan2(target2.getY() - position.getY(), target2.getX() - position.getX()); + double newRot = Math.atan2(target2.getY() - position.getY(), + target2.getX() - position.getX()); int difx = (int) (target2.getX() - position.getX()); int dify = (int) (target2.getY() - position.getY()); int distance = (int) Math.sqrt((difx * difx) + (dify * dify)); - if (rotation > newRot && distance > 10) - { + if (rotation > newRot && distance > 10) { rotation -= 0.15; - } - else if (rotation < newRot && distance > 10) - { + } else if (rotation < newRot && distance > 10) { rotation += 0.15; } @@ -327,41 +290,35 @@ public class Visitor float directionX = (float) Math.cos(rotation); float directionY = (float) Math.sin(rotation); - if (distance > 10) - { - position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed)); + if (distance > 10) { + position = new Point2D.Double((position.getX() + directionX + * speed), (position.getY() + directionY * speed)); } boolean possible = true; - for (DrawObject object : objects) - { - if (hasCollisionDO(object)) - { + for (DrawObject object : objects) { + if (hasCollisionDO(object)) { possible = false; } } - for (Visitor object : visitors) - { - if (hasCollision(object) && object != this) - { + for (Visitor object : visitors) { + if (hasCollision(object) && object != this) { possible = false; } } - if (possible == false) - { + if (possible == false) { position = oldPosition; rotation += 0.2; } } } - @SuppressWarnings("static-access") public void fillActions() { int startTime = 540; int stopTime = 1260; while (startTime < stopTime) { - int random = (int) Math.floor(Math.random() * 21); + int random = (int) Math.floor(Math.random() * 51); if (random < 40) { Point2D position = null; DrawObject targetStage = null; @@ -372,25 +329,16 @@ public class Visitor } } - int totalPopularity = 0; - for (Event ev : posEvents){ - totalPopularity += ev.getExpectedPopularity(); - } - - int r = (int) Math.floor(Math.random() * totalPopularity); - int currentPop = 0; - for (Event evs : posEvents){ - int before = currentPop; - currentPop += evs.getExpectedPopularity(); - if (currentPop >= r && before < r){ - for (DrawObject d : objects) { - if (d.getFileName().equals("stage")) { - Stage s = (Stage) d; - if (s.getStage().getName() - .equals(evs.getStage().getName())) { - position = s.getPosition(); - targetStage = d; - } + if (posEvents.size() != 0) { + int r = (int) Math.floor(Math.random() * posEvents.size()); + Event evs = posEvents.get(r); + for (DrawObject d : objects) { + if (d.getFileName().equals("stage")) { + Stage s = (Stage) d; + if (s.getStage().getName() + .equals(evs.getStage().getName())) { + position = s.getPosition(); + targetStage = d; } } } @@ -399,7 +347,8 @@ public class Visitor int randomtime = (int) (40 + (Math.random() * (60 - 40))); if (position != null) { Waypoint w = getClosedWaypoint(position); - actions.add(new Action(position, startTime, randomtime, targetStage, w)); + actions.add(new Action(position, startTime, randomtime, + targetStage, w)); } startTime = startTime + randomtime; @@ -412,17 +361,18 @@ public class Visitor foodPlaces.add(d); } } - - if (foodPlaces.size() != 0){ + + if (foodPlaces.size() != 0) { int r = (int) Math.floor(Math.random() * foodPlaces.size()); position = foodPlaces.get(r).getPosition(); targetStage = foodPlaces.get(r); Waypoint w = getClosedWaypoint(position); - actions.add(new Action(position, startTime, 35, targetStage, w)); + actions.add(new Action(position, startTime, 35, + targetStage, w)); startTime = startTime + 20; } startTime = startTime + 35; - + } else if (random > 45) { Point2D position = null; DrawObject targetStage = null; @@ -432,12 +382,13 @@ public class Visitor toilets.add(d); } } - if (toilets.size() != 0){ + if (toilets.size() != 0) { int r = (int) Math.floor(Math.random() * toilets.size()); position = toilets.get(r).getPosition(); targetStage = toilets.get(r); Waypoint w = getClosedWaypoint(position); - actions.add(new Action(position, startTime, 35, targetStage, w)); + actions.add(new Action(position, startTime, 35, + targetStage, w)); startTime = startTime + 20; } startTime = startTime + 35; @@ -445,19 +396,19 @@ public class Visitor } } } - - public Waypoint getClosedWaypoint(Point2D point){ + + public Waypoint getClosedWaypoint(Point2D point) { Waypoint waypoint = null; - int distance = 10000; - for(Waypoint w : waypoints){ - if (w.getPosition().distance(point) < distance){ + int distance = 10000; + for (Waypoint w : waypoints) { + if (w.getPosition().distance(point) < distance) { waypoint = w; distance = (int) w.getPosition().distance(point); } - } + } return waypoint; } - + public boolean hasCollisionDO(DrawObject object) { Image image = Images.getImage(filename); Image image2 = Images.getImage(object.getFileName());