Class: GLW::Map

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

Constant Summary collapse

MAP =
"############\n#..........#          ##########\n#..........############....#...#\n#..............................#\n#.......g..############....#...#\n#..........#          #....#...#\n############          ##########\n"

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/glw/map.rb', line 13

def initialize
  @player_x = 3
  @player_y = 3

  @map = Hash.new(" ")
  MAP.lines.each_with_index do |line, y|
    line.chars.each_with_index do |c, x|
      @map[[x, y]] = c
    end
  end

  @offset_x = 1
  @offset_y = 1
end

Instance Method Details

#render(term, width:, height:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/glw/map.rb', line 41

def render(term, width:, height:)
  (0..width - 1).each do |x|
    (0..height - 1).each do |y|

      if [@player_x + @offset_x, @player_y + @offset_y] == [x, y]
        term.set(
          x: x,
          y: y,
          c: "@",
          fg: 220
        )
      else
        term.set(
          x: x,
          y: y,
          c: @map[[x - @offset_x, y - @offset_y]]
        )
      end
    end
  end

  term.refresh
end

#send_event(e) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/glw/map.rb', line 28

def send_event(e)
  case e
  when :left
    @player_x -= 1 if valid_player_position?(@player_x - 1, @player_y)
  when :right
    @player_x += 1 if valid_player_position?(@player_x + 1, @player_y)
  when :up
    @player_y -= 1 if valid_player_position?(@player_x, @player_y - 1)
  when :down
    @player_y += 1 if valid_player_position?(@player_x, @player_y + 1)
  end
end