Class: RSokoban::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/level.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Level

Returns a new instance of Level.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/level.rb', line 23

def initialize(data)
  @walls  = {}
  @boxes  = {}
  @player = {}
  @goals  = {}
  
  data.lines.each_with_index do |line, row|
    line.split(//).each_with_index do |char, col|
      case char
      when '#'
        @walls[[row, col]] = true
      when '$'
        @boxes[[row, col]] = true
      when '.'
        @goals[[row, col]] = true
      when '@'
        @player = {row: row, col: col}
      end
    end
  end
end

Instance Attribute Details

#boxesObject

Returns the value of attribute boxes.



19
20
21
# File 'lib/level.rb', line 19

def boxes
  @boxes
end

#goalsObject

Returns the value of attribute goals.



21
22
23
# File 'lib/level.rb', line 21

def goals
  @goals
end

#playerObject

Returns the value of attribute player.



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

def player
  @player
end

#wallsObject

Returns the value of attribute walls.



18
19
20
# File 'lib/level.rb', line 18

def walls
  @walls
end

Class Method Details

.load(filename) ⇒ Object



63
64
65
# File 'lib/level.rb', line 63

def self.load(filename)
  self.parse(File.open(filename).read)
end

.parse(string) ⇒ Object



57
58
59
60
61
# File 'lib/level.rb', line 57

def self.parse(string)
  LevelData.parse(string).map do |level|
    Level.new level
  end
end

Instance Method Details

#draw(window) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/level.rb', line 45

def draw(window)
  @walls.keys.each do |k|
    window.move k[0], k[1]
    window.addstr '#'
  end

  @goals.keys.each do |k|
    window.move k[0], k[1]
    window.addstr '.'
  end
end