Class: Meiro::BaseMap

Inherits:
MapLayer show all
Defined in:
lib/meiro/map_layer.rb

Instance Method Summary collapse

Methods inherited from MapLayer

#[], #[]=, #dup, #each_line, #each_tile, #get_around, #height, #width

Constructor Details

#initialize(width, height, klass = nil) ⇒ BaseMap

Returns a new instance of BaseMap.



62
63
64
65
66
# File 'lib/meiro/map_layer.rb', line 62

def initialize(width, height, klass=nil)
  proc = klass.nil? ? lambda { nil } : lambda { klass.new }
  @map = Array.new(height)
  @map.map! {|line| Array.new(width).map! {|e| proc.call } }
end

Instance Method Details

#apply_passage(rooms, gate_klass, pass_klass) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/meiro/map_layer.rb', line 85

def apply_passage(rooms, gate_klass, pass_klass)
  all_pass = []
  all_gates = []
  rooms.each do |room|
    all_pass << room.all_pass
    all_gates << room.gate_coordinates
  end

  all_pass.flatten.uniq.each do |p|
    self.fill_rect(p.start_x, p.start_y, p.end_x, p.end_y, pass_klass)
  end

  all_gates.flatten(1).each do |x, y|
    self[x, y] = gate_klass.new
  end
end

#apply_room(room, klass) ⇒ Object



79
80
81
82
83
# File 'lib/meiro/map_layer.rb', line 79

def apply_room(room, klass)
  room.each_coordinate do |x, y|
    self[x, y] = klass.new
  end
end

#classify(type = :rogue_like) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/meiro/map_layer.rb', line 102

def classify(type=:rogue_like)
  res = self.class.new(width, height)
  res.each_tile do |x, y, tile|
    res[x, y] = Tile.classify(self.get_around(x, y), type)
  end
  res
end

#classify!(type = :rogue_like) ⇒ Object



110
111
112
113
114
115
# File 'lib/meiro/map_layer.rb', line 110

def classify!(type=:rogue_like)
  self.each_tile do |x, y, tile|
    self[x, y] = Tile.classify(self.get_around(x, y), type)
  end
  self
end

#fill_rect(x1, y1, x2, y2, klass) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/meiro/map_layer.rb', line 68

def fill_rect(x1, y1, x2, y2, klass)
  x_begin, x_end = x1 <= x2 ? [x1, x2] : [x2, x1]
  y_begin, y_end = y1 <= y2 ? [y1, y2] : [y2, y1]

  (y_begin..y_end).each do |y|
    (x_begin..x_end).each do |x|
      self[x, y] = klass.new
    end
  end
end