Visitor.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package gui.simulator;
  2. import gui.simulator.facilities.SimulatorStage;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Point2D;
  6. import java.io.Serializable;
  7. import java.util.List;
  8. public class Visitor implements Serializable {
  9. private Point2D positie, target;
  10. private double rotation, speed;
  11. private WalkingPath currentpath;
  12. private int currentpoint;
  13. private boolean walkInversed;
  14. private double food, drink, pis;
  15. Images.ImageType image = Images.ImageType.Visitor;
  16. public Visitor(Point2D positie, Point2D starttarget) {
  17. this.positie = positie;
  18. this.rotation = 0;
  19. double number = Math.random()*2;
  20. if(number < 1){
  21. this.speed = 1;
  22. } else {
  23. this.speed = number;
  24. }
  25. this.target = starttarget;
  26. this.food = (int)(Math.random()*100);
  27. this.drink = (int)(Math.random()*100);
  28. this.pis = (int)(Math.random()*100);
  29. }
  30. void update(List<Visitor> visitors, List<DrawEngine> buildings, WalkingPathArrayList walkingpaths)
  31. {
  32. Point2D difference = new Point2D.Double(
  33. target.getX() - positie.getX(),
  34. target.getY() - positie.getY()
  35. );
  36. rotation %= 360;
  37. double newRotation = Math.atan2(difference.getY(), difference.getX());
  38. double rotDifference = rotation - newRotation;
  39. while(rotDifference > Math.PI)
  40. rotDifference -= 2 * Math.PI;
  41. while(rotDifference < -Math.PI)
  42. rotDifference += 2 * Math.PI;
  43. if(Math.abs(rotDifference) < 0.1)
  44. rotation = newRotation;
  45. else if(rotDifference < 0)
  46. rotation += 0.1;
  47. else if(rotDifference > 0)
  48. rotation -= 0.1;
  49. Point2D oldPositie = positie;
  50. positie = new Point2D.Double(
  51. positie.getX() + speed * Math.cos(rotation),
  52. positie.getY() + speed * Math.sin(rotation)
  53. );
  54. this.food = (int)(Math.random()*100);
  55. this.drink = (int)(Math.random()*100);
  56. this.pis = (int)(Math.random()*100);
  57. if(hasCollision(visitors) || hasCollisionObject(buildings)) // collision with a ridged building or a visitor, rotate
  58. {
  59. positie = oldPositie;
  60. rotation += 0.5*Math.random();
  61. }
  62. if(currentpath != null){
  63. DrawEngine object = collisionObject(buildings);
  64. if(object != null){
  65. if((currentpath.getObject1() == object && walkInversed) || (currentpath.getObject2() == object && !walkInversed)){ //end point reached, what to do?
  66. if(object.type == SimulatorPane.Objects.STAGE){
  67. if(((SimulatorStage) object).getPlayingact() == null){
  68. walkNewRoute(walkingpaths, object);
  69. }
  70. }else if(object.type == SimulatorPane.Objects.DRINK){
  71. drink += 50;
  72. walkNewRoute(walkingpaths, object);
  73. }else if(object.type == SimulatorPane.Objects.SNACKBAR){
  74. food += 50;
  75. walkNewRoute(walkingpaths, object);
  76. }else if(object.type == SimulatorPane.Objects.RESTROOM){
  77. pis = 100;
  78. walkNewRoute(walkingpaths, object);
  79. }else if(object.type == SimulatorPane.Objects.WAYPOINT){
  80. walkNewRoute(walkingpaths, object);
  81. }
  82. }
  83. }
  84. if(positie.distance(currentpath.get(currentpoint)) < 11){ //collision with a path-point
  85. if(walkInversed && currentpoint > 0){
  86. currentpoint--;
  87. }else if(currentpoint < currentpath.getPath().size()-1){
  88. currentpoint++;
  89. }
  90. target = currentpath.get(currentpoint);
  91. }
  92. }else{ //entrance
  93. DrawEngine object = collisionObject(buildings);
  94. if(object != null){
  95. walkNewRoute(walkingpaths,object);
  96. }
  97. }
  98. }
  99. public void paint(Graphics2D g)
  100. {
  101. AffineTransform tx = new AffineTransform();
  102. tx.translate(positie.getX()-8, positie.getY()-11);
  103. tx.rotate(rotation, 4, 6);
  104. g.drawImage(Images.getImage(image), tx ,null);
  105. }
  106. private boolean hasCollision(List<Visitor> visitors) { // check collision with other visitors
  107. for(Visitor b : visitors)
  108. {
  109. if(b != this && b.positie.distance(positie) < 11){
  110. double rotdifference = Math.toDegrees(Math.abs(rotation-b.rotation));
  111. if(!(rotdifference > 160 && rotdifference < 210)){
  112. return true;
  113. }
  114. }
  115. }
  116. return false;
  117. }
  118. private boolean hasCollisionObject(List<DrawEngine> objects){ // check collision with rigid objects
  119. for(DrawEngine o:objects){
  120. if(o.type != SimulatorPane.Objects.WAYPOINT && o.type != SimulatorPane.Objects.EXIT && o.type != SimulatorPane.Objects.ENTRANCE )
  121. if (o.contains(positie))
  122. return true;
  123. }
  124. return false;
  125. }
  126. private DrawEngine collisionObject(List<DrawEngine> objects){
  127. for(DrawEngine o:objects){
  128. if (o.containsWalkArea(positie) || o.contains(positie) )
  129. return o;
  130. }
  131. return null;
  132. }
  133. private void walkRoute(WalkingPath p, DrawEngine object){
  134. if(p.getObject1() == object){
  135. currentpoint = 1;
  136. walkInversed = false;
  137. }else{
  138. currentpoint = p.getPath().size()-2;
  139. walkInversed= true;
  140. }
  141. target = p.get(currentpoint);
  142. currentpath = p;
  143. }
  144. private void walkNewRoute(WalkingPathArrayList walkingpaths, DrawEngine object){
  145. WalkingPathArrayList connectedWalkingPaths = walkingpaths.getWalkingPadObject(object);
  146. connectedWalkingPaths.removeType(object, SimulatorPane.Objects.ENTRANCE);
  147. if(food > 25)
  148. connectedWalkingPaths.removeType(object, SimulatorPane.Objects.SNACKBAR);
  149. if(drink > 25)
  150. connectedWalkingPaths.removeType(object, SimulatorPane.Objects.DRINK);
  151. if(pis > 25)
  152. connectedWalkingPaths.removeType(object, SimulatorPane.Objects.RESTROOM);
  153. if(!connectedWalkingPaths.isEmpty()){
  154. walkRoute(connectedWalkingPaths.get((int)(Math.random()*connectedWalkingPaths.size())), object);
  155. }
  156. }
  157. public double getFood() {
  158. return food;
  159. }
  160. public void setFood(double food) {
  161. if(food >= 0 && food <= 100)
  162. this.food = food;
  163. }
  164. public double getDrink() {
  165. return drink;
  166. }
  167. public void setDrink(double drink) {
  168. if(drink >= 0 && drink <= 100)
  169. this.drink = drink;
  170. }
  171. public double getPis() {
  172. return pis;
  173. }
  174. public void setPis(double pis) {
  175. if(pis >= 0 && pis <= 100)
  176. this.pis = pis;
  177. }
  178. }