Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.image.BufferedImage;
  2.  
  3.  
  4. public class PuzzlePiece {
  5.     private static final int DIST = 32;
  6.    
  7.     private BufferedImage image;
  8.     private PuzzlePiece north, east, south, west;
  9.     private boolean n, e, s, w;
  10.     private int x, y;
  11.    
  12.     public PuzzlePiece(BufferedImage image) {
  13.         this.image = image;
  14.         north = null;
  15.         east = null;
  16.         south = null;
  17.         west = null;
  18.         n = false;
  19.         e = false;
  20.         s = false;
  21.         w = false;
  22.        
  23.         x = 0;
  24.         y = 0;
  25.     }
  26.    
  27.     public BufferedImage getImage() {
  28.         return image;
  29.     }
  30.    
  31.     public void setNorthLink(PuzzlePiece p) {
  32.         north = p;
  33.     }
  34.     public void setEastLink(PuzzlePiece p) {
  35.         east = p;
  36.     }
  37.     public void setSouthLink(PuzzlePiece p) {
  38.         south = p;
  39.     }
  40.     public void setWestLink(PuzzlePiece p) {
  41.         west = p;
  42.     }
  43.  
  44.     public PuzzlePiece getNorthLink() {
  45.         return north;
  46.     }
  47.     public PuzzlePiece getEastLink() {
  48.         return east;
  49.     }
  50.     public PuzzlePiece getSouthLink() {
  51.         return south;
  52.     }
  53.     public PuzzlePiece getWestLink() {
  54.         return west;
  55.     }
  56.    
  57.     public void linkToNorth() {
  58.         n = true;
  59.     }
  60.     public void linkToEast() {
  61.         e = true;
  62.     }
  63.     public void linkToSouth() {
  64.         s = true;
  65.     }
  66.     public void linkToWest() {
  67.         w = true;
  68.     }
  69.  
  70.     public boolean isLinkedToNorth() {
  71.         return n;
  72.     }
  73.     public boolean isLinkedToEast() {
  74.         return e;
  75.     }
  76.     public boolean isLinkedToSouth() {
  77.         return s;
  78.     }
  79.     public boolean isLinkedToWest() {
  80.         return w;
  81.     }
  82.    
  83.     public void setPos(int x, int y) {
  84.         if(x != this.x && y != this.y) {
  85.             this.x = x;
  86.             this.y = y;
  87.             if(n) {
  88.                 getNorthLink().setPos(x, y - Game.pieceHeight);
  89.             }
  90.             if(e) {
  91.                 getEastLink().setPos(x + Game.pieceWidth, y);
  92.             }
  93.             if(s) {
  94.                 getSouthLink().setPos(x, y + Game.pieceHeight);
  95.             }
  96.             if(w) {
  97.                 getWestLink().setPos(x - Game.pieceWidth, y);
  98.             }
  99.         }
  100.     }
  101.     public int getX() {
  102.         return x;
  103.     }
  104.     public int getY() {
  105.         return y;
  106.     }
  107.    
  108.     public void updateConnections() {
  109.         if(north != null && !n) {
  110.             int xDist = getX() - getNorthLink().getX();
  111.             int yDist = getY() - getNorthLink().getY() - Game.pieceHeight;
  112.             if(Math.abs(xDist) <= DIST && Math.abs(yDist) <= DIST) {
  113.                 linkToNorth();
  114.                 getNorthLink().linkToSouth();
  115.                 PuzzlePiece t = getNorthLink();
  116.                 setPos(t.x, t.y + Game.pieceHeight);
  117.             }
  118.         }
  119.        
  120.         if(east != null && !e) {
  121.             int xDist = getX() - getEastLink().getX() + Game.pieceWidth;
  122.             int yDist = getY() - getEastLink().getY();
  123.             if(Math.abs(xDist) <= DIST && Math.abs(yDist) <= DIST) {
  124.                 linkToEast();
  125.                 getEastLink().linkToWest();
  126.                 PuzzlePiece t = getEastLink();
  127.                 setPos(t.x - Game.pieceWidth, t.y);
  128.             }
  129.         }
  130.        
  131.         if(south != null && !s) {
  132.             int xDist = getX() - getSouthLink().getX();
  133.             int yDist = getY() - getSouthLink().getY() + Game.pieceHeight;
  134.             if(Math.abs(xDist) <= DIST && Math.abs(yDist) <= DIST) {
  135.                 linkToSouth();
  136.                 getSouthLink().linkToNorth();
  137.                 PuzzlePiece t = getSouthLink();
  138.                 setPos(t.x, t.y - Game.pieceHeight);
  139.             }
  140.         }
  141.        
  142.         if(west != null && !w) {
  143.             int xDist = getX() - getWestLink().getX() - Game.pieceWidth;
  144.             int yDist = getY() - getWestLink().getY();
  145.             if(Math.abs(xDist) <= DIST && Math.abs(yDist) <= DIST) {
  146.                 linkToWest();
  147.                 getWestLink().linkToEast();
  148.                 PuzzlePiece t = getWestLink();
  149.                 setPos(t.x + Game.pieceWidth, t.y);
  150.             }
  151.         }
  152.     }
  153. }