Source of Room.java


  1: 
  2: package maze; 
  3: 
  4: import java.awt.*; 
  5: import java.applet.AudioClip; 
  6: 
  7: public class Room implements MapSite { 
  8: 
  9:   public static final Color ROOM_COLOR = new Color(152, 251, 152);
 10:   public static final Color PLAYER_COLOR = Color.red;
 11: 
 12:   public Room(int roomNumber) { 
 13:     this.roomNumber = roomNumber; 
 14:   }
 15: 
 16:   public Object clone() throws CloneNotSupportedException { 
 17:     Room room = (Room) super.clone();
 18:     room.sides = new MapSite[4]; 
 19:     for (int i = 0; i < 4; i++) {
 20:       if (sides[i] != null) { 
 21:         room.sides[i] = (MapSite) sides[i].clone(); 
 22:       }
 23:     }
 24:     return room; 
 25:   }
 26:   
 27:   public MapSite getSide(Direction dir) { 
 28:     if (dir != null) { 
 29:       return sides[dir.getOrdinal()]; 
 30:     }
 31:     return null; 
 32:   }
 33: 
 34:   public void setSide(Direction dir, MapSite site) { 
 35:     if (dir != null) { 
 36:       sides[dir.getOrdinal()] = site; 
 37:       if (site instanceof Door) { 
 38:         Door door = (Door) site;
 39:         if (dir == Direction.NORTH ||
 40:             dir == Direction.SOUTH) {
 41:           door.setOrientation(Orientation.HORIZONTAL); 
 42:         } else { 
 43:           door.setOrientation(Orientation.VERTICAL); 
 44:         }
 45:       }
 46:     }
 47:   }
 48:   
 49:   public void setRoomNumber(int roomNumber) { 
 50:     this.roomNumber = roomNumber; 
 51:   }
 52: 
 53:   public int getRoomNumber() { 
 54:     return roomNumber; 
 55:   }
 56: 
 57:   public Point getLocation() { 
 58:     return location; 
 59:   }
 60: 
 61:   public void setLocation(Point location) { 
 62:     this.location = location;
 63:   }
 64: 
 65:   public void enter(Maze maze) {
 66:     maze.setCurrentRoom(this);
 67:     gong.play();
 68:   }
 69: 
 70:   public boolean isInRoom() { 
 71:     return inroom; 
 72:   }
 73: 
 74:   public void setInRoom(boolean inroom) { 
 75:     this.inroom = inroom;
 76:   }
 77: 
 78:   public void draw(Graphics g, int x, int y, int w, int h) {
 79:     g.setColor(ROOM_COLOR); 
 80:     g.fillRect(x, y, w, h); 
 81:     if (inroom) { 
 82:       g.setColor(PLAYER_COLOR);
 83:       g.fillOval(x + w / 2 - 5, y + h / 2 - 5, 10, 10); 
 84:     }
 85:   }
 86: 
 87:   protected int roomNumber = 0; 
 88:   protected boolean inroom = false; 
 89:   protected MapSite[] sides = new MapSite[4]; 
 90:   protected Point location = null;   
 91: 
 92:   protected static AudioClip gong = util.AudioUtility.getAudioClip("audio/gong.au"); 
 93:   //protected static AudioClip spacemusic = util.AudioUtility.getAudioClip("audio/spacemusic.au"); 
 94: }