From a2110d3e58c407d62fe090188d23a0d68c490d18 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 7 Apr 2015 12:36:14 +0200 Subject: [PATCH] ok --- Applicatie/DrawObject.java | 8 ++- Applicatie/Images.java | 5 +- Applicatie/MenuBar.java | 1 - Applicatie/Panel.java | 8 +-- Applicatie/SaveLoad.java | 2 - Applicatie/Visitor.java | 106 ++++++++++++++++++++++--------------- Listeners/MouseMotion.java | 4 -- Objects/Path.java | 1 - Objects/Podium.java | 6 +++ 9 files changed, 81 insertions(+), 60 deletions(-) diff --git a/Applicatie/DrawObject.java b/Applicatie/DrawObject.java index 899a4ef..d00366e 100644 --- a/Applicatie/DrawObject.java +++ b/Applicatie/DrawObject.java @@ -14,7 +14,11 @@ import java.io.Serializable; import javax.swing.ImageIcon; -public class DrawObject implements Serializable{ +public abstract class DrawObject implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 1L; Point2D position; double rotation; double scale; @@ -41,6 +45,8 @@ public class DrawObject implements Serializable{ this.position = position; } + public abstract String getName(); + public void draw(Graphics2D g) { AffineTransform tx = getTransform(); diff --git a/Applicatie/Images.java b/Applicatie/Images.java index 48aec2f..355823f 100644 --- a/Applicatie/Images.java +++ b/Applicatie/Images.java @@ -10,8 +10,7 @@ public class Images{ private static Map images; - public Images() { - + public Images() { images = new HashMap(); images.put("entrance", loadImage("images/entrance.png")); @@ -20,7 +19,7 @@ public class Images{ images.put("stage", loadImage("images/stage4.png")); images.put("wall", loadImage("images/wall.png")); images.put("stage1", loadImage("images/stage3.png")); - + images.put("visitor", loadImage("images/visitor.png")); } public static Image loadImage(String path) { diff --git a/Applicatie/MenuBar.java b/Applicatie/MenuBar.java index 99bbbb4..413ac9a 100644 --- a/Applicatie/MenuBar.java +++ b/Applicatie/MenuBar.java @@ -5,7 +5,6 @@ import java.awt.event.ActionListener; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; public class MenuBar extends JMenuBar { diff --git a/Applicatie/Panel.java b/Applicatie/Panel.java index b8f3fa0..233e8df 100644 --- a/Applicatie/Panel.java +++ b/Applicatie/Panel.java @@ -105,7 +105,7 @@ public class Panel extends JPanel implements ActionListener { } public void addVisitors(){ - visitors.add(new Visitor("images/coin.png", new Point(100,300), agenda, objects)); + visitors.add(new Visitor("visitor", new Point(100,300), agenda, objects)); } public int getPanelInfoLength() { @@ -181,14 +181,14 @@ public class Panel extends JPanel implements ActionListener { @Override public void actionPerformed(ActionEvent e) { tick++; - if ( tick >= 10){ + if ( tick >= 10 && visitors.size() > 0){ tick = 0; currentTime++; - //System.out.println(currentTime); + System.out.println(currentTime); } for (Visitor v: visitors){ - v.update(objects, currentTime); + v.update(objects, currentTime, visitors); } repaint(); diff --git a/Applicatie/SaveLoad.java b/Applicatie/SaveLoad.java index 8e4d7fc..cdc6a63 100644 --- a/Applicatie/SaveLoad.java +++ b/Applicatie/SaveLoad.java @@ -11,8 +11,6 @@ import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileFilter; -import Agenda.Agenda; - public class SaveLoad { diff --git a/Applicatie/Visitor.java b/Applicatie/Visitor.java index dcd63bb..41c1835 100644 --- a/Applicatie/Visitor.java +++ b/Applicatie/Visitor.java @@ -16,79 +16,90 @@ public class Visitor { Point2D position; double rotation; - double scale; + double speed; String filename; ArrayList actions; Agenda agenda; ArrayList objects; - public Visitor(String filename, Point2D position, Agenda agenda, ArrayList objects) { + public Visitor(String filename, Point2D position, Agenda agenda, + ArrayList objects) { this.position = position; this.filename = filename; - scale = 1; - rotation = 0; + this.rotation = 0; + this.speed = 1 + Math.random()*4; actions = new ArrayList(); this.agenda = agenda; this.objects = objects; fillActions(); - for (Action a : actions){ - System.out.println(a.getStartTime()); - } } public void draw(Graphics2D g2) { - Image image = new ImageIcon(filename).getImage(); - g2.drawImage(image, getTransform(), null); + AffineTransform tx = getTransform(); + Image image = Images.getImage(filename); + g2.drawImage(image, tx, null); } - + private AffineTransform getTransform() { AffineTransform tx = new AffineTransform(); - tx.scale(scale, scale); tx.translate(position.getX(), position.getY()); - Image image = new ImageIcon(filename).getImage(); - tx.rotate(Math.toRadians(rotation), image.getWidth(null) / 2, - image.getHeight(null) / 2); + Image image = Images.getImage(filename); + tx.rotate(rotation, image.getWidth(null) / 2, image.getHeight(null) / 2); return tx; } - public void update(ArrayList objects, int currentTime) { - Point2D target = new Point(-100,-100); - for (Action a : actions){ - if (currentTime >= a.getStartTime() && currentTime < a.getStoptime()){ + public void update(ArrayList objects, int currentTime, + ArrayList visitors) { + Point2D target = new Point(-100, -100); + for (Action a : actions) { + if (currentTime >= a.getStartTime() + && currentTime < a.getStoptime()) { target = a.getPosition(); } - + } - moveToTarget(target, objects); + moveToTarget(target, objects, visitors); } - - public void moveToTarget(Point2D target, ArrayList objects){ - int x = 0; - int y = 0; + + public void moveToTarget(Point2D target, ArrayList objects, ArrayList visitors) { + + double newRot = Math.atan2(target.getY()-position.getY(), target.getX()- position.getX()); - if (target.getX() > position.getX()){ - x = 2; - } else if (target.getX() < position.getX()){ - x = - 2; + int difx = (int)(target.getX() - position.getX()); + int dify = (int)(target.getY() - position.getY()); + int distance = (int) Math.sqrt((difx*difx)+(dify*dify)); + + if (rotation > newRot && distance > 10){ + rotation -= 0.15; + } else if (rotation < newRot && distance > 10){ + rotation += 0.15; } - - if (target.getY() > position.getY()){ - y = 2; - } else if (target.getY() < position.getY()){ - y = - 2; - } - + Point2D oldPosition = position; - position = new Point2D.Double((int) (position.getX() + x), - (int) (position.getY() + y)); + + //face direction + 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)); + } + + boolean possible = true; for (DrawObject object : objects) { if (hitTest(object)) { possible = false; } } + for (Visitor object : visitors) { + if (hitTestVisitor(object) && object != this) { + possible = false; + } + } if (possible == false) { position = oldPosition; + rotation += 0.2; } } @@ -98,7 +109,6 @@ public class Visitor { while (startTime < stopTime) { int random = (int) Math.floor(Math.random() * 51); - System.out.println(random); if (random < 30) { Point2D position = null; for (Event e : agenda.getEvents()) { @@ -112,7 +122,7 @@ public class Visitor { } } int randomtime = (int) (40 + (Math.random() * (60 - 40))); - if ( position != null ){ + if (position != null) { actions.add(new Action(position, startTime, randomtime)); } startTime = startTime + randomtime; @@ -124,10 +134,10 @@ public class Visitor { position = d.getPosition(); } } - if ( position != null){ + if (position != null) { actions.add(new Action(position, startTime, 35)); startTime = startTime + 35; - } + } startTime = startTime + 35; } else if (random > 35) { @@ -137,7 +147,7 @@ public class Visitor { position = d.getPosition(); } } - if ( position != null){ + if (position != null) { actions.add(new Action(position, startTime, 35)); } startTime = startTime + 35; @@ -155,12 +165,12 @@ public class Visitor { } public int getEndX() { - Image image = new ImageIcon(filename).getImage(); + Image image = Images.getImage(filename); return (int) (position.getX() + image.getWidth(null)); } public int getEndY() { - Image image = new ImageIcon(filename).getImage(); + Image image = Images.getImage(filename); return (int) (position.getY() + image.getHeight(null)); } @@ -168,5 +178,13 @@ public class Visitor { return (getEndX() >= to2.getX() && getEndY() >= to2.getY() && to2.getEndX() >= position.getX() && to2.getEndY() >= position .getY()); + + } + + public boolean hitTestVisitor(Visitor v) { + return (getEndX() >= v.position.getX() + && getEndY() >= v.position.getY() + && v.getEndX() >= position.getX() && v.getEndY() >= position + .getY()); } } diff --git a/Listeners/MouseMotion.java b/Listeners/MouseMotion.java index dc8fd65..4e459c8 100644 --- a/Listeners/MouseMotion.java +++ b/Listeners/MouseMotion.java @@ -24,10 +24,6 @@ public class MouseMotion extends MouseMotionAdapter { panel.getDragObject().setPosition(new Point2D.Double( (panel.getDragObject().getPosition().getX() ) - ((panel.getLastClickPosition().getX() - clickPoint.getX()) ) / panel.getDragObject().getScale(), (panel.getDragObject().getPosition().getY()) - (panel.getLastClickPosition().getY() - clickPoint.getY()) / panel.getDragObject().getScale() )); - System.out.println(panel.getDragObject().getPosition().getX()); - System.out.println(panel.getLastClickPosition().getX()); - System.out.println(clickPoint.getX()); - System.out.println("----------'"); break; case "upperLeft": if(clickPoint.getX() < panel.getDragObject().getPosition().getX() && clickPoint.getY() < panel.getDragObject().getPosition().getY()) { diff --git a/Objects/Path.java b/Objects/Path.java index fef5553..16770cf 100644 --- a/Objects/Path.java +++ b/Objects/Path.java @@ -10,7 +10,6 @@ public class Path extends DrawObject { super("path", position); } - @Override public String getName() { return "Path"; } diff --git a/Objects/Podium.java b/Objects/Podium.java index 153015d..c311496 100644 --- a/Objects/Podium.java +++ b/Objects/Podium.java @@ -10,4 +10,10 @@ public class Podium extends DrawObject { super("stage", position); } + @Override + public String getName() { + + return "stage"; + } + } \ No newline at end of file