Class: RSokoban::LayeredMap

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

Overview

I separate elements of the map from the floor/wall.

Since:

  • 0.74.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ LayeredMap

Returns a new instance of LayeredMap.

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 0.74.1



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

#cratesArray<Crate> (readonly)

Returns:

Since:

  • 0.74.1



13
14
15
# File 'lib/rsokoban/layered_map.rb', line 13

def crates
  @crates
end

#floorArray<String> (readonly)

Returns map with only walls and floor.

Returns:

  • (Array<String>)

    map with only walls and floor

Since:

  • 0.74.1



7
8
9
# File 'lib/rsokoban/layered_map.rb', line 7

def floor
  @floor
end

#manMan (readonly)

Returns:

Since:

  • 0.74.1



10
11
12
# File 'lib/rsokoban/layered_map.rb', line 10

def man
  @man
end

#storagesArray<Storage> (readonly)

Returns:

Since:

  • 0.74.1



16
17
18
# File 'lib/rsokoban/layered_map.rb', line 16

def storages
  @storages
end

Instance Method Details

#map_as_arrayArray<String>

Get an instant map of the game.

Returns:

  • (Array<String>)

    the map, after X turns of game.

Since:

  • 0.74.1



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

Parameters:

  • x_coord (Fixnum)

    x coordinate in the map

  • y_coord (Fixnum)

    y coordinate in the map

Returns:

  • (' ' | '#' | '.' | 'o' | '*')

Since:

  • 0.74.1



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