Class: Rubygoal::Team

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

Direct Known Subclasses

AwayTeam, HomeTeam

Constant Summary collapse

INFINITE =
100_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, coach) ⇒ Team

Returns a new instance of Team.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubygoal/team.rb', line 22

def initialize(game, coach)
  @game    = game
  @players = {}
  @coach   = coach

  @match_data_factory = MatchData::Factory.new(game, side)

  initialize_lineup_values
  initialize_players
  initialize_formation
end

Instance Attribute Details

#coachObject (readonly)

Returns the value of attribute coach.



13
14
15
# File 'lib/rubygoal/team.rb', line 13

def coach
  @coach
end

#formationObject

Returns the value of attribute formation.



13
14
15
# File 'lib/rubygoal/team.rb', line 13

def formation
  @formation
end

#goalkeeper=(value) ⇒ Object

Sets the attribute goalkeeper

Parameters:

  • value

    the value to set the attribute goalkeeper to.



14
15
16
# File 'lib/rubygoal/team.rb', line 14

def goalkeeper=(value)
  @goalkeeper = value
end

#opponent_sideObject (readonly)

Returns the value of attribute opponent_side.



13
14
15
# File 'lib/rubygoal/team.rb', line 13

def opponent_side
  @opponent_side
end

#playersObject (readonly)

Returns the value of attribute players.



13
14
15
# File 'lib/rubygoal/team.rb', line 13

def players
  @players
end

#sideObject (readonly)

Returns the value of attribute side.



13
14
15
# File 'lib/rubygoal/team.rb', line 13

def side
  @side
end

Instance Method Details

#players_listObject



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

def players_list
  players.values
end

#players_positionObject



83
84
85
86
87
# File 'lib/rubygoal/team.rb', line 83

def players_position
  players.each_with_object({}) do |(name, player), hash|
    hash[name] = Field.field_position(player.position, side)
  end
end

#players_to_initial_positionObject



34
35
36
37
38
# File 'lib/rubygoal/team.rb', line 34

def players_to_initial_position
  match_data = match_data_factory.create
  formation = coach.formation(match_data)
  restart_player_positions_in_own_field(formation)
end

#update(elapsed_time) ⇒ Object



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
# File 'lib/rubygoal/team.rb', line 40

def update(elapsed_time)
  match_data = match_data_factory.create
  self.formation = coach.formation(match_data)

  unless formation.valid?
    puts formation.errors
    raise "Invalid formation: #{coach.name}"
  end

  update_coach_defined_positions(formation)

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

    distance_to_ball = player.distance(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(ball.position)

  players.each do |name, player|
    if name == :goalkeeper
      if player != player_to_move
        player.move_to_cover_goal(ball)
        player.update(elapsed_time)
        next
      end
    end

    player.move_to_coach_position unless player == player_to_move
    player.update(elapsed_time)
  end
end