Module: Position

Defined in:
tomes/spells/geometry/position.rb

Overview

A module for calculating the position of one object in relation to another.

Class Method Summary collapse

Class Method Details

.above(object, spacing: 1) ⇒ Object

Calculates a point above the object (along the Y-axis only), including an optional spacing parameter. (This method does not assume the object includes the Apex module.)

Parameters:

  • object (Object)
  • spacing (Integer) (defaults to: 1)

Returns:

  • Integer



59
60
61
# File 'tomes/spells/geometry/position.rb', line 59

def above(object, spacing: 1)
  object.y + object.height + spacing
end

.add_coordinates(first_coordinates, second_coordinates) ⇒ Array<Integer, Integer>

Adds one set of coordinates to the other.

Parameters:

  • first_coordinates (Array<Integer, Integer>)
  • second_coordinates (Array<Integer, Integer>)

Returns:

  • (Array<Integer, Integer>)

    The sum of the two.



32
33
34
# File 'tomes/spells/geometry/position.rb', line 32

def add_coordinates(first_coordinates, second_coordinates)
  [first_coordinates[0] + second_coordinates[0], first_coordinates[1] + second_coordinates[1]]
end

.below(object, spacing: 1) ⇒ Object

Calculates a point below the object (along the Y-axis only), including an optional spacing parameter. (This method does not assume the object includes the Apex module.)

Parameters:

  • object (Object)
  • spacing (Integer) (defaults to: 1)

Returns:

  • Integer



68
69
70
# File 'tomes/spells/geometry/position.rb', line 68

def below(object, spacing: 1)
  object.y - spacing
end

.left_of(object, spacing: 1) ⇒ Object

Calculates a point left of the object (along the X-axis only), including an optional spacing parameter. (This method does not assume the object includes the Apex module.)

Parameters:

  • object (Object)
  • spacing (Integer) (defaults to: 1)

Returns:

  • Integer



41
42
43
# File 'tomes/spells/geometry/position.rb', line 41

def left_of(object, spacing: 1)
  object.x - spacing
end

.right_of(object, spacing: 1) ⇒ Object

Calculates a point right of the object (along the X-axis only), including an optional spacing parameter. (This method does not assume the object includes the Apex module.)

Parameters:

  • object (Object)
  • spacing (Integer) (defaults to: 1)

Returns:

  • Integer



50
51
52
# File 'tomes/spells/geometry/position.rb', line 50

def right_of(object, spacing: 1)
  object.x + object.width + spacing
end

.subtract_coordinates(first_coordinates, second_coordinates) ⇒ Array<Integer, Integer>

Subtracts one set of coordinates from the other.

Parameters:

  • first_coordinates (Array<Integer, Integer>)
  • second_coordinates (Array<Integer, Integer>)

Returns:

  • (Array<Integer, Integer>)

    The difference between the two.



24
25
26
# File 'tomes/spells/geometry/position.rb', line 24

def subtract_coordinates(first_coordinates, second_coordinates)
  [first_coordinates[0] - second_coordinates[0], first_coordinates[1] - second_coordinates[1]]
end

.these_collide?(primary_object, secondary_object) ⇒ Boolean Also known as: collide?

Checks if two objects collide.

Parameters:

Returns:

  • (Boolean)


12
13
14
15
16
17
# File 'tomes/spells/geometry/position.rb', line 12

def these_collide?(primary_object, secondary_object)
  primary_object.top > secondary_object.bottom and
    primary_object.left < secondary_object.right and
    primary_object.bottom < secondary_object.top and
    primary_object.right > secondary_object.left
end