Class: Cell
- Inherits:
-
Array
- Object
- Array
- Cell
- Defined in:
- lib/game_2d/game_space.rb
Overview
Cell is a portion of the game space, the exact size of one entity. The cell (0,0) contains subpixel coordinates (0,0) through (399,399).
The behavior I want from Cells is to consider them all unique objects. I want to be able to say “Subtract this set of cells from that set”. Treating Cells as equal if their contents are equal defeats this purpose.
It’s also handy if each Cell knows where it lives in the grid.
Previously, I was using Set as the superclass. That seemed to make sense, since this is an unordered collection. But Set stores everything as hash keys, and hashes get very confused if their keys get mutated without going through the API.
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(cell_x, cell_y) ⇒ Cell
constructor
A new instance of Cell.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(cell_x, cell_y) ⇒ Cell
Returns a new instance of Cell.
41 42 43 44 45 |
# File 'lib/game_2d/game_space.rb', line 41 def initialize(cell_x, cell_y) @a = [] @x, @y = cell_x, cell_y super(@a) end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
32 33 34 |
# File 'lib/game_2d/game_space.rb', line 32 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
32 33 34 |
# File 'lib/game_2d/game_space.rb', line 32 def y @y end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/game_2d/game_space.rb', line 34 def ==(other) other.class.equal?(self.class) && other.x == self.x && other.y == self.y && other.instance_variable_get(:@a) == @a end |
#inspect ⇒ Object
48 |
# File 'lib/game_2d/game_space.rb', line 48 def inspect; "Cell(#{x}, #{y}) #{@a}"; end |
#to_s ⇒ Object
47 |
# File 'lib/game_2d/game_space.rb', line 47 def to_s; "(#{x}, #{y}) [#{@a.join(', ')}]"; end |