
class Spriteset_MapEffectEx
  #--------------------------------------------------------------------------
  # ● 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :type                     # 天候タイプ
  attr_accessor :ox                       # 原点 X 座標
  attr_accessor :oy                       # 原点 Y 座標
  attr_reader   :power                    # 強さ
  attr_accessor :map_width
  attr_accessor :map_height
  
  attr_accessor :count
  PI = Math::PI
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(viewport = nil)
    @viewport = viewport
    init_members

    @count = 0
    @centerx = 0
    @centery = 0
    create_hotaru_bitmap
    create_stardust_bitmap
  end
  #--------------------------------------------------------------------------
  # ● メンバ変数の初期化
  #--------------------------------------------------------------------------
  def init_members
    @type = :none
    @ox = 0
    @oy = 0
    @power = 0
    @sprites = []
  end
  #--------------------------------------------------------------------------
  # ● 解放
  #--------------------------------------------------------------------------
  def dispose
    @sprites.each {|sprite| sprite.dispose }
    @hotaru_bitmap.dispose
    @stardust_bitmap.dispose
  end

  
  #--------------------------------------------------------------------------
  # ● 天候［幻想］のビットマップを作成
  #--------------------------------------------------------------------------
  def create_hotaru_bitmap

    @hotaru_bitmap = Cache.hotaru
  end   
  
  #--------------------------------------------------------------------------
  # ● 天候［幻想］のビットマップを作成
  #--------------------------------------------------------------------------
  def create_hotaru_bitmap
    
    @hotaru_bitmap = Cache.hotaru
  end   
     
  #--------------------------------------------------------------------------
  # ● 天候［星屑］のビットマップを作成
  #--------------------------------------------------------------------------
  def create_stardust_bitmap
    @stardust_bitmap = Bitmap.new(4, 4)
    color = Color.new(200, 255, 255, 255)
    @stardust_bitmap.fill_rect(0, 1, 4, 2, color)
    @stardust_bitmap.fill_rect(1, 0, 2, 4, color)

  end   
  
  #--------------------------------------------------------------------------
  # ● 天候の強さを設定
  #--------------------------------------------------------------------------
  def power=(power)
    @power = power
    (sprite_max - @sprites.size).times { add_sprite }
    (@sprites.size - sprite_max).times { remove_sprite }
  end
  

  
  #--------------------------------------------------------------------------
  # ● スプライトの最大数を取得
  #--------------------------------------------------------------------------
  def sprite_max
    (@power * 10).to_i
  end
  #--------------------------------------------------------------------------
  # ● スプライトの追加
  #--------------------------------------------------------------------------
  def add_sprite
    sprite = Sprite_ParticleSp.new(@viewport,0)
    sprite.opacity = 0
    @sprites.push(sprite)
  end
  #--------------------------------------------------------------------------
  # ● スプライトの削除
  #--------------------------------------------------------------------------
  def remove_sprite
    sprite = @sprites.pop
    sprite.dispose if sprite
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    update_screen
    @sprites.each {|sprite| update_sprite(sprite) }
    @count = (count + 1) % 1000
  end
  #--------------------------------------------------------------------------
  # ● 画面の更新
  #--------------------------------------------------------------------------
  def update_screen
#~     @viewport.tone.set(-dimness, -dimness, -dimness)
  end
  #--------------------------------------------------------------------------
  # ● 暗さの取得
  #--------------------------------------------------------------------------
  def dimness
    (@power * 6).to_i
  end
  #--------------------------------------------------------------------------
  # ● スプライトの更新
  #--------------------------------------------------------------------------
  def update_sprite(sprite)
    sprite.ox = @ox
    sprite.oy = @oy
    sprite.update
    case @type

    when :hotaru
      update_sprite_hotaru(sprite)    
    when :hotaru2
      update_sprite_hotaru2(sprite)   
    when :stardust  
      update_sprite_stardust(sprite) 
    else
      update_sprite_else(sprite)
    end
    create_new_particle(sprite) if sprite.dead 
    
  end

  # ● 追加用
  def update_sprite_else(sprite)
    
  end  

  def inside_screen?(sprite,marginx,marginy)
    -marginx < sprite.x && sprite.x < @map_width + marginx && -marginy < sprite.y && sprite.y < @map_height + marginy 
    
  end  
  #--------------------------------------------------------------------------
  # ● スプライトの更新［幻想］
  #--------------------------------------------------------------------------
  def update_sprite_hotaru(sprite)
    sprite.bitmap = @hotaru_bitmap
      dx = sprite.x - sprite.basex
      dy = sprite.y - sprite.basey
      
      cx = (rand(9) - 3) * - (dx + rand(20) - 10) 
      cy = (rand(9) - 3) * - (dy + rand(20) - 10)
      sprite.vx += cx * 0.0002
      sprite.vy += cy * 0.0002

#~       sprite.opacity = (Math.sin(PI * sprite.count / 24) * 128).floor
  end    

  #--------------------------------------------------------------------------
  # ● スプライトの更新［蛍］
  #--------------------------------------------------------------------------
  def update_sprite_hotaru2(sprite)
    sprite.bitmap = @hotaru_bitmap
    tx = @centerx - sprite.x
    ty = @centery - sprite.y

    ntv = VectorCalc.normalize([tx,ty])
    if (sprite.x - sprite.basex).abs < 2 && (sprite.y - sprite.basey).abs < 2
      sprite.basex += (rand(320 + 32 * ntv[0].floor ) - 160 ) 
      sprite.basey += (rand(320 + 32 * ntv[1].floor) - 160 )
      sprite.vx = 0
      sprite.vy = 0   
      sprite.phase = 1
    else
      dx = sprite.x - sprite.basex
      dy = sprite.y - sprite.basey
      nv = VectorCalc.normalize([dx,dy])
      cx = -nv[0]
      cy = -nv[1]
      if sprite.phase ==  0
        
        sprite.ax = (rand - 0.5) * 0.012
        sprite.ay = (rand - 0.5) * 0.012
        sprite.vx = (cx) * 0.8 
        sprite.vy = (cy) * 0.8
      elsif sprite.count % 100 == sprite.basex % 100
        sprite.phase = 0
        
        
      end

      
    end  
    hotaru_opacity(sprite)

  end      
  
  def hotaru_opacity(sprite)
    a = Math::PI * sprite.count / 120 
    sprite.opacity = 128 * (Math.sin(a) + 1)    
  end  
 
  #--------------------------------------------------------------------------
  # ● スプライトの更新［星屑］
  #--------------------------------------------------------------------------
  def update_sprite_stardust(sprite)
    sprite.bitmap = @stardust_bitmap

    if sprite.y < stardust_rheight
      sprite.vx = -2
      sprite.vy = 2    
    else
      sprite.rx = rand(stardust_rwidth + stardust_rheight * -sprite.vx / sprite.vy) - stardust_marginx
      sprite.ry = -stardust_marginy - rand(48)

    end
#~       sprite.opacity = (Math.sin(PI * sprite.count / 24) * 128).floor
  end     

  def stardust_marginx
    10
  end  
 
  def stardust_marginy
    10
  end   
  
  def stardust_rwidth
    @map_width + stardust_marginx * 2
  end  
 
  def stardust_rheight
    @map_height + stardust_marginy * 2
  end     
  #--------------------------------------------------------------------------
  # ● 新しい粒子の作成
  #--------------------------------------------------------------------------
  def create_new_particle(sprite)
    case @type
    when :hotaru
      create_new_particle_hotaru(sprite)  
    when :hotaru2
      create_new_particle_hotaru2(sprite) 
    when :stardust  
      create_new_particle_stardust(sprite) 
    end    

  end
  
  def create_new_particle_hotaru(sprite) 
    sprite.x = rand(@map_width) + @ox
    sprite.y = rand(@map_height) + @oy
    sprite.rx = sprite.x
    sprite.ry = sprite.y    
    
    sprite.basex = sprite.x + rand(160) - 80
    sprite.basey = sprite.y + rand(160) - 80
    sprite.opacity = 160 + rand(96)
    
    
    sprite.life = 240    
  end  
  
  def create_new_particle_hotaru2(sprite) 
    sprite.x = rand(@map_width + 100) - 50 + @ox
    sprite.y = rand(@map_height + 100) - 50 + @oy
    sprite.rx = sprite.x
    sprite.ry = sprite.y
    @centerx = @map_width / 2
    @centery = @map_height / 2
    sprite.basex = sprite.x + rand(200) - 100
    sprite.basey = sprite.y + rand(200) - 100
    sprite.count = rand(300)
    hotaru_opacity(sprite)
    sprite.life = 240    
  end    
  
  def create_new_particle_stardust(sprite) 
    sprite.vx = -2
    sprite.vy = 2        
    
    sprite.x = rand(stardust_rwidth + stardust_rheight * -sprite.vx / sprite.vy)  
    sprite.y = rand(@map_height) 
    sprite.rx = sprite.x
    sprite.ry = sprite.y
    sprite.count = rand(300)
    sprite.opacity = 255
    sprite.life = 240    
  end      
  
end
