class Dungeon

Attributes

current_room[R]
hallways[R]
rooms[R]

Public Class Methods

from_json( json_string ) click to toggle source
# File lib/dungeon/dungeon.rb, line 52
def self.from_json( json_string )
  data = JSON.parse( json_string )
  dungeon = Dungeon.new
  dungeon.from_json(data)
  dungeon
end
new() click to toggle source
# File lib/dungeon/dungeon.rb, line 20
def initialize
  @dungeon_size = @rooms_removal_coef = @rooms = @hallways = @dungeon_generated = @current_room = @lair = nil
end

Public Instance Methods

available_directions() click to toggle source
# File lib/dungeon/dungeon.rb, line 33
def available_directions
  assert_dungeon_generated
  @hallways.directions( @current_room )
end
from_json( data ) click to toggle source
# File lib/dungeon/dungeon.rb, line 59
def from_json( data )
  raise 'You must parse the json string before calling this method' if data.is_a? String

  @dungeon_size = data['dungeon_size']
  @rooms_removal_coef = data['rooms_removal_coef']
  @dungeon_generated = data['dungeon_generated']

  @lair = Lairs.from_hash( data['lair'] )
  @rooms = Hash[ data['rooms'].map{ |dr| [ dr['id'], Room.new( dr['top'], dr['left'], @lair, dr ) ] } ]

  @hallways = HallwaysList.new
  @hallways.from_json(data['hallways'], @rooms)

  @entry = @rooms[data['entry_room_id']]
  @current_room = @rooms[data['current_room_id']]
end
set_next_room( direction ) click to toggle source
# File lib/dungeon/dungeon.rb, line 24
def set_next_room( direction )
  assert_dungeon_generated
  # puts 'Current room = ' + @current_room.id.to_s
  room_id = @hallways.get_room_id_from_direction( @current_room, direction )
  # puts 'Connected room id = ' + room_id.to_s
  # puts 'Rooms = ' + @rooms.keys.to_s
  @current_room = @rooms[ room_id ]
end
to_json() click to toggle source
# File lib/dungeon/dungeon.rb, line 38
def to_json
  assert_dungeon_generated
  {
      dungeon_size: @dungeon_size,
      rooms_removal_coef: @rooms_removal_coef,
      entry_room_id: @entry.id,
      current_room_id: @current_room.id,
      dungeon_generated: @dungeon_generated,
      rooms: @rooms.values.map{ |r| r.to_json_hash( @hallways ) },
      hallways: @hallways.to_hash,
      lair: @lair.to_hash
  }.to_json
end

Private Instance Methods

check_params( dungeon_size, party_levels, encounters_difficulty, rooms_removal_coef ) click to toggle source
# File lib/dungeon/dungeon.rb, line 78
def check_params( dungeon_size, party_levels, encounters_difficulty, rooms_removal_coef )
  raise "dungeon_size should not be null" if dungeon_size == 0
end