Visitor.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package gui.simulator;
  2. import java.awt.Graphics2D;
  3. import java.awt.Image;
  4. import java.awt.Point;
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.geom.Point2D;
  7. import java.util.List;
  8. import javax.swing.ImageIcon;
  9. public class Visitor {
  10. Point2D positie;
  11. double rotation;
  12. double speed;
  13. Point2D target;
  14. WalkingPath currentpath;
  15. int currentpoint;
  16. Image image = new ImageIcon("res/visitor.png").getImage();
  17. public Visitor(Point2D positie) {
  18. this.positie = positie;
  19. this.rotation = 0;
  20. double number = Math.random()*2;
  21. if(number < 1){
  22. this.speed = 1;
  23. } else {
  24. this.speed = number;
  25. }
  26. this.target = new Point2D.Double(0, 0);
  27. }
  28. void update(List<Visitor> visitors, List<DrawEngine> buildings)
  29. {
  30. Point2D difference = new Point2D.Double(
  31. target.getX() - positie.getX(),
  32. target.getY() - positie.getY()
  33. );
  34. double newRotation = Math.atan2(difference.getY(), difference.getX());
  35. double rotDifference = rotation - newRotation;
  36. while(rotDifference > Math.PI)
  37. rotDifference -= 2 * Math.PI;
  38. while(rotDifference < -Math.PI)
  39. rotDifference += 2 * Math.PI;
  40. if(Math.abs(rotDifference) < 0.1)
  41. rotation = newRotation;
  42. else if(rotDifference < 0)
  43. rotation += 0.1;
  44. else if(rotDifference > 0)
  45. rotation -= 0.1;
  46. Point2D oldPositie = positie;
  47. positie = new Point2D.Double(
  48. positie.getX() + speed * Math.cos(rotation),
  49. positie.getY() + speed * Math.sin(rotation)
  50. );
  51. if(hasCollision(visitors) || hasCollisionObject(buildings))
  52. {
  53. positie = oldPositie;
  54. rotation += 0.2;
  55. }
  56. if(currentpath != null){
  57. if(hasCollision(currentpath.get(currentpoint))){
  58. if(currentpoint < currentpath.getPath().size()-1){
  59. currentpoint++;
  60. target = currentpath.get(currentpoint);
  61. }
  62. }
  63. }
  64. }
  65. void paint(Graphics2D g)
  66. {
  67. AffineTransform tx = new AffineTransform();
  68. tx.translate(positie.getX()-8, positie.getY()-11);
  69. tx.rotate(rotation, 4, 6);
  70. g.drawImage(image, tx ,null);
  71. }
  72. public boolean hasCollision(List<Visitor> visitors) {
  73. for(Visitor b : visitors)
  74. {
  75. if(b == this)
  76. continue;
  77. if(b.positie.distance(positie) < 11)
  78. return true;
  79. }
  80. return false;
  81. }
  82. public boolean hasCollisionObject(List<DrawEngine> objects){
  83. for(DrawEngine o:objects){
  84. if (o.contains(positie))
  85. return true;
  86. }
  87. return false;
  88. }
  89. public boolean hasCollision(Point p){
  90. return positie.distance(p) < 11;
  91. }
  92. public void walkRoute(WalkingPath p){
  93. this.currentpath = p;
  94. currentpoint = 0;
  95. target = p.get(0);
  96. }
  97. }