Class: RubyFighter::Controls

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_fighter/controls.rb

Constant Summary collapse

PLAYER1 =
{
  'a' => :left,
  'd' => :right,
  'q' => :block,
  'e' => :punch,
  'r' => :kick
}
PLAYER2 =
{
  'k' => :left,
  ';' => :right,
  'i' => :block,
  'p' => :punch,
  '[' => :kick
}

Instance Method Summary collapse

Constructor Details

#initialize(window, player, num) ⇒ Controls

Returns a new instance of Controls.



20
21
22
23
24
# File 'lib/ruby_fighter/controls.rb', line 20

def initialize(window, player, num)
  @window = window
  @player = player
  @keys   = [PLAYER1, PLAYER2][num-1]
end

Instance Method Details

#button_down(key) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ruby_fighter/controls.rb', line 33

def button_down(key)
  case @keys[key]
  when :left, :right then @player.walking!
  when :block then @player.blocking!
  when :punch then @player.punch!
  when :kick  then @player.kick!
  end
end

#button_up(key) ⇒ Object



42
43
44
# File 'lib/ruby_fighter/controls.rb', line 42

def button_up(key)
  @player.idle!
end

#update(left, right) ⇒ Object



26
27
28
29
30
31
# File 'lib/ruby_fighter/controls.rb', line 26

def update(left, right)
  case matching_action
  when :left  then @player.move_left  if @player.left > left
  when :right then @player.move_right if @player.right < right
  end
end