Sha256: 4eb508309ad797b2d1581acf91f50ce23728b61e25bd305dc506ae377003be78

Contents?: true

Size: 860 Bytes

Versions: 3

Compression:

Stored size: 860 Bytes

Contents

# frozen_string_literal: true

class ProconBypassMan::Procon::AnalogStick
  attr_accessor :neutral_position
  attr_writer :bin_x, :bin_y

  def initialize(binary: )
    @neutral_position = ProconBypassMan::ButtonsSettingConfiguration.instance.neutral_position
    bytes = binary[ProconBypassMan::Procon::ButtonCollection::LEFT_ANALOG_STICK.fetch(:byte_position)]
    byte6, byte7, byte8 = bytes.each_char.map { |x| x.unpack("C").first.to_s(2).rjust(8, "0") }

    self.bin_x = "#{byte7[4..7]}#{byte6}"
    self.bin_y = "#{byte8}#{byte7[0..3]}"
    freeze
  end

  def abs_x
    @bin_x.to_i(2)
  end

  def abs_y
    @bin_y.to_i(2)
  end

  def relative_x
    @bin_x.to_i(2) - neutral_position.x
  end

  def relative_y
    @bin_y.to_i(2) - neutral_position.y
  end

  def relative_hypotenuse
    Math.sqrt((relative_x**2) + (relative_y**2)).floor(6)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
procon_bypass_man-0.3.2 lib/procon_bypass_man/procon/value_objects/analog_stick.rb
procon_bypass_man-0.3.1 lib/procon_bypass_man/procon/value_objects/analog_stick.rb
procon_bypass_man-0.3.0 lib/procon_bypass_man/procon/value_objects/analog_stick.rb