Class: Dungeon

Inherits:
Object
  • Object
show all
Includes:
DungeonDraw, DungeonGenerator
Defined in:
lib/dungeon/dungeon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DungeonDraw

#draw, #draw_current_room, #print_dungeon

Methods included from DungeonGenerator

#generate

Constructor Details

#initializeDungeon

Returns a new instance of Dungeon.



20
21
22
# File 'lib/dungeon/dungeon.rb', line 20

def initialize
  @dungeon_size = @rooms_removal_coef = @rooms = @hallways = @dungeon_generated = @current_room = @lair = nil
end

Instance Attribute Details

#current_roomObject (readonly)

Returns the value of attribute current_room.



15
16
17
# File 'lib/dungeon/dungeon.rb', line 15

def current_room
  @current_room
end

#hallwaysObject (readonly)

Returns the value of attribute hallways.



15
16
17
# File 'lib/dungeon/dungeon.rb', line 15

def hallways
  @hallways
end

#roomsObject (readonly)

Returns the value of attribute rooms.



15
16
17
# File 'lib/dungeon/dungeon.rb', line 15

def rooms
  @rooms
end

Class Method Details

.from_json(json_string) ⇒ Object



52
53
54
55
56
57
# 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

Instance Method Details

#available_directionsObject



33
34
35
36
# File 'lib/dungeon/dungeon.rb', line 33

def available_directions
  assert_dungeon_generated
  @hallways.directions( @current_room )
end

#from_json(data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



24
25
26
27
28
29
30
31
# 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_jsonObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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