Module: Rubygoal::FieldMetrics

Defined in:
lib/rubygoal/field_metrics.rb

Class Method Summary collapse

Class Method Details

.center_positionObject



8
9
10
11
12
13
14
# File 'lib/rubygoal/field_metrics.rb', line 8

def center_position
  center = Position.new(
    Config.field.width / 2,
    Config.field.height / 2
  )
  Config.field.offset.add(center)
end

.close_to_goal?(position, side) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/rubygoal/field_metrics.rb', line 79

def close_to_goal?(position, side)
  goal_position = FieldMetrics.goal_position(side)
  goal_position.distance(position) < Config.field.close_to_goal_distance
end

.goal?(position) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# File 'lib/rubygoal/field_metrics.rb', line 68

def goal?(position)
  if out_of_bounds_width?(position)
    lower_limit = center_position.y - Config.field.goal_height / 2
    upper_limit = center_position.y + Config.field.goal_height / 2

    (lower_limit..upper_limit).include?(position.y)
  else
    false
  end
end

.goal_position(side) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/rubygoal/field_metrics.rb', line 16

def goal_position(side)
  position = center_position
  case side
  when :home
    position.x = Config.field.offset.x
  when :away
    position.x = Config.field.offset.x + Config.field.width
  end
  position
end

.goalkeeper_position(side) ⇒ Object



44
45
46
# File 'lib/rubygoal/field_metrics.rb', line 44

def goalkeeper_position(side)
  initial_player_positions(side).first
end

.initial_player_positions(side) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubygoal/field_metrics.rb', line 27

def initial_player_positions(side)
  Config.team.initial_player_positions.map do |pos|
    case side
    when :home
      Position.new(
        Config.field.offset.x + pos[0],
        Config.field.offset.y + pos[1]
      )
    when :away
      Position.new(
        Config.field.offset.x + Config.field.width - pos[0],
        Config.field.offset.y + Config.field.height - pos[1]
      )
    end
  end
end

.out_of_bounds_height?(position) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/rubygoal/field_metrics.rb', line 62

def out_of_bounds_height?(position)
  lower_limit = Config.field.offset.y
  upper_limit = Config.field.offset.y + Config.field.height
  !(lower_limit..upper_limit).include?(position.y)
end

.out_of_bounds_width?(position) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/rubygoal/field_metrics.rb', line 56

def out_of_bounds_width?(position)
  lower_limit = Config.field.offset.x
  upper_limit = Config.field.offset.x + Config.field.width
  !(lower_limit..upper_limit).include?(position.x)
end

.position_side(position) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rubygoal/field_metrics.rb', line 48

def position_side(position)
  if position.x < FieldMetrics.center_position.x
    :home
  else
    :away
  end
end