Class: XenosEnigma::Radar

Inherits:
Object
  • Object
show all
Includes:
DefaultData
Defined in:
lib/xenos_enigma/radar.rb

Overview

Radar will orchestrate scan of radar data, xenos detection and echo of those findings

Constant Summary

Constants included from DefaultData

DefaultData::SCAN_DATA

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Radar

Returns a new instance of Radar.



12
13
14
15
16
# File 'lib/xenos_enigma/radar.rb', line 12

def initialize(data = nil)
  @data = (data || SCAN_DATA).split(/\n/)
  @known_xenos = load_all_known_xenos
  @hit_collector = XenosEnigma::HitCollector.new
end

Instance Method Details

#echoObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xenos_enigma/radar.rb', line 32

def echo
  echo_width  = @data.first.size - 1
  echo_height = @data.size - 1
  print "\033[33m"

  (0..echo_height).each do |echo_y|
    (0..echo_width).each do |echo_x|
      hit_data = @hit_collector.detection_data(echo_x, echo_y)
      print hit_data || '-'
    end
    puts
  end
end

#scanObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xenos_enigma/radar.rb', line 18

def scan
  @data.each_with_index do |scan_row, scan_position_y|
    (0..scan_row.length).each do |scan_position_x|
      next if @hit_collector.already_detected?(scan_position_x, scan_position_y)

      @known_xenos.each do |xeno|
        partial = scan_row_partial(scan_row, scan_position_x, xeno.ship_width)
        hit = xeno.analyze?(partial, look_ahead_data(scan_position_x, scan_position_y, xeno))
        @hit_collector.push(hit, scan_position_x, scan_position_y) if hit
      end
    end
  end
end