Class: CellularMap::Cell

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

Overview

Map cells

Cells are generated on the fly to provide a wrapper to access to the map’s content in a more user-friendly way.

(see README for examples)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, map) ⇒ Cell

:nodoc:



15
16
17
18
19
# File 'lib/cellular_map/cell.rb', line 15

def initialize(x, y, map) # :nodoc:
  @x = x.to_i
  @y = y.to_i
  @map = map
end

Instance Attribute Details

#mapObject (readonly)

Map the cell’s a part of.



13
14
15
# File 'lib/cellular_map/cell.rb', line 13

def map
  @map
end

#xObject (readonly)

Cell’s coordinates.



11
12
13
# File 'lib/cellular_map/cell.rb', line 11

def x
  @x
end

#yObject (readonly)

Cell’s coordinates.



11
12
13
# File 'lib/cellular_map/cell.rb', line 11

def y
  @y
end

Instance Method Details

#+(vector) ⇒ Object

Selecting a related cell through a vector movement.



49
50
51
# File 'lib/cellular_map/cell.rb', line 49

def +(vector)
  @map[@x + vector.first, @y + vector.last]
end

#-(vector) ⇒ Object

Selecting a related cell through a vector movement.



54
55
56
# File 'lib/cellular_map/cell.rb', line 54

def -(vector)
  self + vector.collect { |v| -v }
end

#<=>(other) ⇒ Object

:nodoc:



40
41
42
# File 'lib/cellular_map/cell.rb', line 40

def <=>(other) # :nodoc:
  2 * (y <=> other.y) + (x <=> other.x)
end

#==(other) ⇒ Object

:nodoc:



35
36
37
38
# File 'lib/cellular_map/cell.rb', line 35

def ==(other) # :nodoc:
  self.class == other.class &&
  [@x, @y, @map] == [other.x, other.y, other.map]
end

#contentObject

Cell’s content.



22
23
24
# File 'lib/cellular_map/cell.rb', line 22

def content
  @map.store[[x, y]]
end

#content=(content) ⇒ Object

Setting new content in the cell.



27
28
29
# File 'lib/cellular_map/cell.rb', line 27

def content=(content)
  @map[@x, @y] = content
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


44
45
46
# File 'lib/cellular_map/cell.rb', line 44

def eql?(other) # :nodoc:
  self == other
end

#hashObject

:nodoc:



31
32
33
# File 'lib/cellular_map/cell.rb', line 31

def hash # :nodoc:
  x + y
end