# さるたま管理
SarutamaCollision = []

module SarutamaManager

  # 初期化
  def SarutamaManager.initAll
    # 全部のさるたま配列
    @objects = []
    SarutamaCollision.clear
  end

  # 全部のさるたまを移動
  def SarutamaManager.moveAll
    # 移動＆消えたときは配列から削除
    @objects.hs_each(:move)
  end

  # 全部のさるたまを描画
  def SarutamaManager.drawAll
    # 描画
    @objects.hs_each(:draw)
  end

  # オブジェクト配列を返す
  def SarutamaManager.objects
    @objects
  end

  # フラグがたっているオブジェクトを削除する
  def SarutamaManager.clear
    @objects.hs_delete_if(:delflag)
  end
end


# さるのたま
class Sarutama1

  def Sarutama1.create(*arr)
    SarutamaManager.objects.push(Sarutama1.new(*arr))
  end

  def initialize(x, y)
    @x = x + 20
    @y = y
    @speed = 10
    @rot = 0
    @delflag = false
    @damage = 10
    @collision = CollisionBox.new(self, IMAGE_sarutama.x1, IMAGE_sarutama.y1,
                                        IMAGE_sarutama.x2, IMAGE_sarutama.y2)
  end

  def move
    # 移動
    @x = @x + @speed

    # 画像回転
    @rot = @rot + 10
    if @rot > 359 then
      @rot = @rot - 360
    end

    # 画面外判定
    if @y <= -IMAGE_sarutama.height or @x <= -IMAGE_sarutama.width or
       @y > 480 or @x > 640 then
      @delflag = true
      return
    end

    # あたり判定範囲設定
    @collision.set(@x, @y)
    SarutamaCollision.push(@collision)
    return
  end

  def shot(opponent)
    OtherSarutama.create(@x, @y, 40)
    @collision.delete
    @delflag = true
  end

  def draw
    # さるたまを描画する
    Window.drawRot(@x, @y, IMAGE_sarutama, @rot, nil, nil, 6)
  end

attr_accessor :delflag, :damage

end


# こざるのたま
class Sarutama2

  def Sarutama2.create(*arr)
    SarutamaManager.objects.push(Sarutama2.new(*arr))
  end

  def initialize(x, y, angle)
    @x = x + 20
    @y = y
    @rot = 0
    @delflag = false
    @damage = 4
    @collision = CollisionBox.new(self, IMAGE_sarutama2.x1, IMAGE_sarutama2.y1,
                                        IMAGE_sarutama2.x2, IMAGE_sarutama2.y2)
    @s_left = -IMAGE_sarutama2.width
    @s_top = -IMAGE_sarutama2.height
    angle = angle * TO_RADIAN
    @dx = Math.cos(angle) * 10
    @dy = Math.sin(angle) * 10
    @option = { :angle=>@rot, :scalex=>0.5, :scaley=>0.5, :z=>6 }
  end

  def move
    # 移動
    @x = @x + @dx
    @y = @y + @dy

    # 画像回転
    @rot = @rot + 10
    if @rot > 359 then
      @rot = @rot - 360
    end

    # 画面外判定
    if @y <= @s_top or @x <= @s_left or
       @y >  480  or @x >  640  then
      @delflag = true
      return
    end

    # あたり判定範囲設定
    @collision.set(@x, @y)
    SarutamaCollision.push(@collision)
  end

  def shot(opponent)
    OtherSarutama.create(@x, @y, 20)
    @collision.delete
    @delflag = true
  end

  def draw
    # さるたまを描画する
    @option[:aangle] = @rot
    Window.drawEx(@x, @y, IMAGE_sarutama2,@option)
  end

attr_accessor :delflag, :damage

end


# 爆弾
class Sarutama3

  def Sarutama3.create(*arr)
    SarutamaManager.objects.push(Sarutama3.new(*arr))
  end

  def initialize(x, y, dx)
    @x = x + 10
    @y = y + 10
    @dx = (dx + 1)*2+1
    @dy = 0
    @delflag = false
    @damage = 20
    @anime = 0
    @animewait = 5
    @animecount = @animewait
    @collision = CollisionBox.new(self, IMAGE_bomb[0].x1, IMAGE_bomb[0].y1,
                                        IMAGE_bomb[0].x2, IMAGE_bomb[0].y2)
  end

  def move
    # 移動
    @x = @x + @dx
    @y = @y + @dy
    @dy = @dy + 0.1

    # 画面外判定
    if @y <= -IMAGE_bomb[@anime].height or @x <= -IMAGE_bomb[@anime].width or
       @x > 640 then
      @delflag = true
      return
    end

    # 地面判定
    if @y > 480 - IMAGE_bomb[@anime].height then
      @delflag = true
      OtherBomber.create(@x - 12, @y - 15)
      return
    end

    # あたり判定範囲設定
    @collision.setRange(IMAGE_bomb[@anime].x1, IMAGE_bomb[@anime].y1,
                        IMAGE_bomb[@anime].x2, IMAGE_bomb[@anime].y2)
    @collision.set(@x, @y)
    SarutamaCollision.push(@collision)

    @animecount = @animecount - 1
    if @animecount == 0 then
      @animecount = @animewait
      @anime = 1 - @anime
    end

  end

  def shot(opponent)
    OtherBomber.create(@x - 12, @y)
    @collision.delete
    @delflag = true
  end

  def draw
    # 爆弾を描画する
    Window.drawScale(@x, @y, IMAGE_bomb[@anime], 1, 1, nil, nil, 6)
  end

attr_accessor :delflag, :damage

end







