bullet.rb 692 Bytes
class Bullet < Movable

  def initialize game, opts = {}
    opts[:rot] = opts[:dir] == :down ? 180.0 : 0.0
    opts.merge!(m: 10.0, ctype: :bullet)
    super(game, opts)
    p = opts[:dir] == :down ? pos.add(@shape.r) : pos.sub(@shape.r)
    @body.p = CP::Vec2.new(*p)
  end

  def draw screen
    return hide if pos[0] < (0 - size[0]) || pos[1] < (0 - size[1]) ||
      pos[0] > @game.screen.size[0] || pos[1] > @game.screen.size[1]
    super screen
  end

  def hit ship
    p = pos; @options[:dir] == :up ? p[1] -= @shape.r : p[1] += @shape.r
    Explosion.new(@game, p: p).show
    ship.take_damage(options[:damage])
    @options[:shooter].hit(ship, options[:damage])
    hide
  end
end