Class: BoardGameGrid::Direction

Inherits:
Object
  • Object
show all
Defined in:
lib/board_game_grid/direction.rb

Overview

Direction

The Direction that something is moving on a 2d plane

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dx, dy) ⇒ Direction

New objects can be instantiated with

Example:

# Instantiates a new Direction
BoardGameGrid::Direction.new(1, 1)

Parameters:

  • dx (Fixnum)

    the dx magnitude.

  • dy (Fixnum)

    the dy magnitude.



19
20
21
22
23
24
# File 'lib/board_game_grid/direction.rb', line 19

def initialize(dx, dy)
  x = dx == 0 ? dx : dx/dx.abs
  y = dy == 0 ? dy : dy/dy.abs

  @x, @y = x, y
end

Instance Attribute Details

#xFixnum (readonly)

Returns the x magnitude.

Returns:

  • (Fixnum)

    the x magnitude.



27
28
29
# File 'lib/board_game_grid/direction.rb', line 27

def x
  @x
end

#yFixnum (readonly)

Returns the y magnitude.

Returns:

  • (Fixnum)

    the y magnitude.



30
31
32
# File 'lib/board_game_grid/direction.rb', line 30

def y
  @y
end

Instance Method Details

#==(other) ⇒ Boolean

Check if directions are equal by seeing if their magnitudes are equal.

Parameters:

  • other (Direction)

    the other direction to compare to.

Returns:

  • (Boolean)


38
39
40
# File 'lib/board_game_grid/direction.rb', line 38

def ==(other)
  self.x == other.x && self.y == other.y
end