# てき管理
TekiCollision = []

module TekiManager

  # 初期化
  def TekiManager.initAll
    # 全部のてき配列
    @objects = []
    TekiCollision.clear

    # 敵移動データ読み込み
    @movedata = {}

    moveflag = false
    movename = nil
    movetemp = nil

    Dir.foreach("tekidata") do |filename|
      next if filename == "." or filename == ".."

      open("tekidata/" + filename).each do |line|
        next if line.strip == "" or line[0,1] == "#"

        # まとめオブジェクト終了
        if line.strip == "}" then
          raise "てきデータが異常です" if moveflag == false
          moveflag = false
          @movedata[movename] = movetemp
          next
        end

        # まとめオブジェクト定義
        if line.strip[0,3] == "set" then
          raise "てきデータが異常です" if moveflag == true
          movename = line.strip.split(' ')[1]
          moveflag = true
          movetemp = []
          next
        end

        data = []
        line.split(',').each do |temp|
          if temp.index("=") == nil then
            data.push(temp.strip)
          else
            data.push(temp.strip.split("=")[1])
          end
        end

        for i in 1...data.size
          data[i] = data[i].to_i
        end

        movetemp.push(data)
      end
    end
  end

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

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

  def TekiManager.objects
    @objects
  end

  def TekiManager.data
    @movedata
  end

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


# てきクラス（ふわおばけ）
class TekiObake

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

  # てきオブジェクト初期化
  def initialize(x, y, name)
    @x = x
    @y = y
    @bx = x
    @by = y
    @dx = 0
    @speed = 1
    @delflag = false

    @f = 0
    @df = 5
    @wf = 10
    @hp = 10
    @hitcount = 0
    @animecount = 60
    @anime = 0
    @damage = 20

    @data = TekiManager.data[name]
    @datacount = 0
    @count = 0
    @collision = CollisionBox.new(self, IMAGE_obake[0].x1, IMAGE_obake[0].y1,
                                        IMAGE_obake[0].x2, IMAGE_obake[0].y2)

  end

  # てき移動
  def move

    loop do
      data = @data[@datacount]
      case data[0]
      when '初期設定'
        @hp = data[1]
        @datacount += 1
        next

      when '速度設定'
        @speed = data[1]
        @datacount += 1
        next

      when '振幅設定'
        @wf = data[1]
        @f = data[2]
        @df = data[3]
        @datacount += 1
        next

      when '移動'
        # 移動コマンド初回実行時はカウントを設定する
        if @count == 0 then
          @count = data[2]
        end

        @dx = Math.cos(TO_RADIAN * data[1]) * @speed
        @x = @x + @dx
        @by = @by + Math.sin(TO_RADIAN * data[1]) * @speed

        @y = @by + Math.sin(TO_RADIAN * @f) * @wf

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

        @count -= 1
        if @count == 0 then
          @datacount += 1
        end
        break

      when '狙い弾'
        if $saru != nil then
          angle = (Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180) + rand(20) - 10
          TekitamaS.create(@x + 10, @y + 10, angle, 3)
        end
        @datacount += 1
        next
      end
    end

    # 揺れる
    @f = @f + @df
    if @f > 359 then
      @f = @f - 360
    end

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

    # アニメーション
    @animecount = @animecount - @speed
    if @animecount < 0 then
      @animecount = 60
      @anime = 1 - @anime
    end

  end

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherObakeKO.create(@x, @y, @dx > 0 ? -1 : 1)
      $score = $score + 1
      SE_enemy_knockout.play
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 20
    end
  end

  # てき描画
  def draw
    # てきを描画する
    if @hitcount > 0 then
      # 被弾の絵
      Window.drawScale(@x, @y, IMAGE_obakehit, @dx > 0 ? -1 : 1, 1, nil, nil, 8)
      @hitcount = @hitcount - 1
    else
      # 通常の絵
      Window.drawScale(@x, @y, IMAGE_obake[@anime], @dx > 0 ? -1 : 1, 1, nil, nil, 8)
    end
  end

# クラス外に公開する変数
attr_accessor :delflag, :damage

end


# こうもり
class TekiBat

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

  def initialize(x, y)
    @x = x
    @bx = x
    @y = y
    @by = y
    @alpha = 0
    @delflag = false
    @hp = 10
    @damage = 15
    @hitcount = 0
    @speed = 6
    @collision = CollisionBox.new(self, IMAGE_bat[0].x1, IMAGE_bat[0].y1,
                                        IMAGE_bat[0].x2, IMAGE_bat[0].y2)
    @animecount = 10
    @anime = 0
    @state = 0
    @speed = 6
  end

  def move

    case @state
    when 0
      @bx = @bx - 0.5
      @x = @bx
      @alpha = @alpha + 4
      if @alpha > 255 then
        if $saru == nil then
          @state = 3
          return
        end
        @alpha = 255
        @state = 1
        @angle = (Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180)
        return
      end

    when 1
      # 移動
      @x = @x + Math.cos(TO_RADIAN * @angle) * @speed
      @y = @y + Math.sin(TO_RADIAN * @angle) * @speed

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

    when 3
      @alpha = @alpha - 4
      if @alpha < 0 then
        @delflag = true
        return
      end

    end

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

    # アニメーション
    @animecount = @animecount - 1
    if @animecount < 0 then
      @animecount = 10
      @anime = 1 - @anime
    end

  end

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherBatKO.create(@x, @y)
      $score = $score + 1
      SE_enemy_knockout.play
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 20
    end
  end

  def draw
    # てきを描画する
    if @hitcount > 0 then
      # 被弾の絵
      Window.draw(@x, @y, IMAGE_bathit, 8)
      @hitcount = @hitcount - 1
    else
      # 通常の絵
      Window.drawAlpha(@x, @y, IMAGE_bat[@anime], @alpha, 8)
    end
  end

attr_accessor :delflag, :damage

end


# 工場で作られたふわおばけ
class TekiObake2

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

  def initialize(x, y)
    @x = x
    @bx = x
    @y = y
    @by = y
    @f = 90
    @alpha = 0
    @delflag = false
    @hp = 20
    @damage = 20
    @hitcount = 0
    @collision = CollisionBox.new(self, IMAGE_obake[0].x1, IMAGE_obake[0].y1,
                                        IMAGE_obake[0].x2, IMAGE_obake[0].y2)
    @animecount = 60
    @anime = 0
    @state = 0
  end

  def move

    case @state
    when 0
      @bx = @bx - 0.5
      @x = @bx
      @alpha = @alpha + 4
      if @alpha > 255 then
        @alpha = 255
        @state = 1
        return
      end

    when 1
      # 移動
      @bx = @bx - 0.5
      @x = @bx + Math.cos(TO_RADIAN * @f) * 10
      @y = @y - 2

      # 左右に揺れる
      @f = @f + 5
      if @f > 359 then
        @f = @f - 360
      end

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

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

    # アニメーション
    @animecount = @animecount - 2
    if @animecount < 0 then
      @animecount = 60
      @anime = 1 - @anime
    end

  end

  def shot(opponent)
  end
  
  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherObakeKO.create(@x, @y, -1)
      $score = $score + 2
      SE_enemy_knockout.play
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 20
    end
  end

  def draw
    # てきを描画する
    if @hitcount > 0 then
      # 被弾の絵
      Window.draw(@x, @y, IMAGE_obakehit, 8)
      @hitcount = @hitcount - 1
    else
      # 通常の絵
      Window.drawAlpha(@x, @y, IMAGE_obake[@anime], @alpha, 8)
    end
  end

attr_accessor :delflag, :damage

end


# ジャック
class TekiJack

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

  def initialize(x, y, speed=6, angle=220, group=nil)
    @x = x
    @y = y
    angle = 160 if y < 240
    @dx = Math.cos(TO_RADIAN * angle) * speed
    @dy = Math.sin(TO_RADIAN * angle) * speed
    @count = 30
    @rot = 0
    if @dy > 0 then
      @drot = 3
    else
      @drot = -3
    end
    @delflag = false
    @hp = 10
    @damage = 20
    @collision = CollisionBox.new(self, IMAGE_jack1.x1, IMAGE_jack1.y1,
                                        IMAGE_jack1.x2, IMAGE_jack1.y2)
    @hitcount = 0
  end

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

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

    # 回転
    @rot = @rot + @drot
    if @rot > 359 then
      @rot = @rot - 360
    end
    if @rot < 0 then
      @rot = @rot + 360
    end

    if $saru != nil and @x < $saru.x + 100 and @count > 0 then
      @count = 0
    end

    if @count == 0 and $saru != nil then
      angle = (Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180) + rand(20) - 10
      TekitamaY.create(@x + 10, @y + 10, angle+15, 4.5)
      TekitamaY.create(@x + 10, @y + 10, angle-15, 4.5)
    end
    @count = @count - 1
    if @count < 0 then
      @dx = @dx + 0.1
      if @dy > 0 then
        @dy = @dy + 0.05
        @drot = @drot + 0.1
      else
        @dy = @dy - 0.05
        @drot = @drot - 0.1
      end
    end

    @hitcount = @hitcount - 1 if @hitcount > 0

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

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherJackKO.create(@x, @y)
      $score = $score + 2
      @collision.delete
      @delflag = true
      SE_enemy_knockout.play
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 10
    end
  end

  def draw
    # てきを描画する
    if @hitcount > 0 then
      Window.drawRot(@x, @y, IMAGE_jack2, @rot, nil, nil, 8)
    else
      Window.drawRot(@x, @y, IMAGE_jack1, @rot, nil, nil, 8)
    end
  end

attr_accessor :delflag, :damage

end

# ぼすおばけ
class TekiBoss

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

  def initialize(x, y)
    @bx = x
    @by = y
    @x = x
    @y = y
    @speed = 1
    @r = 0
    @angle = 0
    @delflag = false
    @hp = 3000
    @damage = 20
    @collision = CollisionBox.new(self, IMAGE_boss.x1, IMAGE_boss.y1,
                                        IMAGE_boss.x2, IMAGE_boss.y2)
    @alpha = 0
    @da = 1
    @dr = 1
    @state = 0
    @count = 0
  end

  def move
    case @state
    when 0 # 出現
      @alpha = @alpha + 1
      if @alpha > 255 then
        @alpha = 255
        @state = 1
      end
      return
    when 1 # 出現その２
      @alpha = @alpha - 0.5
      if @alpha < 130 then
        @alpha = 130
        @state = 2
      end
      # あたり判定範囲設定
      @collision.set(@x, @y)
      TekiCollision.push(@collision)
      return
    when 2 # 攻撃１
      if $saru == nil then
        @state = 5
        return
      end
      @count = @count + 1
      if @count % 7 == 0 then
        angle = rand(22.5)
        for i in 0..15
          TekitamaS.create(@x + 70, @y + 115, i * 22.5 + angle, 3)
        end
      end
      if @count > 400 then
        @state = 4
        @count = 0
      elsif @count % 100 == 0 then
        @state = 3
      end

    when 3 # 休憩
      @count = @count + 1
      if @count % 200 == 0 then
        @state = 2
      end

    when 4 # 攻撃２
      if $saru == nil then
        @state = 5
        return
      end
      if @count % 30 == 0 then
        angle = @count % 60 == 0 ? 11.75 : 0
        for i in 0..15
          TekitamaY.create(@x + 70, @y + 115, i * 22.5 + angle, 5)
        end
      end

      if @count % 12 == 0 then
        angle = (Math.atan2($saru.y - (@y+115), $saru.x - (@x+70)) / Math::PI * 180) + rand(20) - 10
        TekitamaU.create(@x + 70, @y + 115, angle, 5)
      end

      @count = @count + 1
      if @count == 300 then
        @state = 6
        @count = 0
      end

    when 5 # さる待ち
      if $saru != nil then
        @count = -200
        @state = 3
      end

    when 6 # コウモリアタック
      @count = @count + 1
      if @count % 3 == 0 then
        TekiBat.create(rand(250) + @x - 125 + 70, rand(250) + @y - 125 + 115)
      end
      
      if @count == 400 then
        @state = 3
        @count = -200
      end
    end

    # 移動
    @x = @bx + Math.cos(TO_RADIAN * @angle) * @r
    @y = @by + Math.sin(TO_RADIAN * @angle) * @r

    @r = @r + @dr
    if @r > 60 then
      @dr = -0.1
    elsif @r < 40 then
      @dr = 0.1
    end

    @angle = @angle - @speed
    if @angle > 359 then
      @angle = @angle - 360
    end

    @alpha = @alpha + @da
    if @alpha < 130 then
      @da = 1
    elsif @alpha > 180 then
      @da = -1
    end
    # あたり判定範囲設定
    @collision.set(@x, @y)
    TekiCollision.push(@collision)
  end

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      SE_enemy_knockout.play
      OtherObakeBoss.create(@x, @y)
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
    end
  end

  def draw
    Window.drawEx(@x, @y, IMAGE_boss, :alpha=>@alpha, :add=>true, :z=>9)
  end

attr_accessor :delflag, :damage

end


# 魔女
class TekiMajo

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

  def initialize(x, y)
    @x = x
    @y = y
    @delflag = false
    @hp = 600
    @damage = 20
    @dx = -6.8
    @dy = 4.0
    @angle = 180
    @shotcount = 4
    @animecount = 10
    @anime = 0
    @hitcount = 0
    @count = 0
    
    @state = 0

    @collision = CollisionBox.new(self, IMAGE_majo1.x1, IMAGE_majo1.y1,
                                        IMAGE_majo1.x2, IMAGE_majo1.y2)
  end

  def move
    
    case @state
    when 0 # 登場
      @dx = @dx + (@dx < 0 ? 0.1 : -0.1)
      @dy = @dy + (@dy < 0 ? 0.05 : -0.05)

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

      # 状態遷移
      if @dy.abs < 0.1 then
        @state = 1
        @count = 0
      end

    when 1 # 待機
      @count = @count - 1
      if @count < 0 then
        @state = 2
      end

    when 2 # 弾を撃つ（弾幕ぽいやつ）
      if $saru != nil then
        21.downto(10) do |i|
          tempx = Math.cos(TO_RADIAN * i * 30) * 80 + @x + 32
          tempy = Math.sin(TO_RADIAN * i * 30) * 80 + @y + 58
          angle = Math.atan2($saru.y - tempy, $saru.x - tempx) / Math::PI * 180
          TekitamaMajo.create(tempx, tempy, angle, 5, (21 - i) * 15, 64, self)
        end
      end
      @state = 3
      @count = 300

    when 3 # 待機
      @count = @count - 1
      if @count < 0 then
        @count = 160
        @state = 4
      end
        
    when 4 # 弾を撃つ（ソウルストライクぽいやつ）
      @count = @count - 1
      if $saru != nil and @count % 10 == 0 then
        tempx = @x + 32
        tempy = @y + 58
        if rand(2) == 0 then
          dy = rand(0)*1.4 + 0.4
        else
          dy = -(rand(0)*1.4 + 0.4)
        end
        TekitamaSS.create(tempx, tempy, dy, self)
      end
      
      if @count == 0 then
        @count = 100
        @state = 5
      end

    when 5 # 待機
      @count = @count - 1
      if @count < 0 then
        @state = 6
        @count = 240
      end
        
    when 6 # こうもりアタック
      @count = @count - 1
      if $saru != nil and @count % 12 == 0 then
        TekiBat.create(rand(150) + @x - 50, rand(150) + @y - 50)
      end
      
      if @count == 0 then
        @shotcount = @shotcount - 1
        if @shotcount > 0 then
          @state = 1
          @count = 100
        else
          @state = 7
        end
      end

    when 7 # にげる
      @dx = @dx + 0.1
      @dy = @dy + 0.05

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

    end
      
    if @state != 0 then
      @angle = @angle + 5
      if @angle > 360 then
        @angle = @angle - 360
      end
    end

    # アニメ処理
    @animecount = @animecount - 1
    if @animecount == 0 then
      @animecount = 10
      @anime = 1 - @anime
    end

    # 被弾絵のカウント
    @hitcount = @hitcount - 1 if @hitcount > 0

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

    # あたり判定範囲設定
    @collision.setRange(IMAGE_majo1.x1, IMAGE_majo1.y1 + Math.sin(TO_RADIAN * @angle) * 2,
                        IMAGE_majo1.x2, IMAGE_majo1.y2 + Math.sin(TO_RADIAN * @angle) * 2)
    @collision.set(@x, @y + Math.sin(TO_RADIAN * @angle) * 5)
    TekiCollision.push(@collision)

  end

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherMajoKO.create(@x, @y)
      $score = $score + 20
      SE_enemy_knockout.play
      ItemNote.create(@x, @y)
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 10
    end
  end

  def draw
    # てきを描画する
    # 通常の絵
    if @hitcount > 0 then
      Window.draw(@x, @y + Math.sin(TO_RADIAN * @angle) * 5, IMAGE_majo3, 8)
    else
      if @anime == 0 then
        Window.draw(@x, @y + Math.sin(TO_RADIAN * @angle) * 5, IMAGE_majo1, 8)
      else
        Window.draw(@x, @y + Math.sin(TO_RADIAN * @angle) * 5, IMAGE_majo2, 8)
      end
    end
  end

attr_accessor :delflag, :damage

end


# おばけ工場
class TekiHaka

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

  def initialize(x, y)
    @x = x
    @y = y
    @delflag = false
    @hp = 200
    @damage = 20
    @hitcount = 0
    @shotcount = 100
    @collision = CollisionBox.new(self, IMAGE_haka.x1, IMAGE_haka.y1,
                                        IMAGE_haka.x2, IMAGE_haka.y2)
  end

  def move
    # 移動
    @x = @x  - 0.5

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

    @hitcount = @hitcount - 1 if @hitcount > 0
    @shotcount = @shotcount - 1 if @shotcount > 0
    
    # おばけ発生
    if @shotcount == 0 then
      TekiObake2.create(@x + 40, @y + 50)
      @shotcount = 100
    end

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

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherHaka.create(@x, @y)
      $score = $score + 10
      SE_enemy_knockout.play
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 10
    end
  end

  def draw
    # てきを描画する
    if @hitcount > 0 then
      # 被弾の絵
      if @hp < 120 then
        Window.draw(@x, @y, IMAGE_hakadhit, 5.9)
      else
        Window.draw(@x, @y, IMAGE_hakahit, 5.9)
      end
    else
      # 通常の絵
      if @hp < 120 then
        Window.draw(@x, @y, IMAGE_hakad, 5.9)
      else
        Window.draw(@x, @y, IMAGE_haka, 5.9)
      end
    end
  end

attr_accessor :delflag, :damage

end



# うさぎ
class TekiUsagi

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

  def initialize(x, y)
    @x = x
    @y = y
    @delflag = false
    @hp = 150
    @damage = 20
    @hitcount = 0
    @collision = CollisionBox.new(self, IMAGE_usagi[0].x1, IMAGE_usagi[0].y1,
                                        IMAGE_usagi[0].x2, IMAGE_usagi[0].y2)
    @state = 0
    @count = rand(200)
    @speed = 2
    @dx = -@speed
    @animewait = 20
    @animecount = @animewait
    @anime = 0
  end

  def move
    # 移動
    @x = @x  - 0.5
    case @state
    when 0 # 移動中
      if @hitcount == 0 then
        @x = @x + @dx
        @count -= 1
        if @count == 0 then
          @state = 1
          @count = 100
        end
        @animecount -= 2
        if @animecount < 0 then
          @animecount = @animewait
          @anime = 1 - @anime
        end
      end

    when 1 # ぼー
      @count -= 1
      if @count == 0 or @x < 10 then
        if rand(2) == 0 then
          @state = 2
          @count = 180
        else
          @state = 0
          loop do 
            @count = rand(100) + 50
            if rand(2) == 0 then
              @dx = @speed
              next if @x + @count * (@dx - 0.5) > 640
            else
              @dx = -@speed
              next if @x + @count * (@dx - 0.5) < 200
            end
            break
          end
        end
      end
      @animecount -= 1
      if @animecount < 0 then
        @animecount = @animewait
        @anime = 1 - @anime
      end
    
    when 2 # 攻撃
      if $saru == nil then
        @state = 1
      else
        if @x - $saru.x > 0 then
          @dx = -1
        else
          @dx = 1
        end

        @count -= 1
        if @count % 35 == 0 then
          angle = (Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180) + rand(20) - 10
          TekitamaU.create(@x + 10, @y + 10, angle, 3)

          for i in 1..8
            temp = (Math.atan2($saru.y - @y, $saru.x - @x) / Math::PI * 180) + rand(120) - 60
            OtherUsaStar.create(@x + 10, @y + 10, temp, rand(2) + 1)
          end
        end

        if @count == 0 or @x < 10 then
          @state = 0
          loop do 
            @count = rand(100) + 50
            if rand(2) == 0 then
              @dx = @speed
              next if @x + @count * (@dx - 0.5) > 640
            else
              @dx = -@speed
              next if @x + @count * (@dx - 0.5) < 100
            end
            break
          end
        end
      end
    end

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

    @hitcount -= 1 if @hitcount > 0

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

  # さるに当たった
  def shot(opponent)
  end

  # たまにあたった
  def hit(opponent)
    @hp = @hp - opponent.damage

    if @hp <= 0 then
      OtherUsagiKO.create(@x, @y)
      $score = $score + 15
      SE_enemy_knockout.play
      @collision.delete
      @delflag = true
    else
      SE_enemy_damage.stop
      SE_enemy_damage.play
      @hitcount = 10 if @hitcount == 0
    end
  end

  def draw
    if @dx < 0 then
      direction = 1
    else
      direction = -1
    end

    if @hitcount > 0 then
      # 被弾の絵
      Window.drawScale(@x, @y - Math.sin(TO_RADIAN * @hitcount * 18) * 10, IMAGE_usagihit, direction, 1, nil, nil, 8)
    else
      case @state
      when 0
        # 通常の絵
        Window.drawScale(@x, @y, IMAGE_usagi[@anime], direction, 1, nil, nil, 8)
      when 1
        # ぼーっとしている絵
        Window.drawScale(@x, @y, IMAGE_usagistop[@anime], direction, 1, nil, nil, 8)
      when 2
        # 攻撃の絵
        Window.drawScale(@x, @y, IMAGE_usagiatk, direction, 1, nil, nil, 8)
      end
    end

  end

attr_accessor :delflag, :damage

end






