Module: Rubygoal::Field

Defined in:
lib/rubygoal/field.rb

Constant Summary collapse

WIDTH =
1394.0
HEIGHT =
938.0
OFFSET =
Position.new(262, 112)
GOAL_HEIGHT =
275
CLOSE_GOAL_DISTANCE =
275

Class Method Summary collapse

Class Method Details

.absolute_position(field_position, side) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_positionObject



16
17
18
19
20
21
22
# 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) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
# 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_positionsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# 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) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# 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) ⇒ Object



67
68
69
70
71
72
# 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) ⇒ Object



81
82
83
# File 'lib/rubygoal/field.rb', line 81

def position_side(position)
  position.x < center_position.x ? :home : :away
end

.position_to_percentages(position) ⇒ Object



74
75
76
77
78
79
# 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