weapon.rb 902 Bytes
class Weapon

  WEAPONS = {
    laser: { damage: 10, speed: 15, freq: 500,
      image: { image: "bullet.png", zoom: 1.0 }},
    missile: { damage: 20, speed: 5, freq: 300,
      image: { image: "bullet.png", zoom: 2.0 }},
    torpedo: { damage: 30, speed: 1, freq: 100,
        image: { image: "bullet.png", zoom: 3.0 }},
  }

  attr_reader :ship, :game, :type, :params
  def initialize ship, type
    @ship, @game, @type, @params = ship, ship.game, type, WEAPONS[type]
    @lock = 0
    @last_shot = 0
  end

  def fire
    if game.steps - @last_shot >= (1000 - params[:freq])
      calc = @ship.is_a?(Player) ? :sub : :add
      v = @ship.body.v.to_a.send(calc, [0.0, params[:speed] / 10.0])
      opts = params[:image].merge(p: @ship.pos,
        v: v, shooter: @ship, damage: params[:damage], m: params[:damage])
      Bullet.new(game, opts).show
      @last_shot = game.steps
    end
  end


end