#==============================================================================
# ** Sprite_Character
#------------------------------------------------------------------------------
#  This sprite is used to display the character.It observes the Game_Character
#  class and automatically changes sprite conditions.
#==============================================================================

class Sprite_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :character                # character
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     viewport       : viewport
  #     light_viewport : light viewport
  #     character      : character (Game_Character)
  #--------------------------------------------------------------------------
  def initialize(viewport, light_viewport, character = nil)
    @viewport = viewport
    @light_viewport = light_viewport
    @sprite = RPG::Sprite.new(viewport)
    @light_sprite = RPG::Sprite.new(light_viewport)
    @light_sprite.blend_type = 1
    @text_sprite = Sprite.new(viewport)
    @text_sprite.bitmap = Bitmap.new(Graphics.width, 24)
    @text_sprite.bitmap.font.size = 12
    @character = character
    update
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Choose the appropriate light sprite
    @light_sprite.viewport = @character.light_ignore_transition ? @viewport : (($game_screen.tone.blank?) ? @viewport : @light_viewport)
    @light_sprite.shader = @sprite.shader =

    #idk what is this, maybe later i remake this
    @character.character_name.start_with?("en_") || (Settings[:twm_shader] && @character.character_name.start_with?("niko") && ($game_switches[160] or Settings[:true_memory_mode])) ? Shader::WorldMachine :
    @character.shader || Shader::Sprite
    @light_sprite.mirror_x = @sprite.mirror_x = @character.mirror_x
    @light_sprite.mirror_y = @sprite.mirror_y = @character.mirror_y
    
    dir = (@character.direction - 2) / 2
    if Settings[:debug_text] && (@last_char_name != character.character_name || dir != @old_dir) 
      @text_sprite.bitmap.clear
      @text_sprite.bitmap.draw_text(0, 0, 256, 12, "#{dir == 0 ? "↓" : dir == 1 ? "←" : dir == 2 ? "→" : "↑"} #{@character.character_name}")
      @last_char_name = @character.character_name
      @old_dir = dir
    elsif !Settings[:debug_text] && @last_char_name != "мямямя :3"
      @text_sprite.bitmap.clear
      @last_char_name = "мямямя :3"
    end
    if Settings[:debug_text]
      @text_sprite.x = @character.screen_x
      @text_sprite.y = @character.screen_y
      @text_sprite.z = 100
    end
    # Update sprites
    @sprite.update
    @light_sprite.update
    # If tile ID, file name, or hue are different from current ones
    if @tile_id != @character.tile_id or
       @character_name != @character.character_name or
       @character_hue != @character.character_hue
      # Remember tile ID, file name, and hue
      @tile_id = @character.tile_id
      @character_name = @character.character_name
      @character_hue = @character.character_hue
      # If tile ID value is valid
      if @tile_id >= 384
        @sprite.bitmap = RPG::Cache.tile($game_map.tileset_name, @tile_id, @character.character_hue)
        @light_sprite.visible = false
        @sprite.src_rect.set(0, 0, 32, 32)
        @light_sprite.src_rect.set(0, 0, 32, 32)
        self.ox = 16
        self.oy = 32
      # If tile ID value is invalid
      else
        name = @character.character_name == "" && Settings[:debug_character] ? "debug" : @character.character_name
        translation_name = "#{$persistent.langcode}/#{name}"
        if File.exist?("Graphics/Characters/#{translation_name}.png")
          @sprite.bitmap = RPG::Cache.character(translation_name,
          @character.character_hue)
        else
          @sprite.bitmap = RPG::Cache.character(name,
          @character.character_hue)
        end
	  
        begin
          @light_sprite.bitmap = RPG::Cache.lightmap(@character.character_name)
          @light_sprite.visible = true
        rescue
          @light_sprite.bitmap = nil
          @light_sprite.visible = false
        end
        @cw = @sprite.bitmap.width / 4
        @ch = @sprite.bitmap.height / 4
        self.ox = @cw / 2
        self.oy = @ch
      end
    end
    # Set visible situation
    self.visible = (not @character.transparent)
    # If graphic is character
    if @tile_id == 0
      # Set rectangular transfer
      sx = @character.pattern * @cw
      sy = dir * @ch
      @sprite.src_rect.set(sx, sy, @cw, @ch)
      @light_sprite.src_rect.set(sx, sy, @cw, @ch)
    end
    # Set sprite coordinates
    self.x = @character.screen_x
    self.y = @character.screen_y
    self.z = @character.screen_z(@ch)
    # Set opacity level, blend method, and bush depth
    self.opacity = @character.opacity
    self.blend_type = @character.blend_type
    self.bush_depth = @character.bush_depth
    # Animation
    if @character.animation_id != 0
      animation = $data_animations[@character.animation_id]
      animation(animation, true)
      @character.animation_id = 0
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update Fast (only position)
  #--------------------------------------------------------------------------
  def update_fast
    # Set sprite coordinates
    self.x = @character.screen_x
    self.y = @character.screen_y
    self.z = @character.screen_z(@ch)
  end
  #--------------------------------------------------------------------------
  # * Sprite attribute wrapper
  #--------------------------------------------------------------------------
  def self.sprite_attr(*args)
    args.each do |arg|
      class_eval("def #{arg};@sprite.#{arg};end")
      class_eval("def #{arg}=(val);@sprite.#{arg}=val;@light_sprite.#{arg}=val;end")
    end
  end
  #--------------------------------------------------------------------------
  # * RPG::Sprite methods
  #--------------------------------------------------------------------------
  def whiten
    @sprite.whiten
    @light_sprite.whiten
  end
  def appear
    @sprite.appear
    @light_sprite.appear
  end
  def escape
    @sprite.escape
    @light_sprite.escape
  end
  def collapse
    @sprite.collapse
    @light_sprite.collapse
  end
  def damage(value, critical)
    @sprite.damage(value, critical)
  end
  def animation(animation, hit)
    @sprite.animation(animation, hit)
  end
  def loop_animation(animation, hit)
    @sprite.loop_animation(animation)
  end
  def blink_on
    @sprite.blink_on
    @light_sprite.blink_on
  end
  def blink_off
    @sprite.blink_off
    @light_sprite.blink_off
  end
  def blink?
    @sprite.blink?
  end
  def effect?
    @sprite.effect?
  end
  #--------------------------------------------------------------------------
  # * Sprite methods
  #--------------------------------------------------------------------------
  def dispose
    @sprite.dispose
    @light_sprite.dispose
    @sprite = nil
    @light_sprite = nil
  end
  def disposed?
    @sprite == nil
  end
  def flash(color, duration)
    @sprite.flash(color, duration)
    @light_sprite.flash(color, duration)
  end
  #--------------------------------------------------------------------------
  # * Sprite properties
  #--------------------------------------------------------------------------
  sprite_attr :src_rect
  sprite_attr :visible
  sprite_attr :x, :y, :z
  sprite_attr :ox, :oy
  sprite_attr :zoom_x, :zoom_y
  sprite_attr :angle
  sprite_attr :bush_depth
  sprite_attr :opacity
  sprite_attr :color
  sprite_attr :tone
  def blend_type
    @sprite.blend_type
  end
  def blend_type=(val)
    @sprite.blend_type = val
  end
end
