class XenosEnigma::HitCollector

HitCollector will consume all ship hits, and cache their data and coordinates

Public Class Methods

new() click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 4
def initialize
  @hit_matrix_cache = {}
end

Public Instance Methods

already_detected?(position_x, position_y) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 12
def already_detected?(position_x, position_y)
  !get_cache(position_x, position_y).nil?
end
detection_data(position_x, position_y) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 8
def detection_data(position_x, position_y)
  get_cache(position_x, position_y)
end
push(xeno_hit, scan_position_x, scan_position_y) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 16
def push(xeno_hit, scan_position_x, scan_position_y)
  xeno_hit.radar_x_position = scan_position_x
  xeno_hit.radar_y_position = scan_position_y

  consume(xeno_hit)
end

Private Instance Methods

cache_key(position_x, position_y) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 45
def cache_key(position_x, position_y)
  "#{position_x}::#{position_y}"
end
consume(xeno_hit) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 25
def consume(xeno_hit)
  xeno_data = xeno_hit.xeno_instance.xeno_signature

  xeno_data.each_with_index do |xeno_row_data, xeno_y|
    next if xeno_y < xeno_hit.xeno_y_start

    xeno_row = xeno_row_data.split(//)
    xeno_row.each_with_index do |xeno_char, xeno_x|
      global_x = xeno_hit.radar_x_position + xeno_x
      global_y = xeno_hit.radar_y_position + xeno_y - xeno_hit.xeno_y_start

      @hit_matrix_cache[cache_key(global_x, global_y)] = xeno_char
    end
  end
end
get_cache(position_x, position_y) click to toggle source
# File lib/xenos_enigma/hit_collector.rb, line 41
def get_cache(position_x, position_y)
  @hit_matrix_cache[cache_key(position_x, position_y)]
end