Visitor.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.io.Serializable;
  8. import java.util.List;
  9. import javax.swing.ImageIcon;
  10. public class Visitor implements Serializable {
  11. private Point2D positie, target;
  12. private double rotation, speed;
  13. private WalkingPath currentpath;
  14. private int currentpoint;
  15. private boolean walkInversed;
  16. Images.ImageType image = Images.ImageType.Visitor;
  17. public Visitor(Point2D positie, Point2D starttarget) {
  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 = starttarget;
  27. }
  28. void update(List<Visitor> visitors, List<DrawEngine> buildings, WalkingPathArrayList walkingpaths)
  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)) // collision with a riged building or a visitor, rotate
  52. {
  53. positie = oldPositie;
  54. rotation += 0.2;
  55. }
  56. if(currentpath != null){
  57. if(hasCollision(currentpath.get(currentpoint))){
  58. DrawEngine object = collisionObject(buildings);
  59. if(object != null){
  60. if(currentpath.getObject1() == object || currentpath.getObject2() == object){
  61. List<WalkingPath> connectedWalkingPaths = walkingpaths.getWalkingPadObject(object);
  62. if(connectedWalkingPaths.isEmpty()){
  63. }else{
  64. walkRoute(connectedWalkingPaths.get((int)(Math.random()*connectedWalkingPaths.size())), object);
  65. }
  66. }
  67. }
  68. if(walkInversed && currentpoint > 0){
  69. currentpoint--;
  70. }else if(currentpoint < currentpath.getPath().size()-1){
  71. currentpoint++;
  72. }
  73. target = currentpath.get(currentpoint);
  74. }
  75. }else{ //entrance
  76. DrawEngine object = collisionObject(buildings);
  77. if(object != null){
  78. List<WalkingPath> connectedWalkingPaths = walkingpaths.getWalkingPadObject(object);
  79. if(connectedWalkingPaths.isEmpty()){ // no paths connected to the entrance, what a shame!
  80. }else{
  81. walkRoute(connectedWalkingPaths.get((int)(Math.random()*connectedWalkingPaths.size())),object);
  82. }
  83. }
  84. }
  85. }
  86. void paint(Graphics2D g)
  87. {
  88. AffineTransform tx = new AffineTransform();
  89. tx.translate(positie.getX()-8, positie.getY()-11);
  90. tx.rotate(rotation, 4, 6);
  91. g.drawImage(Images.getImage(image), tx ,null);
  92. }
  93. public boolean hasCollision(List<Visitor> visitors) {
  94. for(Visitor b : visitors)
  95. {
  96. if(b == this)
  97. continue;
  98. if(b.positie.distance(positie) < 11){
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. public boolean hasCollisionObject(List<DrawEngine> objects){
  105. for(DrawEngine o:objects){
  106. if(o.type != SimulatorPane.Objects.WAYPOINT && o.type != SimulatorPane.Objects.EXIT && o.type != SimulatorPane.Objects.ENTRANCE )
  107. if (o.contains(positie))
  108. return true;
  109. }
  110. return false;
  111. }
  112. public boolean hasCollision(Point p){
  113. return positie.distance(p) < 11;
  114. }
  115. public DrawEngine collisionObject(List<DrawEngine> objects){
  116. for(DrawEngine o:objects){
  117. if (o.contains(positie))
  118. return o;
  119. }
  120. return null;
  121. }
  122. public void walkRoute(WalkingPath p, DrawEngine object){
  123. if(p.getObject1() == object){
  124. currentpoint = 0;
  125. walkInversed = false;
  126. }else{
  127. currentpoint = p.getPath().size()-1;
  128. walkInversed= true;
  129. }
  130. target = p.get(currentpoint);
  131. currentpath = p;
  132. }
  133. }