Class: RSokoban::LayeredMap
- Inherits:
-
Object
- Object
- RSokoban::LayeredMap
- Defined in:
- lib/rsokoban/layered_map.rb
Overview
I separate elements of the map from the floor/wall.
Instance Attribute Summary collapse
- #crates ⇒ Array<Crate> readonly
-
#floor ⇒ Array<String>
readonly
Map with only walls and floor.
- #man ⇒ Man readonly
- #storages ⇒ Array<Storage> readonly
Instance Method Summary collapse
-
#initialize(map) ⇒ LayeredMap
constructor
A new instance of LayeredMap.
-
#map_as_array ⇒ Array<String>
Get an instant map of the game.
-
#what_is_on(x_coord, y_coord) ⇒ ' ' | '#' | '.' | 'o' | '*'
Get the content of box x_coord, y_coord.
Constructor Details
#initialize(map) ⇒ LayeredMap
Returns a new instance of LayeredMap.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rsokoban/layered_map.rb', line 19 def initialize map raise ArgumentError unless [Map, Array].include?(map.class) @map = map @floor = nil @map_array = [] @man = nil @crates = [] @storages = [] init_floor init_man init_crates_and_storages end |
Instance Attribute Details
#crates ⇒ Array<Crate> (readonly)
13 14 15 |
# File 'lib/rsokoban/layered_map.rb', line 13 def crates @crates end |
#floor ⇒ Array<String> (readonly)
Returns map with only walls and floor.
7 8 9 |
# File 'lib/rsokoban/layered_map.rb', line 7 def floor @floor end |
#storages ⇒ Array<Storage> (readonly)
16 17 18 |
# File 'lib/rsokoban/layered_map.rb', line 16 def storages @storages end |
Instance Method Details
#map_as_array ⇒ Array<String>
Get an instant map of the game.
34 35 36 37 38 39 40 |
# File 'lib/rsokoban/layered_map.rb', line 34 def map_as_array @map_array = init_floor draw_crates draw_storages draw_man @map_array end |
#what_is_on(x_coord, y_coord) ⇒ ' ' | '#' | '.' | 'o' | '*'
TODO:
I think we can optimize this algo
TODO:
should accept too only one argument: a Position (or an object inherited from Position)
Get the content of box x_coord, y_coord
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rsokoban/layered_map.rb', line 48 def what_is_on x_coord, y_coord return WALL if (@floor[y_coord][x_coord]).chr == WALL position = Position.new(x_coord, y_coord) if @storages.include?(position) return STORAGE unless @crates.include?(position) CRATE_ON_STORAGE elsif @crates.include?(position) CRATE else FLOOR end end |