Source of Door.java


  1: 
  2: package maze; 
  3: 
  4: import java.awt.*; 
  5: import java.applet.AudioClip; 
  6: 
  7: public class Door implements MapSite { 
  8: 
  9:   public Door(Room room1, Room room2) { 
 10:     this.room1 = room1;
 11:     this.room2 = room2;
 12:   }
 13: 
 14:   public Object clone() throws CloneNotSupportedException { 
 15:     return super.clone();
 16:   }
 17: 
 18:   public boolean isOpen() { 
 19:     return open; 
 20:   }
 21: 
 22:   public void setOpen(boolean open) { 
 23:     this.open = open; 
 24:   }
 25: 
 26:   public void setRooms(Room room1, Room room2) { 
 27:     this.room1 = room1;
 28:     this.room2 = room2;
 29:   }
 30: 
 31:   public Orientation getOrientation() { 
 32:     return orientation;
 33:   }
 34: 
 35:   public void setOrientation(Orientation orientation) { 
 36:     this.orientation = orientation;
 37:   }
 38: 
 39:   public Room otherSideFrom(Room room) { 
 40:     if (room != null) { 
 41:       if (room == room1) {
 42:         return room2; 
 43:       } else if (room == room2) {
 44:         return room1; 
 45:       }
 46:     }
 47:     return null; 
 48:   }
 49: 
 50:   public void enter(Maze maze) {
 51:     if (open) { 
 52:       Room otherRoom = otherSideFrom(maze.getCurrentRoom());
 53:       if (otherRoom != null) { 
 54:         otherRoom.enter(maze); 
 55:       }
 56:     } else { 
 57:       ding.play();
 58:     }
 59:   }
 60: 
 61:   public void draw(Graphics g, int x, int y, int w, int h) {
 62:     g.setColor(Wall.WALL_COLOR); 
 63:     g.fillRect(x, y, w, h); 
 64:     if (orientation == Orientation.VERTICAL) { 
 65:       y += 2 * w; h -= 4 * w;
 66:     } else { 
 67:       x += 2 * h; w -= 4 * h; 
 68:     }
 69:     if (open) { 
 70:       g.setColor(Room.ROOM_COLOR);
 71:       g.fillRect(x, y, w, h); 
 72:     } else { 
 73:       g.setColor(Color.red);
 74:       g.fillRect(x, y, w, h); 
 75:       g.setColor(Color.black);
 76:       g.drawRect(x, y, w, h); 
 77:     }    
 78:   }
 79: 
 80:   protected Room room1;
 81:   protected Room room2;
 82:   protected boolean open; 
 83:   protected Orientation orientation; 
 84: 
 85:   protected static AudioClip ding = util.AudioUtility.getAudioClip("audio/ding.au"); 
 86: 
 87: }