#==============================================================================
#  Discovery Title Screen
#  Version 0.8.3
#  Author: Rekx/TheRexion 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Instructions:
#    Place in scripts, copy over image files or create your own.
#    Customize until pleased.
#    ???
#    PROFI--Yeah, I'll just stop now...
#
# Credits to:
#   Johnny Mercy
#   HungrySnake for helping me do what I could not and optimizing
#   the script for easier customization with the graphics.
#==============================================================================
module Rekx
  module CustomTitle
    
    # Falling objects on or off
    FALLING = false
    
    # Graphics arrays. Format: ["Selected","Unselected"(,"Disabled (only continue)")]
    NEW_GAME_GRAPHICS = ["new_game","new_game_off"]
    CONTINUE_GRAPHICS = ["continue","continue_off","continue_disabled"]
    SHUTDOWN_GRAPHICS = ["exit","exit_off"]
    
    # Three options side by side or three on top of each other
    STYLE = 2 # 1 or 2
    # 1 = Left Aligned, 2 = Centered, 3 = Right Aligned.  Only applies for Style 2
    PLACEMENT = 3
    
    # Would you like the title image to be wavy?
    WAVY = true
    # if WAVY is true, you can customize how it will wave
    WAVE_AMP = 20
    WAVE_LENGTH = 240
    WAVE_SPEED = 160
    
    # Fade out other commands when a certain command is selected
    FADE_OUT_COMMANDS = false
    
    # How quickly things fade in and out
    # I recommend 5 - 30, anything else is too slow or too fast.
    # Only if FADE_OUT_COMMANDS is set to true
    FADE_DURATION = 7
    
    # How fast menu items slide
    # ONLY VALID IN PLACEMENTS 1 AND 3 WITH STYLE 2 ACTIVATED
    # For this, I recommend 3 - 15.
    SLIDE_SPEED = 3
    
    # Allow sliding, or keep it static?
    ALLOW_SLIDE = false
    
    # The transparency of the menu items
    HOVER = 225
    INACTIVE = 50
    
    # These values push items down or right on the screen so you can get them 
    # in just the right position for your game.
    TITLE_X = 0
    TITLE_Y = 0
    # New Game Offset, Continue Offset, Exit Offset
    COMMAND_X = [0,0,0]
    COMMAND_Y = [0,0,0]
    
    # If FADE_OUT_COMMANDS is false, then how visible will the commands be?
    COMMAND_OPA = 200
    
  end
end

#==============================================================================
#  Do not edit below this line if you don't know what you're doing!
#==============================================================================
class Scene_Title < Scene_Base
  include Rekx::CustomTitle
  
  def start
    super
    load_database                     
    create_game_objects               
    check_continue
    create_command_window
    play_title_music                  
    $up = true
    @nBgImage = "title_bg"
    @nfall1 = "fall1"
    @nfall2 = "fall2"
    if @continue_enabled
      @command_window.index = 1
      @nnew_game = NEW_GAME_GRAPHICS[1]
      @ncontinue = CONTINUE_GRAPHICS[0]
      @nexit = SHUTDOWN_GRAPHICS[1]
    else
      @nnew_game = NEW_GAME_GRAPHICS[0]
      @ncontinue = CONTINUE_GRAPHICS[2]
      @nexit = SHUTDOWN_GRAPHICS[1]
    end
    @ntitle = "title_image"
    @ncredits = "credits"
    load_images
  end

  def update
    super
    current_menu_index
    update_command_window_input
    falling_animation if FALLING
    @title.update
  end

  def terminate
    super
    @command_window.dispose
    @bg.dispose
    @new_game.dispose
    @continue.dispose
    @exit.dispose
    @title.dispose
    @credits.dispose
    @fall1.dispose if FALLING
    @fall2.dispose if FALLING
  end
  
  alias hsnake_dtsst_ccw create_command_window unless $@
  def create_command_window
    hsnake_dtsst_ccw
    @command_window.opacity = 0
    @command_window.contents_opacity = 0
  end

  def open_command_window
    @command_window.open
    begin
      @command_window.update
      Graphics.update
    end until @command_window.openness == 255
  end

  def close_command_window
    @command_window.close
    begin
      @command_window.update
      Graphics.update
    end until @command_window.openness == 0
  end

  def update_command_window_input
    @command_window.update
    case @command_window.index
    when 0
      refresh_command_sprites(0)
      command_new_game if Input.trigger? (Input::C)
      @new_game.z = 1
      @continue.z = 0
      @exit.z = 0
    when 1
      refresh_command_sprites(1)
      command_continue if Input.trigger? (Input::C)
      @new_game.z = 0
      @continue.z = 1
      @exit.z = 0
    when 2
      refresh_command_sprites(2)
      command_shutdown if Input.trigger? (Input::C)
      @new_game.z = 0
      @continue.z = 0
      @exit.z = 1
    end
  end

  def refresh_command_sprites(index)
    case index
    when 0
      @new_game.bitmap = Cache.picture(NEW_GAME_GRAPHICS[0])
      if @continue_enabled
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[1])
      else
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[2])
      end
      @exit.bitmap = Cache.picture(SHUTDOWN_GRAPHICS[1])
    when 1
      if @continue_enabled
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[0])
      else
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[2])
      end
      @new_game.bitmap = Cache.picture(NEW_GAME_GRAPHICS[1])
      @exit.bitmap = Cache.picture(SHUTDOWN_GRAPHICS[1])
    when 2
      @new_game.bitmap = Cache.picture(NEW_GAME_GRAPHICS[1])
      if @continue_enabled
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[1])
      else
        @continue.bitmap = Cache.picture(CONTINUE_GRAPHICS[2])
      end
      @exit.bitmap = Cache.picture(SHUTDOWN_GRAPHICS[0])
    end
  end
  
  def falling_animation
    @fall2.ox += 2
    @fall2.oy -= 1
    @fall1.ox -= 1
    @fall1.oy -= 1
  end

  def current_menu_index
    if PLACEMENT == 1
      $MOVE = (Graphics.width / 3 - 165) - 10
      $NOMOVE = (Graphics.width / 3 - 165) + 30
    elsif PLACEMENT == 3
      $MOVE = (Graphics.width - @new_game.width - 15) + 10
      $NOMOVE = (Graphics.width - @new_game.width - 15) - 30
    else
      $NOMOVE = (Graphics.width / 3) - (@new_game.width / 3)
      $MOVE = (Graphics.width / 3) - (@new_game.width / 3)
    end
    a = HOVER
    b = INACTIVE
    case @command_window.index
    when 0
      menu_opacity(a,b,b)  if FADE_OUT_COMMANDS
      if STYLE == 2
        menu_slide($NOMOVE,$MOVE,$MOVE) if ALLOW_SLIDE
      end
    when 1
      if FADE_OUT_COMMANDS
        if @continue_enabled
          menu_opacity(b,a,b)
        else
          menu_opacity(b,20,b)
        end
      end
      if STYLE == 2
        menu_slide($MOVE,$NOMOVE,$MOVE) if ALLOW_SLIDE
      end
    when 2
      menu_opacity(b,b,a) if FADE_OUT_COMMANDS
      if STYLE == 2
        menu_slide($MOVE,$MOVE,$NOMOVE) if ALLOW_SLIDE
      end
    end
  end

  def menu_opacity(a,b,c)
    FADE_DURATION.times { 
      @new_game.opacity -= 1  if @new_game.opacity > a
      @new_game.opacity += 1 if @new_game.opacity < a
      @new_game.update
    break if @new_game.opacity == a
    }
    FADE_DURATION.times { 
      @continue.opacity -= 1  if @continue.opacity > b
      @continue.opacity += 1 if @continue.opacity < b
      @continue.update
    break if @continue.opacity == b
    }
    FADE_DURATION.times { 
      @exit.opacity -= 1  if @exit.opacity > c
      @exit.opacity += 1 if @exit.opacity < c
      @exit.update
    break if @exit.opacity == c
    }
    @title.update
  end
  
  def menu_slide(a, b, c)
    SLIDE_SPEED.times { 
      @new_game.x -= 1  if @new_game.x > a
      @new_game.x += 1 if @new_game.x < a
      @new_game.update
    break if @new_game.x == a
    }
    SLIDE_SPEED.times { 
      @continue.x -= 1  if @continue.x > b
      @continue.x += 1 if @continue.x < b
      @continue.update
    break if @continue.x == b
    }
    SLIDE_SPEED.times { 
      @exit.x -= 1  if @exit.x > c
      @exit.x += 1 if @exit.x < c
      @exit.update
    break if @exit.x == c
    }
  end

  def load_images
    commands_opacity = FADE_OUT_COMMANDS ? 100 : COMMAND_OPA
    #Background
    @bg = Sprite.new
    @bg.bitmap = Cache.picture(@nBgImage)
    
    # New Game Graphic
    @new_game = Sprite.new
    @new_game.bitmap = Cache.picture(@nnew_game)
    if STYLE == 2
      @new_game.y = 288 + COMMAND_Y[0]
      if PLACEMENT == 1
        @new_game.x = ((Graphics.width / 3) - ((@new_game.width / 3) * 2)) + COMMAND_X[0]
      elsif PLACEMENT == 2
        @new_game.x =  (Graphics.width / 3) - (@new_game.width / 3) + COMMAND_X[0]
      else
        @new_game.x = (((Graphics.width / 3) * 2) - (@new_game.width / 3)) + COMMAND_X[0]
      end
    elsif STYLE == 1
      @new_game.x = Graphics.width / 3 - 165
      @new_game.y = 338 + COMMAND_Y[0]
    end
    
    # Continue Graphic
    @continue = Sprite.new
    @continue.bitmap = Cache.picture(@ncontinue)
    if STYLE == 2
      @continue.y = 318 + COMMAND_Y[1]
      if PLACEMENT == 1
        @continue.x = ((Graphics.width / 3) - ((@continue.width / 3) * 2)) + COMMAND_X[1]
      elsif PLACEMENT == 2
        @continue.x =  (Graphics.width / 3) - (@continue.width / 3) + COMMAND_X[1]
      else
        @continue.x = (((Graphics.width / 3) * 2) - (@continue.width / 3)) + COMMAND_X[1]
      end
    elsif STYLE == 1
      @continue.x = Graphics.width / 3 + 19
      @continue.y = 338
    end
    @continue.opacity = commands_opacity
    
    # Exit Graphic
    @exit = Sprite.new
    @exit.bitmap = Cache.picture(@nexit)
    if STYLE == 2
      #@exit.x = -10 
      @exit.y = 348 + COMMAND_Y[2]
      if PLACEMENT == 1
        @exit.x = ((Graphics.width / 3) - ((@exit.width / 3) * 2)) + COMMAND_X[2]
      elsif PLACEMENT == 2
        @exit.x =  (Graphics.width / 3) - (@exit.width / 3) + COMMAND_X[2]
      else
        @exit.x = (((Graphics.width / 3) * 2) - (@exit.width / 3)) + COMMAND_X[2]
      end
    elsif STYLE == 1
      @exit.x = Graphics.width - @exit.width - 15
      @exit.y = 338 + COMMAND_Y[2]
    end
    @exit.opacity = commands_opacity
    
    # Logo graphic
    @title = Sprite.new
    @title.bitmap = Cache.picture(@ntitle)
    @title.x = (Graphics.width / 2 - @title.width / 2) + TITLE_X
    @title.y = 3 + TITLE_Y
    @title.z = 10
    @title.opacity = 200
    if WAVY
      @title.wave_amp = WAVE_AMP
      @title.wave_length = WAVE_LENGTH
      @title.wave_speed = WAVE_SPEED
    end
    #Credit Image
    @credits = Sprite.new
    @credits.bitmap = Cache.picture(@ncredits)
    @credits.x = Graphics.width / 2 - (@credits.width / 2)
    @credits.y = 377
    if FALLING
      # Leaves
      @fall1 = Plane.new
      @fall1.bitmap = Cache.picture(@nfall1)
      @fall1.opacity = 130
      @fall2 = Plane.new
      @fall2.bitmap = Cache.picture(@nfall2)
      @fall2.opacity = 100
      @fall1.z = 9
      @fall2.z = 11
    end
  end
end