class Game_CharacterBase
  attr_accessor   :lamp_timer
  attr_accessor :lamp_type 
  attr_accessor :effect_lamp_flag 
  #--------------------------------------------------------------------------
  # ● 公開メンバ変数の初期化
  #--------------------------------------------------------------------------
  alias tako4583init_public_members init_public_members
  def init_public_members
    tako4583init_public_members
    @lamp_timer = 0
    @lamp_type = 1
    @effect_lamp_flag = false
  end  
end  

class Scene_Map < Scene_Base

  #--------------------------------------------------------------------------
  # ● 場所移動後の処理
  #--------------------------------------------------------------------------
  alias tako4583post_transfer post_transfer
  def post_transfer
 
    
    if $data_lamp_need_dungeon.include?($game_map.map_id)
      $game_player.effect_lamp_flag  = true
      
    else
      $game_player.effect_lamp_flag  = false
    end  
    @spriteset.update  
    tako4583post_transfer
  end  

end  

class Sprite_DarkLamp < Sprite
  attr_accessor :x0 
  attr_accessor :y0 
  attr_reader :r
  attr_accessor :color1
  attr_accessor :color2

  attr_accessor :timer_pt 
  attr_reader   :ptimer  
  attr_reader   :lamp_type  
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化 #lamp_type = 1でtimerあり,0でtimerはなし
  #--------------------------------------------------------------------------
  def initialize(viewport,character,lamp_type)
    super(viewport)
    @character = character
    @x0 = 0
    @y0 = 0
    @ptimer = 0
    @lamp_type　= lamp_type
    @timer_pt   = 20
    @reduce_degree = $data_lamp_reduce_degree
    @min_r = $data_lamp_min_r
#~     @max_r = get_radius($data_lamp_Cache_num )
    @around_lamp_sprites = []

#~     @character.lamp_timer = @timer_pt * ((@r - @min_r) / @reduce_degree )
    @circle_ratio = 1.0
    update    
    create_bitmap
    update_position
    create_around_bitmaps
    update_around_bitmap
    
  end 
  #--------------------------------------------------------------------------
  # ● スプライトの作成
  #--------------------------------------------------------------------------
  def create_bitmap
#~     bitmap = Bitmap.new(Graphics.width * 2  , Graphics.height * 2 ) 
#~     make_dark_lamp_bmp(bitmap, @x0 , @y0 , @r , @color1)
    self.bitmap = Cache.load_Cache_lamp_bitmap(0)
    self.zoom_x = @circle_ratio
    self.zoom_y = @circle_ratio   
    update_circleorigin
    self.z = 1
  end  

  
  #--------------------------------------------------------------------------
  # ● バフスプライトの作成
  #--------------------------------------------------------------------------
  def create_around_bitmap(index)
    color = Color.new(0,0,0) 
#~     return unless self.bitmap
#~     bmp = Cache.load_Cache_lamp_bitmap(0)
    @around_lamp_sprite =  Sprite_DarkLampAround.new(@viewport1,@character,self.bitmap.width,self.bitmap.height,color,index)
  end   
  
  def create_around_bitmaps
    @around_lamp_sprites = [0,1,2,3].collect do |i|    
      create_around_bitmap(i )
    end
  end

  #--------------------------------------------------------------------------
  # ● 解放
  #--------------------------------------------------------------------------
  def dispose
#~     bitmap.dispose if bitmap
    dispose_around_bitmap
    super
  end  
  def dispose_around_bitmap

    @around_lamp_sprites.each {|sprite| sprite.dispose }
  end 
  
  def update_around_bitmap
    @around_lamp_sprites.each do |sprite|
      sprite.update 
      sprite.visible = self.visible
    end
  end  
  
  def update_around_bitmap_psprite_info
    
    @around_lamp_sprites.each do |sprite| 
      sprite.update_psprite_info(self.bitmap.width,self.bitmap.height,@circle_ratio) 

    end  
  end  
  def update_around_bitmap_updatebmp
    @around_lamp_sprites.each do |sprite| 
      sprite.update_bmp

    end  
  end    
  def change_lamp_type(lamp_type)
    @lamp_type = lamp_type

 
  end 
  #--------------------------------------------------------------------------
  # ● 位置の更新
  #--------------------------------------------------------------------------
  def update_position
    update_circleorigin

    self.ox = @x0 - (@character.screen_x / @circle_ratio)
    self.oy = @y0 - ((@character.screen_y - 12 )/ @circle_ratio )

#~     self.z = @character.screen_z
  end  
  #--------------------------------------------------------------------------
  # ● 転送元ビットマップの更新
  #--------------------------------------------------------------------------
  def update_bitmap
    create_bitmap
  end   
  #--------------------------------------------------------------------------
  # ● 転送元ビットマップの更新
  #--------------------------------------------------------------------------
  def update_radius

    if increase_timer? || (@character.lamp_timer > 0 && check_timer) || (@ptimer > 0 && @character.lamp_timer == 0)
#~       change_radius

      update_circleratio  
      update_bitmap
      
#~       update_circleorigin 
#~       update_around_bitmap_psprite_info
#~       update_around_bitmap
#~       update_around_bitmap_updatebmp
    end 
    update_position
    
    
  end     
  
  def update_circleorigin
    return unless self.bitmap
    @x0 = self.bitmap.width / 2
    @y0 = self.bitmap.height / 2    
  end  

  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    super
#~     update_bitmap
  
    update_radius
    update_around    
    update_timer  
#~     update_position

#~     update_visibility
  end
  
  def update_around
    update_around_bitmap_psprite_info
    update_around_bitmap
    update_around_bitmap_updatebmp      
  end  
  
  def update_circleratio
    @circle_ratio = get_radius_ratio(rtimer)
  end  
  #--------------------------------------------------------------------------
  # ● 転送元ビットマップの更新
  #--------------------------------------------------------------------------
  def update_timer
    @ptimer = @character.lamp_timer
    return unless @lamp_type
    @character.lamp_timer -= 1 if @character.lamp_timer > 0
    
  end     
  def check_timer
    @character.lamp_timer % @timer_pt == 0
    
  end   
  
  def rtimer
   [@character.lamp_timer / @timer_pt , $data_lamp_max_num].min
  end  
  
  def get_radius_ratio(index)
    1.0 + @reduce_degree * index * 1.0 / @min_r 

  end
  #タイマーが増加したか
  def increase_timer?
    return false unless @ptimer
    
    @ptimer < @character.lamp_timer
  end  
    
end
class Sprite_DarkLampAround < Sprite
  attr_accessor :type 
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(viewport,character,c_width,c_height,color,type)
    super(viewport)
    @character = character
    @c_width = c_width
    @c_height = c_height
    @px = 0
    @py = 0
    @type = type
    @color = color
    @c_ratio = 1.0
    @edge_a = 3
      
    create_bitmap
    update
  end   
  #--------------------------------------------------------------------------
  # ● スプライトの作成
  #--------------------------------------------------------------------------
  def create_bitmap
    self.bitmap = set_bitmap
    self.bitmap.fill_rect(self.bitmap.rect , @color) 


  end    
  
  def set_bitmap # type 0で上 1で下,2で左 3de右
    if @type == 0 || @type == 1
      width = Graphics.width * 2
      height = Graphics.height
    else
      width = Graphics.width 
      height = Graphics.height * 2  
    end  
    Bitmap.new(width.floor , height.floor )
  end  
  def dispose
    bitmap.dispose if bitmap
    super
  end   
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update_psprite_info(c_width,c_height,c_ratio)
    @c_width = c_width
    @c_height = c_height  
    @c_ratio = c_ratio
  end  
  

  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    super
#~     update_bitmap
#~     update_timer
#~     update_radius
    
    update_position
#~     update_visibility
  end   
  #--------------------------------------------------------------------------
  # ● 位置の更新
  #--------------------------------------------------------------------------
  def update_position
    self.ox = 0
    self.oy = 0
    @px = (@character.screen_x )
    @py = (@character.screen_y - 12)
    tc_height = @c_ratio * @c_height
    tc_width = @c_ratio * @c_width
    tpx = @px
    tpy = @py
    tedge_a = @c_ratio * @edge_a
    width = bitmap.width
    height = bitmap.height
    case @type
    when 0
      self.x = @px - width / 2
      self.y = @py - tc_height / 2 - height + tedge_a
    when 1
      self.x = @px - width / 2
      self.y = @py + tc_height / 2 - tedge_a  
    when 2
      self.x = @px - tc_width / 2 - width + tedge_a
      self.y = @py - tc_height / 2         
    when 3
      self.x = @px + tc_width / 2 - tedge_a
      self.y = @py - tc_height / 2      
    end  
    

    self.z = 1
  end    
  
end  

class Spriteset_Map
  
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    create_viewports
    create_tilemap
    create_parallax
    create_characters
    create_shadow
    create_weather
    create_pictures
    create_timer
    create_dark_lump 
    update
  end 

  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    update_tileset
    update_tilemap
    update_parallax
    update_characters
    update_shadow
    update_weather
    update_pictures
    update_timer
    update_dark_lamp 

    update_viewports
  end  
  
  #--------------------------------------------------------------------------
  # ● ビューポートの作成
  #--------------------------------------------------------------------------
  alias takolampcreate_viewports create_viewports
  def create_viewports
    takolampcreate_viewports
    @viewport_t1 = Viewport.new
    @viewport_t1.z = 2
  end
  
  #--------------------------------------------------------------------------
  # ● ランプ更新
  #--------------------------------------------------------------------------
  def update_dark_lamp
    @dark_lamp_sprite.change_lamp_type($game_player.lamp_type) if $game_player.lamp_type != @dark_lamp_sprite.lamp_type
    @dark_lamp_sprite.visible = $game_player.effect_lamp_flag 
    @dark_lamp_sprite.update
    @dark_lamp_sprite.update_around_bitmap
  end  
  #--------------------------------------------------------------------------
  # ● 解放
  #--------------------------------------------------------------------------
  alias tako0ddddispose dispose
  def dispose
    tako0ddddispose
    dispose_dark_lamp 
  end  
  #--------------------------------------------------------------------------
  # ● スプライトの解放
  #--------------------------------------------------------------------------
  def dispose_dark_lamp
    @dark_lamp_sprite.dispose
  end    
  #--------------------------------------------------------------------------
  # ● スプライトの作成
  #--------------------------------------------------------------------------
  def create_dark_lump
    color1 = Color.new(0,0,0) 

    @dark_lamp_sprite = Sprite_DarkLamp.new(@viewport_t1, $game_player , $game_player.lamp_type)
#~     @darl_lamp_sprite_around1 = Sprite.new(@viewport1 ,)

  end  
  

end  
