##################
#
# Perl module that demonstrates an 
# object orientation approach to programming.
#
# This object is a "room" in a Zork-like game.
#
#################

package Room;

# ----------------------------------------------------------
#    Data that belongs to the entire Room class.
#    There's only one of each of these, 
#    and every individual $room can see it.

my @rooms       = ();                           # List of all existing rooms.

# ----------------------------------------------------------
#    Methods that belong to the entire Room class.
#    Each of these should be invoked with Room->method( arguments );

# Create a new room.
# First room created is the explorer's initial position.
# Usage:  my $room = Room->new();
sub new {
  my $class = shift;
  my $room  = {                     # The room is a hash reference with
	       _description => '',  #     * a text blurb
	       _items       => [],  #     * things in this room
	       _exits       => {},  #     * possible exits
	      };      
  bless $room, $class;
  push @rooms, $room;
  
  return $room;
}

# Return number of current rooms in the whole dungeon.
# Usage: $number = Room->countRooms();
sub countRooms {
  my $class = shift;
  return scalar(@rooms);
}

# ----------------------------------------------------------
#    Methods that belong to each room
#    Each of these should be invoked with $room->method( arguments );

# Usage: $text = $room->getDescription();
sub getDescription {
  my $self = shift;
  return $self->{_description};
}

# Usage: $room->setDescription($text);
sub setDescription {
  my $self = shift;
  my ($text) = @_;
  $self->{_description} = $text;
}

# Make this room the starting point.
# Usage: $room->setInitial();

# Add an item to the list of things in the room.
# Usage: $room->addItem($item);
sub addItem {
  my $self = shift;
  my ($item) = @_;
  push @{$self->{_items}}, $item;
}

# Add an exit from a room to another room in a given direction.
# Usage: $room->addExit( North => $otherRoom );
sub addExit {
  my $self = shift;
  my ($direction, $otherRoom) = @_;
  $self->{_exits}{$direction} = $otherRoom;
}

# Return the next room in a given direction.
# If there isn't one, return a string that reports the error.
# Usage: $nextRoom = $room->getExit('North');
sub getExit {
  my $self = shift;
  my ($where) = @_;
  my $nextRoom = $self->{_exits}{$where};
  if ($nextRoom){
    return $nextRoom;
  }
  else {
    return "can't go in direction '$where'";
  }
}

# Return a text description of a room, including 
# what's in it and which ways you can leave it.
sub asText {
  my $self = shift;
  my $text = $self->{_description};
  $text .= "\n";
  my @stuff = @{$self->{_items}};
  if (@stuff){
    $text .= "On the ground you see : \n";
    foreach my $item (@stuff){
      $text .= "   " . $item . "\n";
    }
  }
  my %exits = %{$self->{_exits}};
  if (%exits){
    $text .= "The exits are : \n";
    foreach my $direction (keys %exits){
      $text .= "   " . $direction . "\n";
    }
  }
  return $text;
}

# All perl packages must end with a true value.
1;



