Class: Rubygoal::Team

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubygoal/team.rb

Constant Summary collapse

INFINITE =
100_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_window, side, coach) ⇒ Team

Returns a new instance of Team.



18
19
20
21
22
23
24
25
26
# File 'lib/rubygoal/team.rb', line 18

def initialize(game_window, side, coach)
  @game = game_window
  @players = []
  @side = side
  @coach = coach

  initialize_formation
  initialize_players(game_window)
end

Instance Attribute Details

#coachObject (readonly)

Returns the value of attribute coach.



10
11
12
# File 'lib/rubygoal/team.rb', line 10

def coach
  @coach
end

#formationObject

Returns the value of attribute formation.



10
11
12
# File 'lib/rubygoal/team.rb', line 10

def formation
  @formation
end

#gameObject (readonly)

Returns the value of attribute game.



10
11
12
# File 'lib/rubygoal/team.rb', line 10

def game
  @game
end

#goalkeeperObject

Returns the value of attribute goalkeeper.



11
12
13
# File 'lib/rubygoal/team.rb', line 11

def goalkeeper
  @goalkeeper
end

#playersObject (readonly)

Returns the value of attribute players.



10
11
12
# File 'lib/rubygoal/team.rb', line 10

def players
  @players
end

#positionsObject

Returns the value of attribute positions.



11
12
13
# File 'lib/rubygoal/team.rb', line 11

def positions
  @positions
end

#sideObject (readonly)

Returns the value of attribute side.



10
11
12
# File 'lib/rubygoal/team.rb', line 10

def side
  @side
end

Instance Method Details

#drawObject



93
94
95
# File 'lib/rubygoal/team.rb', line 93

def draw
  players.each(&:draw)
end

#players_to_initial_positionObject



28
29
30
31
32
33
# File 'lib/rubygoal/team.rb', line 28

def players_to_initial_position
  positions = FieldMetrics.initial_player_positions(side)
  players.each_with_index do |player, index|
    player.position = positions[index]
  end
end

#update(match) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubygoal/team.rb', line 35

def update(match)
  self.formation = coach.formation(match)
  unless formation.valid?
    puts formation.errors
    raise "Invalid formation: #{coach.name}"
  end
  update_positions(formation.lineup)

  player_to_move = nil
  min_distance_to_ball = INFINITE
  players.each do |player|
    pass_or_shoot(player) if player.can_kick?(game.ball)

    distance_to_ball = player.distance(game.ball.position)
    if min_distance_to_ball > distance_to_ball
      min_distance_to_ball = distance_to_ball
      player_to_move = player
    end
  end

  player_to_move.move_to(game.ball.position)

  average_players = []
  fast_players = []
  captain_player = nil

  players.each do |player|
    if player.is_a? AveragePlayer
      average_players << player
    elsif player.is_a? FastPlayer
      fast_players << player
    else
      captain_player = player
    end
  end

  if captain_player != player_to_move
    captain_player.move_to(positions[:captain])
  end
  captain_player.update

  average_players.each_with_index do |player, index|
    if player != player_to_move
      player.move_to(positions[:average][index])
    end

    player.update
  end

  fast_players.each_with_index do |player, index|
    if player != player_to_move
      player.move_to(positions[:fast][index])
    end

    player.update
  end
end