module Rubygoal::Field

Constants

CLOSE_GOAL_DISTANCE
GOAL_HEIGHT
HEIGHT
OFFSET
WIDTH

Public Class Methods

absolute_position(field_position, side) click to toggle source
# File lib/rubygoal/field.rb, line 36
def absolute_position(field_position, side)
  case side
  when :home
    OFFSET.add(field_position)
  when :away
    OFFSET.add(
      Position.new(
        WIDTH - field_position.x,
        HEIGHT - field_position.y
      )
    )
  end
end
center_position() click to toggle source
# File lib/rubygoal/field.rb, line 16
def center_position
  center = Position.new(
    WIDTH / 2,
    HEIGHT / 2
  )
  OFFSET.add(center)
end
close_to_goal?(position, side) click to toggle source
# File lib/rubygoal/field.rb, line 108
def close_to_goal?(position, side)
  goal_position = Field.goal_position(side)
  goal_position.distance(position) < CLOSE_GOAL_DISTANCE
end
default_player_field_positions() click to toggle source
# File lib/rubygoal/field.rb, line 113
def default_player_field_positions
  defenders = Field::WIDTH / 6
  midfielders = Field::WIDTH / 2
  attackers = Field::WIDTH * 5 / 6

  [
    Position.new(50, Field::HEIGHT / 2),
    Position.new(defenders, Field::HEIGHT / 5),
    Position.new(defenders, Field::HEIGHT * 2 / 5),
    Position.new(defenders, Field::HEIGHT * 3 / 5),
    Position.new(defenders, Field::HEIGHT * 4 / 5),
    Position.new(midfielders, Field::HEIGHT / 5),
    Position.new(midfielders, Field::HEIGHT * 2 / 5),
    Position.new(midfielders, Field::HEIGHT * 3 / 5),
    Position.new(midfielders, Field::HEIGHT * 4 / 5),
    Position.new(attackers, Field::HEIGHT / 3),
    Position.new(attackers, Field::HEIGHT * 2 / 3)
  ]
end
field_position(absolute_position, side) click to toggle source
# File lib/rubygoal/field.rb, line 50
def field_position(absolute_position, side)
  case side
  when :home
    absolute_position.add(
      Position.new(
        - OFFSET.x,
        - OFFSET.y
      )
    )
  when :away
    Position.new(
      WIDTH - (absolute_position.x - OFFSET.x),
      HEIGHT - (absolute_position.y - OFFSET.y)
    )
  end
end
goal?(position) click to toggle source
# File lib/rubygoal/field.rb, line 97
def goal?(position)
  if out_of_bounds_width?(position)
    lower_limit = center_position.y - GOAL_HEIGHT / 2
    upper_limit = center_position.y + GOAL_HEIGHT / 2

    (lower_limit..upper_limit).include?(position.y)
  else
    false
  end
end
goal_position(side) click to toggle source
# File lib/rubygoal/field.rb, line 24
def goal_position(side)
  position = center_position
  case side
  when :home
    position.x = OFFSET.x
  when :away
    position.x = OFFSET.x + WIDTH
  end

  position
end
out_of_bounds_height?(position) click to toggle source
# File lib/rubygoal/field.rb, line 91
def out_of_bounds_height?(position)
  lower_limit = OFFSET.y
  upper_limit = OFFSET.y + HEIGHT
  !(lower_limit..upper_limit).include?(position.y)
end
out_of_bounds_width?(position) click to toggle source
# File lib/rubygoal/field.rb, line 85
def out_of_bounds_width?(position)
  lower_limit = OFFSET.x
  upper_limit = OFFSET.x + WIDTH
  !(lower_limit..upper_limit).include?(position.x)
end
position_from_percentages(position_in_percentages) click to toggle source
# File lib/rubygoal/field.rb, line 67
def position_from_percentages(position_in_percentages)
  Position.new(
    position_in_percentages.x / 100.0 * Field::WIDTH,
    position_in_percentages.y / 100.0 * Field::HEIGHT
  )
end
position_side(position) click to toggle source
# File lib/rubygoal/field.rb, line 81
def position_side(position)
  position.x < center_position.x ? :home : :away
end
position_to_percentages(position) click to toggle source
# File lib/rubygoal/field.rb, line 74
def position_to_percentages(position)
  Position.new(
    position.x / Field::WIDTH * 100,
    position.y / Field::HEIGHT * 100
  )
end