# ================================================= =============================
# + + + MOG - Madoka Title Screen (v1.0) + + + / 人 ◕ ‿ ‿ ◕ 人 \
# ================================================= =============================
# By Moghunter
# [url]http://www.atelier-rgss.com/[/url]
# ================================================= =============================
# Animated title screen with the anime Puella Magi theme Madoka Magica.
# Of course you can put any character.
# ================================================= =============================
module MOG_MADOKA_TITLE_SCREEN
  #Posição do titulo.
  TITLE_POSITION = [40,270]
  #Velocidade de deslize da imagem de fundo.
  BACKGROUND_SCROLL_SPEED = [1,0]
  #Posição do circulo mágico
   CIRCLE_POSITION = [-30,-30]
  #Valor do zoom do circulo mágico. (Valores altos causam Lag) 
   CIRCLE_ZOOM_RANGE = 3
  #Definição do tipo de blend do circulo mágico.
   CIRCLE_BLEND_TYPE = 0
  #Imagens dos characteres que terão o efeito de Zoom.
  # CHARACTES_SPRITES_ZOOM_EFFECT = [0,2,5]
   CHARACTES_SPRITES_ZOOM_EFFECT = [0]
  #Prioridade dos personagens  
   CHARACTER_Z = 10
  # Ativar Partículas.
  PARTICLES = true
  # Numero de partículas.
  PARTICLE_NUMBER = 15
  # Ativar Cores Aleatórias.
  PARTICLE_RANDOM_COLOR = false
  # Definição do tipo de blend. (0,1,2)
  PARTICLE_BLEND_TYPE = 0
  #Definição do limite de velocidade das partículas.
  PARTICLE_MOVEMENT_RANGE_X = 0
  PARTICLE_MOVEMENT_RANGE_Y = 3
  PARTICLE_ANGLE_RANGE = 3  
  PARTICLE_Z = 25
  #Posição do comando
  COMMAND_POSITION = [20,50]
  #Prioridade do comando
  COMMNAND_Z = 100
end

#==============================================================================
# ■ Particle Title
#==============================================================================
class Particle_Title < Sprite
  
  include MOG_MADOKA_TITLE_SCREEN 
  
 #--------------------------------------------------------------------------
 # ● Initialize
 #--------------------------------------------------------------------------             
  def initialize(viewport = nil)
      super(viewport)
      self.bitmap = Cache.title1("Particle")
      self.tone.set(rand(255),rand(255), rand(255), 255) if PARTICLE_RANDOM_COLOR
      self.blend_type = PARTICLE_BLEND_TYPE
      self.z = PARTICLE_Z
      @cw = self.bitmap.width
      @ch = self.bitmap.height
      @nx = PARTICLE_MOVEMENT_RANGE_X
      @ny = PARTICLE_MOVEMENT_RANGE_Y
      reset_setting
  end  
  
 #--------------------------------------------------------------------------
 # ● Reset Setting
 #--------------------------------------------------------------------------               
  def reset_setting
      zoom = (50 + rand(100)) / 100.1
      self.zoom_x = zoom
      self.zoom_y = zoom
      self.x = (rand(576) -32)
      self.y = rand(448 + @ch) 
      self.opacity = 0
      self.angle = rand(360)
      nx2 = rand(@nx).abs
      nx2 = 1 if (@nx != 0 and nx2 < 1)      
      @speed_x = @nx > 0 ? nx2 : @nx < 0 ? -nx2 : 0        
      ny2 = rand(@ny).abs
      ny2 = 1 if (@ny != 0 and ny2 < 1)      
      @speed_y = @ny > 0 ? ny2 : @ny < 0 ? -ny2 : 0   
      @speed_a = [[rand(PARTICLE_ANGLE_RANGE), PARTICLE_ANGLE_RANGE].min, 0].max 
  end
  
 #--------------------------------------------------------------------------
 # ● Dispose
 #--------------------------------------------------------------------------               
  def dispose
      super
      self.bitmap.dispose
  end  
  
 #--------------------------------------------------------------------------
 # ● Update
 #--------------------------------------------------------------------------               
  def update
      super
      self.x += @speed_x
      self.y -= @speed_y
      self.angle += @speed_a      
      self.opacity += 5
      reset_setting if can_reset_setting?
  end  
  
 #--------------------------------------------------------------------------
 # ● Can Reset Setting
 #--------------------------------------------------------------------------                 
  def can_reset_setting?
      return true if (self.x < -64 or self.x > 592)    
      return true if (self.y < -164 or self.y > 464)
      return false
  end  
end


#==============================================================================
# ■ Scene Title
#==============================================================================
class Scene_Title < Scene_Base
 include MOG_MADOKA_TITLE_SCREEN 
  
 #--------------------------------------------------------------------------
 # ● Start
 #--------------------------------------------------------------------------          
 def start
     super
     SceneManager.clear
     play_title_music
     @phase = 0
     @skip_wait = 0
     create_srites
 end     
 
 #--------------------------------------------------------------------------
 # ● トランジション速度の取得
 #--------------------------------------------------------------------------
 def transition_speed
     return 20
 end
  
 #--------------------------------------------------------------------------
 # ● Terminate
 #--------------------------------------------------------------------------
 def terminate
     super
     execute_dispose
 end
    
 #--------------------------------------------------------------------------
 # ● Update
 #--------------------------------------------------------------------------             
 def update
     super
     execute_update
 end

 #--------------------------------------------------------------------------
 # ● Create Sprites
 #--------------------------------------------------------------------------            
 def create_srites
     execute_dispose
     create_background
     create_characters_sprites
     create_title_name
     create_magic_circle
     create_particles
     create_commands
 end
  
 #--------------------------------------------------------------------------
 # ● Create Commands
 #--------------------------------------------------------------------------           
 def create_commands
     @command_wait = false
     @command_index = 0
     @command_index = 1 if DataManager.save_file_exists?
     @commands = []
     index = 0
     for i in 0..2
         @commands[index] = Sprite.new
         if index == 1 and !DataManager.save_file_exists?
            @commands[index].bitmap = Cache.title1("Command" + index.to_s + "B")
         else  
            @commands[index].bitmap = Cache.title1("Command" + index.to_s)
         end    
         @commands[index].z = COMMNAND_Z
         @commands[index].ox = @commands[index].bitmap.width / 2
         @commands[index].oy = @commands[index].bitmap.height / 2
         @commands[index].x = COMMAND_POSITION[0] + @commands[index].ox
         @commands[index].y = COMMAND_POSITION[1] + @commands[index].oy
         @commands[index].opacity = 0
         index += 1
     end
 end 
 
 #--------------------------------------------------------------------------
 # ● Create Background
 #--------------------------------------------------------------------------             
 def create_background
     @background = Plane.new
     @background.bitmap = Cache.title1("Background")
     @background.z = 0
 end  
 
 #--------------------------------------------------------------------------
 # ● Create Title Name
 #--------------------------------------------------------------------------              
 def create_title_name
     @title_name_effect = [0,30]
     @title_name = Sprite.new
     @title_name.bitmap = Cache.title1("Title_Name")
     @title_name.z = 301
     @title_name.opacity = 255
     @title_name.blend_type = 0
     @title_name.ox = @title_name.bitmap.width / 2
     @title_name.oy = @title_name.bitmap.height / 2
     @title_name.x = TITLE_POSITION[0] + @title_name.ox 
     @title_name.y = TITLE_POSITION[1] + @title_name.oy 
 end
 
 #--------------------------------------------------------------------------
 # ● Create Magic Circle
 #--------------------------------------------------------------------------               
 def create_magic_circle
     @magic_circle = Sprite.new
     @magic_circle.bitmap = Cache.title1("Magic_Circle")
     @magic_circle.z = 100
     @magic_circle.ox = @magic_circle.bitmap.width / 2
     @magic_circle.oy =@magic_circle.bitmap.height / 2
     @magic_circle.x = @magic_circle.ox - CIRCLE_POSITION[0]
     @magic_circle.y = @magic_circle.oy - CIRCLE_POSITION[1]
     @magic_circle.z = 3
     @magic_circle.zoom_x = CIRCLE_ZOOM_RANGE
     @magic_circle.zoom_y = CIRCLE_ZOOM_RANGE
     @magic_circle.opacity = 0
     @magic_circle.blend_type = CIRCLE_BLEND_TYPE
 end  
 
 #--------------------------------------------------------------------------
 # ● Create Characters Sprites
 #--------------------------------------------------------------------------              
 def create_characters_sprites
     @magic_girls_appear_duration = 0
     @magic_girl_index = 0
     @magic_girls = []
     @magic_girls
     index = 0
     for i in 0..999
         @magic_girls[i] = Sprite.new
         @magic_girls[i].bitmap = Cache.title1("Character" + index.to_s) rescue nil
         if @magic_girls[i].bitmap == nil
            @magic_girls[i].dispose
            @magic_girls.delete(index)
            break
         end  
         @magic_girls[i].z = CHARACTER_Z + index
         @magic_girls[i].opacity = 0
         @magic_girls[i].ox = @magic_girls[i].bitmap.width / 2
         @magic_girls[i].oy = @magic_girls[i].bitmap.height / 2
         @magic_girls[i].x = @magic_girls[i].ox
         @magic_girls[i].y = @magic_girls[i].oy
         if CHARACTES_SPRITES_ZOOM_EFFECT.include?(index)
            @magic_girls[i].zoom_x = 1.5 
            @magic_girls[i].zoom_y = 1.5 
         end   
         index += 1
      end
      @magic_girls.pop
 end  
 
  #--------------------------------------------------------------------------
  # ● Create Particles
  #--------------------------------------------------------------------------  
  def create_particles
      return if !PARTICLES
      @viewport_light = Viewport.new(-32, -32, 600, 480)
      @viewport_light.z = PARTICLE_Z
      @particles_bitmap =[]
      for i in 0...PARTICLE_NUMBER
          @particles_bitmap.push(Particle_Title.new, @viewport_light)
      end  
  end 
    
 #--------------------------------------------------------------------------
 # ● Execute Dispose
 #--------------------------------------------------------------------------              
 def execute_dispose
     dispose_background
     dispose_title_name
     dispose_characters
     dispose_particles
     dispose_commands
     dispose_circle
 end  
 
 #--------------------------------------------------------------------------
 # ● Dispose Background
 #--------------------------------------------------------------------------              
 def dispose_background
     return if @background == nil
     @background.bitmap.dispose
     @background.dispose
     @background = nil
 end 

 #--------------------------------------------------------------------------
 # ● Dispose Tittle Name
 #--------------------------------------------------------------------------               
 def dispose_title_name
     return if  @title_name == nil
     @title_name.bitmap.dispose
     @title_name.dispose
     @title_name = nil
 end   
 
 #--------------------------------------------------------------------------
 # ● Dispose Characters
 #--------------------------------------------------------------------------               
 def dispose_characters
     return if @magic_girls == nil 
     for i in @magic_girls
         if i.bitmap != nil
            i.bitmap.dispose 
         end   
         i.dispose
     end  
     @magic_girls = nil         
 end   
 
 #--------------------------------------------------------------------------
 # ● Dispose Particles
 #--------------------------------------------------------------------------              
  def dispose_particles
      return if @particles_bitmap == nil
      @particles_bitmap.each {|sprite| sprite.dispose }
      @particles_bitmap = nil
      @viewport_light.dispose 
  end   
 
 #--------------------------------------------------------------------------
 # ● Dispose Command
 #--------------------------------------------------------------------------                
  def dispose_commands
      return if @commands == nil
      @commands.each {|sprite| sprite.dispose }
      @commands = nil
  end
  
 #--------------------------------------------------------------------------
 # ● Dispose Circle
 #--------------------------------------------------------------------------                
 def dispose_circle
     return if @magic_circle == nil
     @magic_circle.bitmap.dispose
     @magic_circle.dispose
     @magic_circle = nil 
 end  
  
 #--------------------------------------------------------------------------
 # ● Execute Update
 #--------------------------------------------------------------------------              
 def execute_update
     update_characters
     update_title_name
     update_magic_circle
     update_particles
     update_command
     update_background
 end
 
 #--------------------------------------------------------------------------
 # ● Create Background
 #--------------------------------------------------------------------------               
 def update_background
     return if @background == nil
     @background.ox += BACKGROUND_SCROLL_SPEED[0]
     @background.oy += BACKGROUND_SCROLL_SPEED[1]     
 end
   
 #--------------------------------------------------------------------------
 # ● Update Characters
 #--------------------------------------------------------------------------               
 def update_characters
     return if @magic_girls == nil
     index = 0 
     for i in @magic_girls
         update_magic_girls(i,index)
         index += 1
     end     
 end  
 
 #--------------------------------------------------------------------------
 # ● Update Magic Girls
 #--------------------------------------------------------------------------                
 def update_magic_girls(i,index)
     return if @magic_girl_index != index
     update_zoom_effect(i,index) if CHARACTES_SPRITES_ZOOM_EFFECT.include?(index)
     update_opactiy_effect(i,index) 
 end  
 
 #--------------------------------------------------------------------------
 # ● Update Opacity Effect
 #--------------------------------------------------------------------------                 
 def update_opactiy_effect(i,index) 
     i.opacity += 5
     return if i.opacity < 255
     i.zoom_x = 1.00
     i.zoom_y = 1.00         
     @magic_girl_index += 1
     if @magic_girl_index == @magic_girls.size
        @phase = 1
        clear_command_sprites
     end  
 end
   
 #--------------------------------------------------------------------------
 # ● Update Zoom Effect
 #--------------------------------------------------------------------------                 
 def update_zoom_effect(i,index)
     if i.zoom_x > 1.00
        i.zoom_x -= 0.01
        i.zoom_y -= 0.01
        if i.zoom_x < 1.00
           i.zoom_x = 1.00
           i.zoom_y = 1.00   
        end
     end    
 end   

 #--------------------------------------------------------------------------
 # ● Update Title Name
 #--------------------------------------------------------------------------                  
 def update_title_name
     return if @phase != 1
     return if @title_name == nil     
     @title_name.opacity += 5
     return if @title_name.opacity < 255
     @phase = 2
 end 
 
 #--------------------------------------------------------------------------
 # ● Update Magic Circle
 #--------------------------------------------------------------------------                   
 def update_magic_circle
     return if @magic_circle == nil
     @magic_circle.angle += 1
 end  

 #--------------------------------------------------------------------------
 # ● Update Particles
 #--------------------------------------------------------------------------              
 def update_particles
     return if @particles_bitmap == nil     
     @particles_bitmap.each {|sprite| sprite.update }
 end

 #--------------------------------------------------------------------------
 # ● Update Command
 #--------------------------------------------------------------------------               
 def update_command
     return if @commands == nil
     update_skip_all
     index = 0
     return if @phase != 2
     update_key
     @magic_circle.opacity += 5
     for i in @commands
         if @command_index == index
            update_command_select1(i,index)
         else
            update_command_select2(i,index)
         end  
         index += 1
     end      
 end  
 
 #--------------------------------------------------------------------------
 # ● Update Key
 #--------------------------------------------------------------------------                
 def update_key
     return if @skip_wait > 0
     update_select_command
     return if @command_wait
     if Input.press?(:RIGHT) or Input.press?(:DOWN)
        add_index(1)
     elsif Input.press?(:LEFT) or Input.press?(:UP)
        add_index(-1)
     end   
 end
   
 #--------------------------------------------------------------------------
 # ● Add Index
 #--------------------------------------------------------------------------                 
 def add_index(value)
     Sound.play_cursor
     index = @command_index
     index += value 
     index = 0 if index > 2
     index = 2 if index < 0
     @command_index = index
 end
 
 #--------------------------------------------------------------------------
 # ● Update Select Command
 #--------------------------------------------------------------------------                
 def update_select_command
     if Input.trigger?(:C)  
       case @command_index
           when 0               
             Sound.play_ok
             command_new_game
           when 1
             if DataManager.save_file_exists?
               command_continue
             else
               Sound.play_buzzer  
             end  
           when 2
             Sound.play_ok
             command_shutdown
           end
      end     
 end
        
 #--------------------------------------------------------------------------
 # ● Update Command Select 1
 #--------------------------------------------------------------------------                
 def update_command_select1(i,index)
     return if i.opacity == 255 and i.zoom_x == 1.00
     i.opacity += 7
     if i.zoom_x > 1.00
        i.zoom_x -= 0.01
        i.zoom_y -= 0.01
        @command_wait = true
        if i.zoom_x <= 1.00
           i.opacity = 255 
           @command_wait = false
        end   
     end  
 end  
 
 #--------------------------------------------------------------------------
 # ● Update Command Select 2
 #--------------------------------------------------------------------------                
 def update_command_select2(i,index)
     return if i.opacity == 0
     i.opacity -= 7
     if i.zoom_x < 1.50
        i.zoom_x += 0.01
        i.zoom_y += 0.01
        i.opacity = 0 if i.zoom_x >= 1.50
     end            
 end   
 
  #--------------------------------------------------------------------------
  # ● Command New Game
  #--------------------------------------------------------------------------
  def command_new_game
      DataManager.setup_new_game
      fadeout_all
      $game_map.autoplay
      SceneManager.goto(Scene_Map)
  end 
 
  #--------------------------------------------------------------------------
  # ● Command Continue
  #--------------------------------------------------------------------------  
  def command_continue
      Sound.play_ok
      SceneManager.call(Scene_Load)
  end  
  
  #--------------------------------------------------------------------------
  # ● Commad Shutdown
  #--------------------------------------------------------------------------
  def command_shutdown
      fadeout_all
      SceneManager.exit
  end     
 #--------------------------------------------------------------------------
 # ● Skip All
 #--------------------------------------------------------------------------                
 def update_skip_all
     @skip_wait -= 1 if @skip_wait > 0
     return if @phase > 1
     if Input.trigger?(:C) or Input.trigger?(:B)
        reset_main_sprites
        skip_sprite_effects 
        clear_main_sprites
     end
 end  
   
 #--------------------------------------------------------------------------
 # ● Skip Sprites Effects
 #--------------------------------------------------------------------------                 
 def skip_sprite_effects 
    loop do
       @title_name.opacity += 10 
       for i in @magic_girls
           i.opacity += 10
           if i.zoom_x > 1.00
              i.zoom_x -= 0.01
              i.zoom_y -= 0.01
           end   
       end
       break if @title_name.opacity >= 255
       Graphics.update
    end  
 end
       
 #--------------------------------------------------------------------------
 # ● Reset Main Sprites
 #--------------------------------------------------------------------------                 
 def reset_main_sprites
     @title_name.opacity= 0
     for i in @magic_girls
         i.zoom_x = 1.00
         i.zoom_y = 1.00
     end      
 end
 
 #--------------------------------------------------------------------------
 # ● Clear All Sprites
 #--------------------------------------------------------------------------                 
 def clear_main_sprites
     @title_name.opacity= 255
     for i in @magic_girls
         i.opacity = 255
         i.zoom_x = 1.00
         i.zoom_y = 1.00
     end  
     @magic_girl_index = @magic_girls.size + 1
     @phase = 2      
     clear_command_sprites 
     @skip_wait = 10
     @command_wait = false
 end  
      
 #--------------------------------------------------------------------------
 # ● Clear Command Sprites
 #--------------------------------------------------------------------------                  
 def clear_command_sprites     
     index = 0
     for i in @commands
         if @command_index == index
            i.zoom_x = 1.50
            i.zoom_y = 1.50
            i.opacity = 0
         else   
            i.zoom_x = 1.50
            i.zoom_y = 1.50
            i.opacity = 0      
         end
        index += 1
    end    
  end  
  
end

$mog_rgss3_madoka_title_screen = true