# てきたま管理
TekitamaCollision = []

module TekitamaManager

  # 初期化
  def TekitamaManager.initAll
    # 全部のてきたま配列
    @objects = []
    TekitamaCollision.clear
  end

  # 全部のてきたまを移動
  def TekitamaManager.moveAll
    # 移動
    @objects.hs_each(:move)
  end

  # 全部のてきたまを描画
  def TekitamaManager.drawAll
    # 描画
    @objects.hs_each(:draw)
  end

  def TekitamaManager.objects
    @objects
  end

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



# （直進）
class TekitamaS

  def self.create(*arr)
    TekitamaManager.objects.push(self.new(*arr))
  end

  def initialize(x, y, angle, speed)
    @x = x
    @y = y
    angle = TO_RADIAN * angle
    @dx = Math.cos(angle) * speed
    @dy = Math.sin(angle) * speed
    @delflag = false
    @damage = 10
    @collision = CollisionBox.new(self, IMAGE_tekitama1.x1, IMAGE_tekitama1.y1,
                                        IMAGE_tekitama1.x2, IMAGE_tekitama1.y2)
    @s_left = -IMAGE_tekitama1.width
    @s_top = -IMAGE_tekitama1.height
  end

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

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

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

  def shot(opponent)
    @delflag = true
  end

  def draw
    # てきたまを描画する
    Window.draw(@x, @y, IMAGE_tekitama1, 10)
  end

attr_accessor :delflag, :damage
end


# てきたま（魔女専用）クラス
class TekitamaMajo

  def self.create(*arr)
    TekitamaManager.objects.push(self.new(*arr))
  end

  def initialize(x, y, angle, speed, count, wait, obj)
    @x = x
    @y = y
    @dx = 0
    @dy = 0
    @speed = speed
    @angle = angle
    @count = count
    @obj = obj
    @delflag = false
    @damage = 20
    @alpha = 0
    @wait = wait
    @rot = 0
    @state = 0 # 0は出現、1は待ち状態、2は飛ぶ、3は消滅
    @collision = CollisionBox.new(self, IMAGE_tekitama3.x1, IMAGE_tekitama3.y1,
                                        IMAGE_tekitama3.x2, IMAGE_tekitama3.y2)
  end

  def move

    # 絵の回転
    @rot = @rot - 2
    if @rot < 0 then
      @rot = @rot + 360
    end

    # 移動
    case @state
    when 0 # 出現
      @wait = @wait - 1
      @alpha = @alpha + 4
      if @alpha > 255 then
        @alpha = 255
      end
      if @wait <= 0 then
        @state = 1
      end

      if $saru == nil or @obj.delflag == true then # 出現中にさるか魔女が死んだ場合
        @state = 3
      end

      return

    when 1 # 待ち
      if $saru == nil or @obj.delflag == true then # 待機中にさるか魔女が死んだ場合
        @state = 3
        return
      end

      @count = @count - 1
      if @count <= 0 then
        @state = 2
        @angle = Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180
        @dx = Math.cos(TO_RADIAN * @angle) * @speed
        @dy = Math.sin(TO_RADIAN * @angle) * @speed
      end

    when 2 # 飛ぶ
      @count = @count + 1
      if @count >= 4 then
        OtherKirakira.create(@x+2 - (Math.cos(TO_RADIAN * (@angle + rand(0) * 180 - 90)) * @speed/2),
                             @y+2 - (Math.sin(TO_RADIAN * (@angle + rand(0) * 180 - 90)) * @speed/2)) if $effectmode == 1
        @count = 0
      end

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

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

    when 3 # 消滅
      @alpha = @alpha - 8
      if @alpha < 0 then
        @delflag = true
      end

      return
    end

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

  def shot(opponent)
    @delflag = true
  end
  
  def draw
    Window.drawEx(@x, @y, IMAGE_tekitama3, :angle => @rot, :alpha => @alpha, :z => 10)
  end

attr_accessor :delflag, :damage
end


# ソウルストライクみたいなやつ（魔女専用）
class TekitamaSS

  def self.create(*arr)
    TekitamaManager.objects.push(self.new(*arr))
  end

  def initialize(x, y, dy, obj)
    @x = x
    @y = y
    @dx = 6
    @dy = dy
    @flag = @dy < 0 ? -1 : 1
    @rot = 0

    @obj = obj
    @delflag = false
    @damage = 20
    @state = 0
    @count = 0
    @collision = CollisionBox.new(self, IMAGE_tekitama3.x1, IMAGE_tekitama3.y1,
                                        IMAGE_tekitama3.x2, IMAGE_tekitama3.y2)
  end

  def move

    # 絵の回転
    @rot = @rot - 2
    if @rot < 0 then
      @rot = @rot + 360
    end

    @count = @count + 1
    if @count >= 4 then
      angle = Math.atan2(@ty - @y, @tx - @x) / Math::PI * 180
      speed = Math.sqrt(@dx * @dx + @dy * @dy)
      OtherKirakira.create(@x+2 - (Math.cos(TO_RADIAN * (angle + rand(0) * 180 - 90)) * speed/2),
                           @y+2 - (Math.sin(TO_RADIAN * (angle + rand(0) * 180 - 90)) * speed/2)) if $effectmode == 1
      @count = 0
    end

    @tx = @x
    @ty = @y

    # 移動
    case @state
    when 0

      @dx = @dx - 0.2
      @x = @x + @dx
      @y = @y + @dy
      if @y <= -IMAGE_tekitama3.height or @x <= -IMAGE_tekitama3.width or
         @y >  480  or @x >  640  then
        @delflag = true
        return
      end
      
      if @dx < 0 then
        @state = 1
      end

    when 1
      @dx = @dx - 0.05
      @x = @x + @dx
      if $saru != nil and @y < $saru.y then
        @dy = @dy + 0.02 if @flag == -1
      else
        @dy = @dy - 0.02 if @flag == 1
      end

      @y = @y + @dy
      if @y <= -IMAGE_tekitama3.height or @x <= -IMAGE_tekitama3.width or
         @y >  480  or @x >  640  then
        @delflag = true
        return
      end
      
      if @dx < 0 then
        @state = 1
      end

    end

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

  def shot(opponent)
    @delflag = true
  end
  
  def draw
    Window.drawRot(@x, @y, IMAGE_tekitama3, @rot, nil, nil, 10)
  end

attr_accessor :delflag, :damage
end


# うさ用（直進）
class TekitamaU

  def self.create(*arr)
    TekitamaManager.objects.push(self.new(*arr))
  end

  def initialize(x, y, angle, speed)
    @x = x
    @y = y
    @dx = Math.cos(TO_RADIAN * angle) * speed
    @dy = Math.sin(TO_RADIAN * angle) * speed
    @delflag = false
    @damage = 20
    @rot = 0
    @collision = CollisionBox.new(self, IMAGE_usatama.x1, IMAGE_usatama.y1,
                                        IMAGE_usatama.x2, IMAGE_usatama.y2)
  end

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

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

    @rot += 2
    if @rot > 360 then
      @rot -= 360
    end

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

  def shot(opponent)
#    @delflag = true
  end

  def draw
    # てきたまを描画する
    Window.drawRot(@x, @y, IMAGE_usatama, @rot, nil, nil, 10)
  end

attr_accessor :delflag, :damage, :x1, :x2, :y1, :y2
end

# 黄色い弾（直進）
class TekitamaY

  def self.create(*arr)
    TekitamaManager.objects.push(self.new(*arr))
  end

  def initialize(x, y, angle, speed)
    @x = x
    @y = y
    angle = TO_RADIAN * angle
    @dx = Math.cos(angle) * speed
    @dy = Math.sin(angle) * speed
    @delflag = false
    @damage = 10
    @collision = CollisionBox.new(self, IMAGE_tekitama2.x1, IMAGE_tekitama2.y1,
                                        IMAGE_tekitama2.x2, IMAGE_tekitama2.y2)
    @s_left = -IMAGE_tekitama2.width
    @s_top = -IMAGE_tekitama2.height
  end

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

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

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

  def shot(opponent)
    @delflag = true
  end

  def draw
    # てきたまを描画する
    Window.draw(@x, @y, IMAGE_tekitama2, 10)
  end

attr_accessor :delflag, :damage, :x1, :x2, :y1, :y2
end
