Advertisement
roninator2

Code Input on Title Screen

Nov 21st, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 21.11 KB | None | 0 0
  1. # ╔═══════════════════════════════════════════════╦════════════════════╗
  2. # ║ Title: Code Input on Title Screen             ║  Version: 1.02     ║
  3. # ║ Author: Roninator2                            ║                    ║
  4. # ╠═══════════════════════════════════════════════╬════════════════════╣
  5. # ║ Function:                                     ║   Date Created     ║
  6. # ║   Allows to input a 'code' on the             ╠════════════════════╣
  7. # ║   title screen to activate bonuses            ║    15 Feb 2021     ║
  8. # ╚═══════════════════════════════════════════════╩════════════════════╝
  9. # ╔════════════════════════════════════════════════════════════════════╗
  10. # ║ Requires: nil                                                      ║
  11. # ║                                                                    ║
  12. # ╚════════════════════════════════════════════════════════════════════╝
  13. # ╔════════════════════════════════════════════════════════════════════╗
  14. # ║ Brief Description:                                                 ║
  15. # ║       Allow entering in a cheat code                               ║
  16. # ╚════════════════════════════════════════════════════════════════════╝
  17. # ╔════════════════════════════════════════════════════════════════════╗
  18. # ║ Instructions:                                                      ║
  19. # ║ Script provides the ability to input a code like you               ║
  20. # ║  use to on old arcade games, in order to activate                  ║
  21. # ║  bonuses or special powers.                                        ║
  22. # ║                                                                    ║
  23. # ║  Adjust the settings as desired for how many codes                 ║
  24. # ║  you wish to use for your game.                                    ║
  25. # ║  'B' cannot be used as it controls deleting entries                ║
  26. # ║  and closing the window. This is the X on the keyboard             ║
  27. # ║                                                                    ║
  28. # ║  Codes must have the text in lower case not upper case             ║
  29. # ║                                                                    ║
  30. # ║   Name  Game pad    Keyboard          Main function                ║
  31. # ║   A     Button 1    Shift             Dash                         ║
  32. # ║   B     Button 2    Esc, Num 0, X     Cancel, Menu                 ║
  33. # ║   C     Button 3    Space, Enter, Z   Confirm, OK, Enter           ║
  34. # ║   X     Button 4    A -                                            ║
  35. # ║   Y     Button 5    S -                                            ║
  36. # ║   Z     Button 6    D -                                            ║
  37. # ║   L     Button 7    Q, Page Up        Previous page                ║
  38. # ║   R     Button 8    W, Page Down      Next page                    ║
  39. # ║                                                                    ║
  40. # ║   Other options are                                                ║
  41. # ║   CTRL    ??        CTRL                                           ║
  42. # ║   ALT     ??        ALT                                            ║
  43. # ║   Down   down                                                      ║
  44. # ║   Up     up                                                        ║
  45. # ║   Left   left                                                      ║
  46. # ║   Right  right                                                     ║
  47. # ║                                                                    ║
  48. # ║   More could be programmed if using a full keyboard input script   ║
  49. # ║   this script does not account for keyboard input scripts.         ║
  50. # ╚════════════════════════════════════════════════════════════════════╝
  51. # ╔════════════════════════════════════════════════════════════════════╗
  52. # ║ Updates:                                                           ║
  53. # ║ 1.00 - 15 Feb 2021 - Script finished                               ║
  54. # ║ 1.01 - 15 Feb 2021 - Added block codes if continue                 ║
  55. # ║ 1.02 - 16 Feb 2021 - Added option to hide code in menu             ║
  56. # ║                                                                    ║
  57. # ╚════════════════════════════════════════════════════════════════════╝
  58. # ╔════════════════════════════════════════════════════════════════════╗
  59. # ║ Credits and Thanks:                                                ║
  60. # ║   Roninator2                                                       ║
  61. # ║                                                                    ║
  62. # ╚════════════════════════════════════════════════════════════════════╝
  63. # ╔════════════════════════════════════════════════════════════════════╗
  64. # ║ Terms of use:                                                      ║
  65. # ║  Follow the original Authors terms of use where applicable         ║
  66. # ║    - When not made by me (Roninator2)                              ║
  67. # ║  Free for all uses in RPG Maker except nudity                      ║
  68. # ║  Anyone using this script in their project before these terms      ║
  69. # ║  were changed are allowed to use this script even if it conflicts  ║
  70. # ║  with these new terms. New terms effective 03 Apr 2024             ║
  71. # ║  No part of this code can be used with AI programs or tools        ║
  72. # ║  Credit must be given                                              ║
  73. # ╚════════════════════════════════════════════════════════════════════╝
  74.  
  75. module R2_BONUS_CODES
  76.  
  77.     # Codes for cheats/bonuses.
  78.   # Leave empty if not using that code. I.E. :code# = [""]
  79.     # Add more as you desire
  80.   # each code matches to the switches listed below
  81.     CHEATCODES = {
  82.   :code1 => ["up","up","down","down","left","left","right","right","x","y"],
  83.   :code2 => ["left","left","down","left","right","right","z","x"],
  84.   :code3 => ["up","l","down","r","up","left","down","right","r","l","y"],
  85.   :code4 => [""],
  86.     }
  87.  
  88.   # add more switches as you add more codes.
  89.   # set to 0 to not use that code
  90.   # [switch 5, switch 6, switch 7, switch 0]
  91.   CODE_SWITCHES = [5, 6, 7, 0]
  92.  
  93.     # Sounds for codes
  94.   BAD = "Buzzer1"       # wrong code entry
  95.   BTN = "Knock"         # Button pressed
  96.   CODE = "Load"       # Code complete
  97.    
  98.   CONTINUE = false # if a save file is present, code entry is blocked when true
  99.   HIDE_COMMAND = false # true will hide the command in the title menu
  100.   # then use F8 to open the code screen, F7 to back space and close
  101. end
  102.  
  103. # ╔════════════════════════════════════════════════════════════════════╗
  104. # ║                      End of editable region                        ║
  105. # ╚════════════════════════════════════════════════════════════════════╝
  106.  
  107. #######################################################################
  108. # * Window CodeCommand
  109. #######################################################################
  110.  
  111. class Window_CodeCommand < Window_Selectable
  112.   #--------------------------------------------------------------------------
  113.   # * Object Initialization
  114.   #--------------------------------------------------------------------------
  115.   def initialize(x, y, width, height)
  116.     super(x, y, width, height)
  117.     @inputed_code = []
  118.     @pos = 0
  119.   end
  120.   #--------------------------------------------------------------------------
  121.   # * Process handling
  122.   #--------------------------------------------------------------------------
  123.   def process_handling
  124.     if R2_BONUS_CODES::HIDE_COMMAND
  125.       process_back if Input.repeat?(:F7)
  126.     else
  127.       process_back if Input.repeat?(:B)
  128.     end
  129.   end
  130.   #--------------------------------------------------------------------------
  131.   # * Process delete input / go back one
  132.   #--------------------------------------------------------------------------
  133.   def process_back
  134.     if @pos == 0
  135.       call_cancel_handler
  136.     else
  137.       @inputed_code.pop
  138.       @pos -= 1
  139.     end
  140.   end
  141.   #--------------------------------------------------------------------------
  142.   # * Call cancel to close window
  143.   #--------------------------------------------------------------------------
  144.   def call_cancel_handler
  145.     call_handler(:cancel)
  146.   end
  147.   #--------------------------------------------------------------------------
  148.   # * Update
  149.   #--------------------------------------------------------------------------
  150.   def update
  151.     @sprite.dispose if @sprite
  152.     return unless @inputed_code != []
  153.     @sprite = Sprite.new
  154.     @sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  155.     @sprite.z = 400
  156.     rect = Rect.new(10, y+10, Graphics.width - 20, line_height)
  157.     result = @inputed_code.to_s.clone
  158.     result.gsub!(/\"/)  { "" }
  159.    result.gsub!(/\[/)  { "" }
  160.    result.gsub!(/\]/)  { "" }
  161.    text = result
  162.    @sprite.bitmap.font.size = 24
  163.    @sprite.bitmap.draw_text(rect, text)
  164.  end
  165.  #--------------------------------------------------------------------------
  166.  # * Reset window
  167.  #--------------------------------------------------------------------------
  168.  def reset
  169.    @inputed_code = []
  170.    @pos = 0
  171.  end
  172.  #--------------------------------------------------------------------------
  173.  # * Display Input
  174.  #--------------------------------------------------------------------------
  175.  def dis_input(word)
  176.    case word
  177.    when "down"
  178.      @inputed_code[@pos] = "Down"
  179.      @pos += 1
  180.    when "left"
  181.      @inputed_code[@pos] = "Left"
  182.      @pos += 1
  183.    when "right"
  184.      @inputed_code[@pos] = "Right"
  185.      @pos += 1
  186.    when "up"
  187.      @inputed_code[@pos] = "Up"
  188.      @pos += 1
  189.    when "a"
  190.      @inputed_code[@pos] = "A"
  191.      @pos += 1
  192.    when "b"
  193.      if R2_BONUS_CODES::HIDE_COMMAND
  194.        @inputed_code[@pos] = "B"
  195.        @pos += 1
  196.      else
  197.        process_handling
  198.      end
  199.    when "c"
  200.      @inputed_code[@pos] = "C"
  201.      @pos += 1
  202.    when "x"
  203.      @inputed_code[@pos] = "X"
  204.      @pos += 1
  205.    when "y"
  206.      @inputed_code[@pos] = "Y"
  207.      @pos += 1
  208.    when "z"
  209.      @inputed_code[@pos] = "Z"
  210.      @pos += 1
  211.    when "l"
  212.      @inputed_code[@pos] = "L"
  213.      @pos += 1
  214.    when "r"
  215.      @inputed_code[@pos] = "R"
  216.      @pos += 1
  217.    when "ctrl"
  218.      @inputed_code[@pos] = "Ctrl"
  219.      @pos += 1
  220.    when "alt"
  221.      @inputed_code[@pos] = "Alt"
  222.      @pos += 1
  223.    when "f7"
  224.      process_handling if R2_BONUS_CODES::HIDE_COMMAND
  225.    end
  226.  end
  227. end
  228.  
  229. #######################################################################
  230. # * Add title command for codes
  231. #######################################################################
  232. class Window_TitleCommand < Window_Command
  233.  def make_command_list
  234.    codes = R2_BONUS_CODES::CONTINUE ? !continue_enabled : true
  235.    add_command(Vocab::new_game, :new_game)
  236.    add_command(Vocab::continue, :continue, continue_enabled)
  237.    add_command(Vocab::shutdown, :shutdown)
  238.    add_command("Enter Codes", :bonus_code, codes) unless R2_BONUS_CODES::HIDE_COMMAND
  239.  end
  240. end
  241.  
  242. #######################################################################
  243. # * Scene Title
  244. #######################################################################
  245. class Scene_Title < Scene_Base
  246.  #--------------------------------------------------------------------------
  247.  # * Object Initialization
  248.  #--------------------------------------------------------------------------
  249.     alias r2_bonus_code_start   start
  250.  def start
  251.    r2_bonus_code_start
  252.    @r2_btn_press = 0
  253.    @temp_count = 0
  254.         @cheatcodes = []
  255.    @linecode = []
  256.    for i in 0..R2_BONUS_CODES::CHEATCODES.size - 1
  257.      @cheatcodes.push(false)
  258.      @linecode.push(true)
  259.    end
  260.    create_code_window
  261.         @enter_code = false
  262.  end
  263.  #--------------------------------------------------------------------------
  264.  # * Create Command Window
  265.  #--------------------------------------------------------------------------
  266.  def create_command_window
  267.    @command_window = Window_TitleCommand.new
  268.    @command_window.set_handler(:new_game, method(:command_new_game))
  269.    @command_window.set_handler(:continue, method(:command_continue))
  270.    @command_window.set_handler(:shutdown, method(:command_shutdown))
  271.    @command_window.set_handler(:bonus_code, method(:command_bonus_code)) unless R2_BONUS_CODES::HIDE_COMMAND
  272.  end
  273.  #--------------------------------------------------------------------------
  274.  # * Create Code Window
  275.  #--------------------------------------------------------------------------
  276.  def create_code_window
  277.    @code_window = Window_CodeCommand.new(0, 160, Graphics.width, 48)
  278.    @code_window.viewport = @viewport
  279.    @code_window.x = 0
  280.    @code_window.y = Graphics.height / 2 - 50
  281.    @code_window.hide
  282.    @code_window.set_handler(:cancel,    method(:close_code_window))
  283.  end
  284.  #--------------------------------------------------------------------------
  285.  # * Close Code Window
  286.  #--------------------------------------------------------------------------
  287.  def close_code_window
  288.    @code_window.deactivate
  289.    @code_window.hide
  290.    @command_window.activate
  291.    for i in 0..R2_BONUS_CODES::CHEATCODES.size - 1
  292.      @linecode[i] = true if @linecode[i] == false
  293.    end
  294.         @enter_code = false
  295.  end
  296.  #--------------------------------------------------------------------------
  297.  # * [New Game] Command
  298.  #--------------------------------------------------------------------------
  299.  def command_new_game
  300.    DataManager.setup_new_game
  301.    close_command_window
  302.    fadeout_all
  303.    $game_map.autoplay
  304.    SceneManager.goto(Scene_Map)
  305.    check_bonus
  306.  end
  307.  #--------------------------------------------------------------------------
  308.  # * Activate switches
  309.  #--------------------------------------------------------------------------
  310.  def check_bonus
  311.    R2_BONUS_CODES::CODE_SWITCHES.each_with_index { |code, i|
  312.      next if code == 0
  313.      if @cheatcodes[i] == true
  314.        $game_switches[R2_BONUS_CODES::CODE_SWITCHES[i]] = true
  315.      end
  316.    }
  317.  end
  318.  #--------------------------------------------------------------------------
  319.  # * Show code window
  320.  #--------------------------------------------------------------------------
  321.  def command_bonus_code
  322.    @code_window.show
  323.    @code_window.activate
  324.    max_length
  325.         @enter_code = true
  326.    @command_window.deactivate
  327.  end
  328.  #--------------------------------------------------------------------------
  329.  # * Show code window
  330.  #--------------------------------------------------------------------------
  331.  def max_length
  332.    @max_length = 0
  333.         R2_BONUS_CODES::CHEATCODES.each_with_index do |code_hash, c|
  334.             code = code_hash[1]
  335.      code.each_with_index do | step, i |
  336.        @max_length = code.length if (@max_length < code.length)
  337.      end
  338.    end
  339.  end
  340.  #--------------------------------------------------------------------------
  341.  # * Wait to slow player input
  342.  #--------------------------------------------------------------------------
  343.  def wait(duration)
  344.    count = 0
  345.    duration.times { count += 1}
  346.    return
  347.  end
  348.  #--------------------------------------------------------------------------
  349.  # * Alias Update
  350.  #--------------------------------------------------------------------------
  351.     alias r2_bonus_code_update  update
  352.  def update
  353.    r2_bonus_code_update
  354.    if R2_BONUS_CODES::HIDE_COMMAND
  355.      command_bonus_code if Input.trigger?(:F8)
  356.    end
  357.    btn = triggered if @enter_code
  358.    return if btn == nil
  359.    RPG::SE.new(R2_BONUS_CODES::BTN).play
  360.    wait(30) # wait between player input
  361.    climb = false
  362.    @r2_btn_press -= 1 if btn == "b"
  363.    @r2_btn_press -= 1 if btn == "f7"
  364.    @temp_count -= 1 if btn == "b"
  365.    @temp_count -= 1 if btn == "f7"
  366.    @code_window.reset if @r2_btn_press == -1
  367.    @r2_btn_press = 0 if @r2_btn_press <= 0
  368.    @temp_count = 0 if @temp_count <= 0
  369.    if @r2_btn_press == 0
  370.      for i in 0..R2_BONUS_CODES::CHEATCODES.size - 1
  371.        @linecode[i] = true if @linecode[i] == false
  372.      end
  373.    end
  374.         R2_BONUS_CODES::CHEATCODES.each_with_index do |code_hash, c|
  375.      for line in code_hash
  376.        next unless @linecode[c] == true
  377.      end
  378.             code = code_hash[1]
  379.      code.each_with_index do | step, i |
  380.        if (@r2_btn_press == i) && (btn == code[@r2_btn_press])
  381.          climb = true
  382.          if @r2_btn_press == code.length - 1
  383.            @cheatcodes[c] = true
  384.            RPG::SE.new(R2_BONUS_CODES::CODE).play
  385.            reset
  386.            for r in 0..R2_BONUS_CODES::CHEATCODES.size - 1
  387.              @linecode[r] = true if @linecode[r] == false
  388.            end
  389.          end
  390.        elsif (btn != code[@r2_btn_press])
  391.          @linecode[c] = false
  392.          if @temp_count > @max_length
  393.            RPG::SE.new(R2_BONUS_CODES::BAD).play
  394.            reset
  395.          end
  396.        end
  397.      end
  398.    end
  399.    @temp_count += 1
  400.    @r2_btn_press += 1 if climb == true
  401.    code_count = 0
  402.    @linecode.each do |b|
  403.      if b == true
  404.        code_count += 1
  405.      end
  406.    end
  407.    if code_count == 0 && @r2_btn_press == 0
  408.      @code_window.reset
  409.      for i in 0..R2_BONUS_CODES::CHEATCODES.size - 1
  410.        @linecode[i] = true if @linecode[i] == false
  411.      end
  412.      @temp_count = 0
  413.    end
  414.  end
  415.  #--------------------------------------------------------------------------
  416.  # * Reset - set values to 0
  417.  #--------------------------------------------------------------------------
  418.  def reset
  419.    @code_window.reset
  420.    @r2_btn_press = 0
  421.    @temp_count = 0
  422.  end
  423.  #--------------------------------------------------------------------------
  424.  # * Triggered - to check player input
  425.  #--------------------------------------------------------------------------
  426.  def triggered
  427.    case true
  428.    when Input.trigger?(:DOWN)
  429.      @code_window.dis_input("down")
  430.      return "down"
  431.    when Input.trigger?(:LEFT)
  432.      @code_window.dis_input("left")
  433.      return "left"
  434.    when Input.trigger?(:RIGHT)
  435.      @code_window.dis_input("right")
  436.      return "right"
  437.    when Input.trigger?(:UP)
  438.      @code_window.dis_input("up")
  439.      return "up"
  440.    when Input.trigger?(:A)
  441.      @code_window.dis_input("a")
  442.      return "a"
  443.    when Input.trigger?(:B)
  444.      @code_window.dis_input("b")
  445.      return "b"
  446.    when Input.trigger?(:C)
  447.      @code_window.dis_input("c")
  448.      return "c"
  449.    when Input.trigger?(:X)
  450.      @code_window.dis_input("x")
  451.      return "x"
  452.    when Input.trigger?(:Y)
  453.      @code_window.dis_input("y")
  454.      return "y"
  455.    when Input.trigger?(:Z)
  456.      @code_window.dis_input("z")
  457.      return "z"
  458.    when Input.trigger?(:L)
  459.      @code_window.dis_input("l")
  460.      return "l"
  461.    when Input.trigger?(:R)
  462.      @code_window.dis_input("r")
  463.      return "r"
  464.    when Input.trigger?(:CTRL)
  465.      @code_window.dis_input("ctrl")
  466.      return "ctrl"
  467.    when Input.trigger?(:ALT)
  468.      @code_window.dis_input("alt")
  469.      return "alt"
  470.    when Input.trigger?(:F7)
  471.      @code_window.dis_input("f7")
  472.    end
  473.  end
  474. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement