class PlayerInput

Constants

NAME_COLOR

Dark green

Attributes

stats[R]

Public Class Methods

new(name, camera, object_pool) click to toggle source
Calls superclass method Component::new
# File lib/entities/components/player_input.rb, line 6
def initialize(name, camera, object_pool)
  super(nil)
  @name = name
  @stats = Stats.new(name)
  @camera = camera
  @object_pool = object_pool
end

Public Instance Methods

control(obj) click to toggle source
# File lib/entities/components/player_input.rb, line 14
def control(obj)
  self.object = obj
  obj.components << self
end
draw(viewport) click to toggle source
# File lib/entities/components/player_input.rb, line 47
def draw(viewport)
  @name_image ||= Gosu::Image.from_text(
    $window, @name, Gosu.default_font_name, 20)
  @name_image.draw(
    x - @name_image.width / 2 - 1,
    y + object.graphics.height / 2, 100,
    1, 1, Gosu::Color::WHITE)
  @name_image.draw(
    x - @name_image.width / 2,
    y + object.graphics.height / 2, 100,
    1, 1, NAME_COLOR)
end
on_collision(with) click to toggle source
# File lib/entities/components/player_input.rb, line 19
def on_collision(with)
end
on_damage(amount) click to toggle source
# File lib/entities/components/player_input.rb, line 22
def on_damage(amount)
  @stats.add_damage(amount)
end
update() click to toggle source
# File lib/entities/components/player_input.rb, line 26
def update
  return respawn if object.health.dead?
  d_x, d_y = @camera.target_delta_on_screen
  atan = Math.atan2(($window.width / 2) - d_x - $window.mouse_x,
                    ($window.height / 2) - d_y - $window.mouse_y)
  object.gun_angle = -atan * 180 / Math::PI
  motion_buttons = [Gosu::KbW, Gosu::KbS, Gosu::KbA, Gosu::KbD]

  if any_button_down?(*motion_buttons)
    object.throttle_down = true
    object.physics.change_direction(
      change_angle(object.direction, *motion_buttons))
  else
    object.throttle_down = false
  end

  if Utils.button_down?(Gosu::MsLeft)
    object.shoot(*@camera.mouse_coords)
  end
end

Private Instance Methods

any_button_down?(*buttons) click to toggle source
# File lib/entities/components/player_input.rb, line 71
def any_button_down?(*buttons)
  buttons.each do |b|
    return true if Utils.button_down?(b)
  end
  false
end
change_angle(previous_angle, up, down, right, left) click to toggle source
# File lib/entities/components/player_input.rb, line 78
def change_angle(previous_angle, up, down, right, left)
  if Utils.button_down?(up)
    angle = 0.0
    angle += 45.0 if Utils.button_down?(left)
    angle -= 45.0 if Utils.button_down?(right)
  elsif Utils.button_down?(down)
    angle = 180.0
    angle -= 45.0 if Utils.button_down?(left)
    angle += 45.0 if Utils.button_down?(right)
  elsif Utils.button_down?(left)
    angle = 90.0
    angle += 45.0 if Utils.button_down?(up)
    angle -= 45.0 if Utils.button_down?(down)
  elsif Utils.button_down?(right)
    angle = 270.0
    angle -= 45.0 if Utils.button_down?(up)
    angle += 45.0 if Utils.button_down?(down)
  end
  angle = (angle + 360) % 360 if angle && angle < 0
  (angle || previous_angle)
end
respawn() click to toggle source
# File lib/entities/components/player_input.rb, line 62
def respawn
  if object.health.should_respawn?
    object.health.restore
    object.move(*@object_pool.map.spawn_point)
    @camera.x, @camera.y = x, y
    PlayerSounds.respawn(object, @camera)
  end
end