Sha256: 8cbc5685cd7567f69a37d8bd6f5c615c6915a0c8fe370f53804f510726c05e55

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

module RTanque
  class Bot
    class Radar
      include Enumerable
      include Movable
      extend NormalizedAttr
      VISION_RANGE = Configuration.radar.vision
      attr_normalized(:heading, Heading::FULL_RANGE, Configuration.radar.turn_step)

      # A Reflection is the information obtained for a bot detected by {RTanque::Bot::Radar}
      #
      # @attr_reader [RTanque::Heading] heading
      # @attr_reader [Float] distance
      # @attr_reader [String] name
      Reflection = Struct.new(:heading, :distance, :name) do
        def self.new_from_points(from_position, to_position, &tap)
          self.new(from_position.heading(to_position), from_position.distance(to_position)).tap(&tap)
        end
      end

      def initialize(bot, heading)
        @bot = bot
        @heading = heading
        @reflections = []
      end

      def position
        @bot.position
      end

      def each(&block)
        @reflections.each(&block)
      end

      def empty?
        self.count == 0
      end

      def scan(bots)
        @reflections.clear
        bots.each do |other_bot|
          if self.can_detect?(other_bot)
            @reflections << Reflection.new_from_points(self.position, other_bot.position) { |reflection| reflection.name = other_bot.name }
          end
        end
        self
      end

      def can_detect?(other_bot)
        VISION_RANGE.include?(Heading.delta_between_points(self.position, self.heading, other_bot.position))
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rtanque-0.1.3 lib/rtanque/bot/radar.rb
colstrom-rtanque-0.1.4 lib/rtanque/bot/radar.rb
colstrom-rtanque-0.1.3 lib/rtanque/bot/radar.rb
rtanque-0.1.2 lib/rtanque/bot/radar.rb
rtanque-0.1.1 lib/rtanque/bot/radar.rb
rtanque-0.1.0 lib/rtanque/bot/radar.rb
rtanque-0.0.2 lib/rtanque/bot/radar.rb