Class: JustChess::Direction

Inherits:
Object
  • Object
show all
Defined in:
lib/just_chess/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
JustChess::Direction.new({
  dx: 1,
  dy: 1
})

Parameters:

  • dx (Fixnum)

    the dx magnitude.

  • dy (Fixnum)

    the dy magnitude.



22
23
24
25
26
27
# File 'lib/just_chess/direction.rb', line 22

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.



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

def x
  @x
end

#yFixnum (readonly)

Returns the y magnitude.

Returns:

  • (Fixnum)

    the y magnitude.



33
34
35
# File 'lib/just_chess/direction.rb', line 33

def y
  @y
end