Advertisement
Guest User

person.java

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package buildingpb;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7.  * @author shsmchlr
  8.  * This is for people moving in building
  9.  */
  10. public class Person {
  11.     private Point xy;                   // person's position
  12.     private ArrayList<Point> path;      // path it follows .. a series of xy points moves between
  13.     private boolean stopped;            // is it moving
  14.    
  15.     /**
  16.      * create person at the given xy position
  17.      * @param xys   position
  18.      */
  19.     Person(Point xys) {
  20.         xy = xys;
  21.         path = new ArrayList<Point>();          // create empty path
  22.         stopped = true;                         // by default not moving
  23.     }
  24.  
  25.     /**
  26.      * get the person's position
  27.      * @return  the position
  28.      */
  29.     public Point getXY() {
  30.         return xy;
  31.     }
  32.     /**
  33.      * get x coordinate of person
  34.      * @return x
  35.      */
  36.     public int getX() {
  37.         return (int) xy.getX();
  38.     }
  39.     /**
  40.      * get y coordinate of person
  41.      * @return y
  42.      */
  43.     public int getY() {
  44.         return (int) xy.getY();
  45.     }
  46.     /**
  47.      * set the person's position
  48.      * @param pxy   new position
  49.      */
  50.     public void setXY(Point pxy) {
  51.         xy = pxy;
  52.     }
  53.     /**
  54.      * set person as being stopped or not
  55.      * @param isStopped
  56.      */
  57.     public void setStopped(boolean isStopped) {
  58.         stopped = isStopped;
  59.     }
  60.     /**
  61.      * Is person stopped
  62.      * @return if so
  63.      */
  64.     public boolean getStopped() {
  65.         return stopped;
  66.     }
  67.     /**
  68.      * show person in the given building interface
  69.      * @param bi
  70.      */
  71.     public void showPerson(BuildingGUI bi) {
  72.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'r');
  73.     }
  74.    
  75.     public void showPerson2(BuildingGUI bi) {
  76.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'b');
  77.     }
  78.    
  79.     public void showPerson3(BuildingGUI bi) {
  80.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'b');
  81.     }
  82.    
  83.     public void showPerson4(BuildingGUI bi) {
  84.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'b');
  85.     }
  86.    
  87.     public void showPerson5(BuildingGUI bi) {
  88.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'b');
  89.     }
  90.    
  91.     public void showPerson6(BuildingGUI bi) {
  92.         bi.showItem((int) xy.getX(), (int) xy.getY(), 4, 'b');
  93.     }
  94.    
  95.    
  96.    
  97.     /**
  98.      * return info about person as string
  99.      * @return  the string
  100.      */
  101.     public String toString() {
  102.         return "Person at " + (int) xy.getX() + ", " + (int) xy.getY();
  103.     }
  104.     /**
  105.      * clear the path the person has to follow
  106.      */
  107.     public void clearPath() {
  108.         path.clear();
  109.     }
  110.     /**
  111.      * add new xy to path
  112.      * @param xyp   new position
  113.      */
  114.     public void setPath (Point xyp) {
  115.         path.add(xyp);
  116.     }
  117.     /**
  118.      * is person at the specified position?
  119.      * @param pathXY
  120.      * @return
  121.      */
  122.     private boolean equalXY(Point pathXY) {
  123.         return ( (int)pathXY.getX() == (int) xy.getX() ) && ( (int)pathXY.getY() == (int) xy.getY() );
  124.     }
  125.     /**
  126.      * move one step towards the given position
  127.      * @param pathXY
  128.      */
  129.     private void moveTowards (Point pathXY) {
  130.         int dx = 0;         // amount by which it will move in x .. and y, set to -1, 0 or 1
  131.         int dy = 0;
  132.         if (xy.getX() < pathXY.getX()) dx = 1; else if (xy.getX() > pathXY.getX()) dx = -1;
  133.         if (xy.getY() < pathXY.getY()) dy = 1; else if (xy.getY() > pathXY.getY()) dy = -1;
  134.        
  135.         xy.translate(dx, dy);   // now move
  136.     }
  137.     /**
  138.      * attempt to move person unless it is stopped
  139.      */
  140.     public void update() {
  141.         if (stopped) {}                         // do nowt
  142.         else if (equalXY(path.get(0)) ){        // if at next point on path
  143.             stopped = true;                     // as one point in this version, say stopped
  144.         }  
  145.         else moveTowards(path.get(0));          // move closer to next destination
  146.     }
  147.  
  148.  
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement