Class: Rubygoal::Team
- Inherits:
-
Object
- Object
- Rubygoal::Team
- Extended by:
- Forwardable
- Defined in:
- lib/rubygoal/team.rb
Constant Summary collapse
- INFINITE =
100_000
Instance Attribute Summary collapse
-
#coach ⇒ Object
readonly
Returns the value of attribute coach.
-
#formation ⇒ Object
readonly
Returns the value of attribute formation.
-
#goalkeeper ⇒ Object
writeonly
Sets the attribute goalkeeper.
-
#opponent_side ⇒ Object
readonly
Returns the value of attribute opponent_side.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#side ⇒ Object
readonly
Returns the value of attribute side.
Instance Method Summary collapse
-
#initialize(game, coach) ⇒ Team
constructor
A new instance of Team.
- #players_list ⇒ Object
- #players_position ⇒ Object
- #players_to_initial_position ⇒ Object
- #update(elapsed_time) ⇒ Object
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
#coach ⇒ Object (readonly)
Returns the value of attribute coach.
13 14 15 |
# File 'lib/rubygoal/team.rb', line 13 def coach @coach end |
#formation ⇒ Object
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
14 15 16 |
# File 'lib/rubygoal/team.rb', line 14 def goalkeeper=(value) @goalkeeper = value end |
#opponent_side ⇒ Object (readonly)
Returns the value of attribute opponent_side.
13 14 15 |
# File 'lib/rubygoal/team.rb', line 13 def opponent_side @opponent_side end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
13 14 15 |
# File 'lib/rubygoal/team.rb', line 13 def players @players end |
#side ⇒ Object (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_list ⇒ Object
79 80 81 |
# File 'lib/rubygoal/team.rb', line 79 def players_list players.values end |
#players_position ⇒ Object
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_position ⇒ Object
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 |