ship.rb 1.53 KB
class Ship < Movable

  SHIPS = {
    glider: { power: 50, shield: 20, speed: 2.5, weapons: [:laser],
      image: { image: "ship.png", zoom: 0.5 }},
    drone: { power: 75, shield: 50, speed: 2, weapons: [:missile],
      image: { image: "ship.png", zoom: 0.7 }},
    fighter: { power: 100, shield: 100, speed: 1, weapons: [
        :laser,
        :torpedo,
        :missile
      ],
      image: { image: "ship.png", zoom: 1.0 }},
  }

  attr_accessor :power, :image, :type, :debug
  def initialize game, options
    @params = SHIPS[options[:type]]
    options.merge!(m: 1000.0, ctype: :ship, v_limit: 3.0)
    image = @params[:image]
    image[:rot] = is_a?(Enemy) ? 0.0 : 180.0
    super game, options.merge(image)
    @max_power = @params[:power]
    @power = @max_power
    @weapons = @params[:weapons].map {|w| Weapon.new(self, w) }
  end

  def fire
    @weapons.each(&:fire)
  end

  def take_damage damage
    @power -= damage
    if @power <= 0
      @power = 1
      destroy
    end
    @debug = nil
  end

  def size
    [@surface.size[0] - 10, @surface.size[1] - 10]
  end

  def hit other, damage
    @power += damage / 5
    @power = @max_power  if @power > @max_power
  end

  def destroy
    @game.enemies.delete(self)
    Explosion.new(@game, p: pos.add(size.div(2)), image: "explosion.png",
      zoom: options[:zoom] * 1.5, volume: options[:zoom]) do
      hide
    end.show
  end

  def debug
    text = "[ #{@type}: #{@power}, v: #{@body.v}, m: #{@body.m} ]"
    @debug = @game.font("FreeSans").render(text, false, :white).to_display
  end

end