| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package gui.simulator;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.Point2D;
- import java.util.List;
- import javax.swing.ImageIcon;
- public class Visitor {
- Point2D positie;
- double rotation;
- double speed;
- Point2D target;
- WalkingPath currentpath;
- int currentpoint;
-
- Image image = new ImageIcon("res/visitor.png").getImage();
-
- public Visitor(Point2D positie) {
- this.positie = positie;
- this.rotation = 0;
- double number = Math.random()*2;
- if(number < 1){
- this.speed = 1;
- } else {
- this.speed = number;
- }
- this.target = new Point2D.Double(0, 0);
- }
- void update(List<Visitor> visitors, List<DrawEngine> buildings)
- {
-
- Point2D difference = new Point2D.Double(
- target.getX() - positie.getX(),
- target.getY() - positie.getY()
- );
-
- double newRotation = Math.atan2(difference.getY(), difference.getX());
- double rotDifference = rotation - newRotation;
- while(rotDifference > Math.PI)
- rotDifference -= 2 * Math.PI;
- while(rotDifference < -Math.PI)
- rotDifference += 2 * Math.PI;
-
- if(Math.abs(rotDifference) < 0.1)
- rotation = newRotation;
- else if(rotDifference < 0)
- rotation += 0.1;
- else if(rotDifference > 0)
- rotation -= 0.1;
-
- Point2D oldPositie = positie;
-
- positie = new Point2D.Double(
- positie.getX() + speed * Math.cos(rotation),
- positie.getY() + speed * Math.sin(rotation)
- );
-
- if(hasCollision(visitors) || hasCollisionObject(buildings))
- {
- positie = oldPositie;
- rotation += 0.2;
- }
- if(currentpath != null){
- if(hasCollision(currentpath.get(currentpoint))){
- if(currentpoint < currentpath.getPath().size()-1){
- currentpoint++;
- target = currentpath.get(currentpoint);
- }
- }
- }
-
- }
-
- void paint(Graphics2D g)
- {
- AffineTransform tx = new AffineTransform();
- tx.translate(positie.getX()-8, positie.getY()-11);
- tx.rotate(rotation, 4, 6);
-
- g.drawImage(image, tx ,null);
-
- }
- public boolean hasCollision(List<Visitor> visitors) {
- for(Visitor b : visitors)
- {
- if(b == this)
- continue;
- if(b.positie.distance(positie) < 11)
- return true;
- }
- return false;
- }
- public boolean hasCollisionObject(List<DrawEngine> objects){
- for(DrawEngine o:objects){
- if (o.contains(positie))
- return true;
- }
- return false;
- }
- public boolean hasCollision(Point p){
- return positie.distance(p) < 11;
- }
- public void walkRoute(WalkingPath p){
- this.currentpath = p;
- currentpoint = 0;
- target = p.get(0);
- }
- }
|