Class: Hlockey::Game::Fight

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/hlockey/game/fight.rb

Overview

A fight within a Hlockey game

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, starting_player = nil) ⇒ Fight

Returns a new instance of Fight.

Parameters:

  • game (Game)

    the game the fight is a part of

  • starting_player (Team::Player) (defaults to: nil)

    the player that started the fight, if any



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hlockey/game/fight.rb', line 20

def initialize(game, starting_player = nil)
  @game = game
  @actions = 0
  @players = { home: [], away: [] }
  @players.each_key do |team|
    add_player(if @game.send(team).roster.value?(starting_player)
                 starting_player
               else
                 get_joining_player(team)
               end,
               team)
  end

  @score = { home: 0, away: 0 }
end

Instance Attribute Details

#playersHash<Symbol => Array<Team::Player>> (readonly)

Returns the players in the fight.

Returns:

  • (Hash<Symbol => Array<Team::Player>>)

    the players in the fight



13
14
15
# File 'lib/hlockey/game/fight.rb', line 13

def players
  @players
end

#scoreHash<Symbol => Integer> (readonly)

Returns the amount of successful punches for each team.

Returns:

  • (Hash<Symbol => Integer>)

    the amount of successful punches for each team



16
17
18
# File 'lib/hlockey/game/fight.rb', line 16

def score
  @score
end

Instance Method Details

#next_actionObject



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
# File 'lib/hlockey/game/fight.rb', line 36

def next_action
  @actions += 1

  # If fight has a single player on one side who is outnumbered,
  # there is a chance that player will withdraw, ending the fight.
  if @players[:home].length == 1
    return if @players[:away].length > 1 && player_withdraws?(@players[:home].first,
                                                              :home)
  elsif @players[:away].length == 1
    return if player_withdraws?(@players[:away].first, :away)
  end

  case @game.prng.rand(@actions)
  when 0..3 # attack
    attack
  when 4..7 # player joins
    team_joining = opposite(rand_team)

    player_joining = get_joining_player(team_joining)
    if player_joining.nil?
      team_joining = opposite(team_joining)
      player_joining = get_joining_player(team_joining)
      if player_joining.nil?
        attack
        return
      end
    end

    add_player(player_joining, team_joining)
  else # fight ends
    @game.stream << Message.new(:fight_end)
  end
end