DarkSoul144

DSI Leaderboard

May 31st, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.50 KB | None | 0 0
  1. #==============================================================================
  2. # -- Script       : DSI-Leaderboard
  3. # -- Author       : dsiver144
  4. # -- Last Updated : 01/06/2021
  5. #==============================================================================
  6. # > Script Calls:
  7. # ---------------------------------------------------------------------
  8. # 1. open_leaderboard(board_key, point_column_name)
  9. # ex: open_leaderboard(:dart, "Darts")
  10. # 2. add_item_to_leaderboard(board_key, name, point)
  11. # ex: add_item_to_leaderboard(:dart, "Alex", 10)
  12. # 2. add_actor_to_leaderboard(board_key, actor_index_in_database, point)
  13. # ex: add_actor_to_leaderboard(:dart, 1, 25)
  14. #==============================================================================
  15. module DSIVER144
  16.   module LEADERBOARD
  17.     TEXTS = {}
  18.     TEXTS["Title"]  = "~ Leaderboard ~"
  19.     TEXTS["Rank"]   = "Rank"
  20.     TEXTS["Name"]   = "Name"
  21.    
  22.     TEXT_COLORS = {}
  23.     TEXT_COLORS["Title"] = 14
  24.     TEXT_COLORS["Column_Name"] = 16
  25.    
  26.     SELECTED_COLOR0 = Color.new(250, 208, 152, 0)
  27.     SELECTED_COLOR1 = Color.new(252, 186, 3, 255)
  28.   end
  29. end
  30.  
  31. class Game_Interpreter
  32.   #--------------------------------------------------------------------------
  33.   # * Open Leaderboard
  34.   #--------------------------------------------------------------------------
  35.   def open_leaderboard(key, point_column_name)
  36.     SceneManager.call(Scene_Leaderboard)
  37.     SceneManager.scene.prepare(key, point_column_name)
  38.     Fiber.yield
  39.   end
  40. end
  41.  
  42. class Scene_Leaderboard < Scene_MenuBase
  43.   include DSIVER144::LEADERBOARD
  44.   #--------------------------------------------------------------------------
  45.   # * Prepare
  46.   #--------------------------------------------------------------------------
  47.   def prepare(board_key, point_column_name)
  48.     @board_key = board_key
  49.     @point_column_name = point_column_name
  50.   end
  51.   #--------------------------------------------------------------------------
  52.   # * Start Scene
  53.   #--------------------------------------------------------------------------
  54.   def start
  55.     super
  56.     @list_window = Window_RankList.new(0, 0)
  57.     @list_window.set_data($game_party.leader, @board_key)
  58.     @list_window.x = (Graphics.width - @list_window.width) / 2
  59.     @list_window.y = (Graphics.height - @list_window.height) / 2
  60.     @temp_window = Window_Base.new(0, 0, @list_window.width, @list_window.height + @list_window.fitting_height(1) + 12)
  61.     @temp_window.z = 0
  62.     @list_window.z = 10
  63.    
  64.     @list_window.opacity = 0
  65.     @list_window.back_opacity = 0
  66.     @temp_window.x = (Graphics.width - @temp_window.width) / 2
  67.     @temp_window.y = (Graphics.height - @temp_window.height) / 2
  68.     @list_window.x = @temp_window.x
  69.     @list_window.y = @temp_window.y + @temp_window.height - @list_window.height
  70.     @list_window.set_handler(:cancel, method(:return_scene))
  71.    
  72.     cw = @temp_window.contents_width
  73.     lh = @temp_window.line_height
  74.     @temp_window.contents.font.size += 4
  75.     @temp_window.contents.font.bold = true
  76.     @temp_window.change_color(@temp_window.text_color(TEXT_COLORS["Title"]))
  77.     @temp_window.draw_text(0, 0, cw, lh, TEXTS["Title"], 1)
  78.     @temp_window.contents.font.size -= 4
  79.    
  80.     @temp_window.change_color(@temp_window.text_color(TEXT_COLORS["Column_Name"]))
  81.     @temp_window.draw_text(0, lh + 4, cw, lh, TEXTS["Rank"], 0)
  82.     @temp_window.draw_text(0, lh + 4, cw, lh, TEXTS["Name"], 1)
  83.     @temp_window.draw_text(0, lh + 4, cw, lh, @point_column_name, 2)
  84.     @temp_window.contents.fill_rect(0, lh + 4, cw, 1, Color.new(255, 255, 255))
  85.     @temp_window.contents.fill_rect(0, lh + 5, cw, 1, Color.new(0, 0, 0, 100))
  86.     @temp_window.contents.fill_rect(0, lh * 2 + 4, cw, 1, Color.new(255, 255, 255))
  87.     @temp_window.contents.fill_rect(0, lh * 2+ 5, cw, 1, Color.new(0, 0, 0, 100))
  88.   end
  89.  
  90. end
  91.  
  92. class Window_RankList < Window_Command
  93.   #--------------------------------------------------------------------------
  94.   # * Window Width
  95.   #--------------------------------------------------------------------------
  96.   def window_width
  97.     return Graphics.width - 200
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # * Window Height
  101.   #--------------------------------------------------------------------------
  102.   def window_height
  103.     fitting_height(10)
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # * Set Data
  107.   #--------------------------------------------------------------------------
  108.   def set_data(actor, board_key)
  109.     board = $game_system.init_leaderboard(board_key)
  110.     @data = {:actor => actor, :board => board}
  111.     refresh
  112.   end
  113.   #--------------------------------------------------------------------------
  114.   # * Make Command List
  115.   #--------------------------------------------------------------------------
  116.   def make_command_list
  117.     return unless @data
  118.     @show_leader = true
  119.     @data[:board].each do |item|
  120.       add_command("#{item.name}-#{item.points}", :ok, true)
  121.     end
  122.   end
  123.   #--------------------------------------------------------------------------
  124.   # * Get Command Ext
  125.   #--------------------------------------------------------------------------
  126.   def command_ext(index)
  127.     @list[index][:ext]
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # * Processing When OK Button Is Pressed
  131.   #--------------------------------------------------------------------------
  132.   def process_ok
  133.     if current_item_enabled?
  134.       Input.update
  135.       deactivate
  136.       call_ok_handler
  137.     else
  138.       Sound.play_buzzer
  139.     end
  140.   end
  141.   #--------------------------------------------------------------------------
  142.   # * Draw Item
  143.   #--------------------------------------------------------------------------
  144.   def draw_item(index)
  145.     rect = item_rect(index)
  146.     rect_text = item_rect_for_text(index)
  147.     name, point = command_name(index).split("-")
  148.     isLeader = @data[:actor].name == name && @show_leader
  149.     contents.font.bold = false
  150.     temp_rect = rect.clone
  151.     if isLeader
  152.       color1 = DSIVER144::LEADERBOARD::SELECTED_COLOR0
  153.       color2 = DSIVER144::LEADERBOARD::SELECTED_COLOR1
  154.       temp_rect.width /= 2
  155.       contents.gradient_fill_rect(temp_rect, color1, color2)
  156.       temp_rect.x = temp_rect.width
  157.       contents.gradient_fill_rect(temp_rect, color2, color1)
  158.       @show_leader = false
  159.       contents.font.bold = true
  160.     else
  161.       temp_rect.y += 1
  162.       temp_rect.height -= 2
  163.       contents.fill_rect(temp_rect, Color.new(0, 0, 0, 50))
  164.     end
  165.     rank = index + 1
  166.     draw_text(rect_text, rank, 0)
  167.     draw_text(rect_text, name, 1)
  168.     draw_text(rect_text, point, 2)
  169.   end
  170.  
  171. end
  172.  
  173. class Game_System
  174.   #--------------------------------------------------------------------------
  175.   # * Init Leaderboard
  176.   #--------------------------------------------------------------------------
  177.   def init_leaderboard(key)
  178.     @leaderboards ||= {}
  179.     @leaderboards[key] ||= []
  180.   end
  181.   #--------------------------------------------------------------------------
  182.   # * Clear Leaderboard
  183.   #--------------------------------------------------------------------------
  184.   def clear_leaderboard(key)
  185.     return if @leaderboards.nil?
  186.     @leaderboards[key] = []
  187.   end
  188.   #--------------------------------------------------------------------------
  189.   # * Add Item To Leaderboard
  190.   #--------------------------------------------------------------------------
  191.   def add_item_to_leaderboard(key, name, points)
  192.     board = init_leaderboard(key)
  193.     board << Leaderboard_Item.new(name, points)
  194.     sort_leaderboard(key)
  195.   end
  196.   #--------------------------------------------------------------------------
  197.   # * Add Actor To Leaderboard
  198.   #--------------------------------------------------------------------------
  199.   def add_actor_to_leaderboard(key, actor_index, points)
  200.     name = $game_actors[actor_index].name
  201.     add_item_to_leaderboard(key, name, points)
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # * Sort Leaderboard
  205.   #--------------------------------------------------------------------------
  206.   def sort_leaderboard(key)
  207.     board = init_leaderboard(key)
  208.     board.sort! {|a, b| b.points <=> a.points}
  209.   end
  210.  
  211. end
  212.  
  213. class Leaderboard_Item
  214.   attr_accessor :name
  215.   attr_accessor :points
  216.   #--------------------------------------------------------------------------
  217.   # * Initialize Leaderboard Item
  218.   #--------------------------------------------------------------------------
  219.   def initialize(name, points)
  220.     @name = name
  221.     @points = points
  222.   end
  223. end
Add Comment
Please, Sign In to add comment