TheoAllen

Theo - Highscore Board

Oct 29th, 2013
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.29 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Highscore Board
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_HighScore] = true
  8. # =============================================================================
  9. # Change Logs:
  10. # -----------------------------------------------------------------------------
  11. # 2013.10.24 - Finished Script
  12. # =============================================================================
  13. =begin
  14.  
  15.   Perkenalan :
  16.   Mau game kamu punya fitur highscore? Script ini mungkin bisa mengabulkan
  17.   permintaan kamu
  18.  
  19.   Cara penggunaan :
  20.   Pasang script ini dibawah material namun diatas main
  21.   Gunakan beberapa script call berikut untuk keperluan tertentu
  22.  
  23.   *) Untuk masuk ke menu High Score
  24.      - SceneManager.goto_scoreboard  
  25.      
  26.   *) Untuk memasukkan nama buat score, gunakan script call ini
  27.      - SceneManager.input_scorename(max_char)
  28.      Ganti max_char dengan angka. Angka tersebut adalah untuk menentukan
  29.      sebanyak apa player boleh memasukkan huruf untuk nama
  30.  
  31.   Instruksi lain ada di konfigurasi
  32.      
  33.   Terms of use :
  34.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  35.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  36.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.
  37.  
  38. =end
  39. # =============================================================================
  40. # Konfigurasi :
  41. # =============================================================================
  42. module Theo
  43.   module HScore
  44.  
  45.   # --------------------------------------------------------------------
  46.   # *) Setting umum
  47.   # --------------------------------------------------------------------
  48.     Variable_ID   = 1
  49.   # Tentukan variable ID yang akan jadi acuan untuk penilaian skor
  50.    
  51.     Reset_Key     = :SHIFT
  52.   # Key buat ngereset score kalo kamu lagi di menu highscore
  53.  
  54.   # --------------------------------------------------------------------
  55.   # *) Kosakata umum
  56.   # --------------------------------------------------------------------
  57.     ScoreTitle    = "HighScore Board"   # Title untuk menu highscore
  58.     ScoreTable    = "No.  Name"         # Text untuk tabel kiri
  59.     ScoreVocab    = "Score"             # Sebutan untuk skor
  60.     ScoreCommand  = "HighScore"         # Command yang ada di title screen
  61.     YourScore     = "Your Score :"      # Text saat kamu memasukkan nama
  62.    
  63.   # --------------------------------------------------------------------
  64.   # *) Untuk perihal konfirmasi ngereset skor
  65.   # --------------------------------------------------------------------
  66.     ScoreFooter   = "Press SHIFT to reset your score"
  67.   # Kata bantuan untuk ngereset
  68.     Confirmation  = "Are you sure want to reset the highscore?"
  69.   # Konfirmasi saat player mo ngereset skor
  70.  
  71.     YesCommand    = "Yes"   # Kata untuk iya
  72.     NoCommand     = "No"    # Kata untuk tidak
  73.    
  74.   end
  75. end
  76. # =============================================================================
  77. # Akhir dari konfigurasi
  78. # =============================================================================
  79. class << DataManager
  80.  
  81.   alias theo_hscore_load_db load_database
  82.   def load_database
  83.     theo_hscore_load_db
  84.     load_scoreboard
  85.   end
  86.  
  87.   def load_scoreboard
  88.     open_scoreboard
  89.     $scoreboard = load_data(score_path)
  90.   end
  91.  
  92.   def open_scoreboard(reset = false)
  93.     if !FileTest.exist?(score_path) || reset
  94.       score = []
  95.       File.open(score_path, "w") do |file|
  96.         Marshal.dump(score, file)
  97.       end
  98.     end
  99.   end
  100.  
  101.   def reset_score
  102.     open_scoreboard(true)
  103.     $scoreboard = load_data(score_path)
  104.   end
  105.  
  106.   def update_score
  107.     save_data($scoreboard, score_path)
  108.   end
  109.  
  110.   def score_path
  111.     return "Data/Score.rvdata2"
  112.   end
  113.  
  114.   def score
  115.     $game_variables[Theo::HScore::Variable_ID]
  116.   end
  117.  
  118. end
  119.  
  120. class ScoreBoard
  121.   attr_accessor :name
  122.   attr_accessor :score
  123.  
  124.   def initialize(name, score)
  125.     @name = name
  126.     @score = score
  127.   end
  128. end
  129.  
  130. class Window_HScoreTitle < Window_Base
  131.  
  132.   def initialize
  133.     super(0,0,Graphics.width,fitting_height(2))
  134.     draw_title
  135.   end
  136.  
  137.   def draw_title
  138.     draw_text(contents.rect, Theo::HScore::ScoreTitle,1)
  139.   end
  140.  
  141. end
  142.  
  143. class Window_HScoreBoard < Window_Base
  144.  
  145.   def initialize(y)
  146.     super(0,y,Graphics.width, Graphics.height - y - line_height*2)
  147.     refresh
  148.   end
  149.  
  150.   def refresh
  151.     contents.clear
  152.     draw_contents
  153.   end
  154.  
  155.   def draw_contents
  156.     scorelist = $scoreboard.sort {|a,b| b.score <=> a.score }
  157.     draw_header
  158.     ((contents.height / line_height) - 1).times do |line|
  159.       score = scorelist[line]
  160.       line += 1
  161.       rect = Rect.new(0,line*line_height,contents.width,line_height)
  162.       if score
  163.         txt = sprintf("%d.   %s",line, score.name)
  164.         draw_text(rect,txt)
  165.         draw_text(rect, score.score,2)
  166.       else
  167.         txt = sprintf("%d.   ",line)
  168.         draw_text(rect,txt)
  169.       end
  170.     end
  171.   end
  172.  
  173.   def draw_header
  174.     rect = Rect.new(0,0,contents.width, line_height)
  175.     change_color(system_color)
  176.     draw_text(rect, Theo::HScore::ScoreTable)
  177.     draw_text(rect, Theo::HScore::ScoreVocab, 2)
  178.     change_color(normal_color)
  179.     color = Color.new(255,255,255,128)
  180.     contents.fill_rect(0,23,contents.width,2,color)
  181.   end
  182.  
  183. end
  184.  
  185. class Window_HScoreFooter < Window_Base
  186.  
  187.   def initialize
  188.     super(0,0,Graphics.width, fitting_height(1))
  189.     self.y = Graphics.height - height
  190.     draw_text(contents.rect, Theo::HScore::ScoreFooter,1)
  191.   end
  192.  
  193. end
  194.  
  195. class Window_HScoreCommand < Window_Command
  196.  
  197.   def initialize
  198.     super(0,0)
  199.     self.openness = 0
  200.     select(0)
  201.     deactivate
  202.   end
  203.  
  204.   def make_command_list
  205.     add_command(Theo::HScore::YesCommand, :yes)
  206.     add_command(Theo::HScore::NoCommand, :cancel)
  207.   end
  208.  
  209. end
  210.  
  211. class Window_HScoreConfirm < Window_Base
  212.  
  213.   def initialize
  214.     super(0,0,window_width,fitting_height(1))
  215.     self.openness = 0
  216.     update_placement    
  217.     draw_text(contents.rect, Theo::HScore::Confirmation, 1)
  218.   end
  219.  
  220.   def update_placement
  221.     self.x = (Graphics.width - width) / 2
  222.     self.y = ((Graphics.height - height) / 2) - 25
  223.   end
  224.  
  225.   def window_width
  226.     return 400
  227.   end
  228.  
  229. end
  230.  
  231. class Scene_ScoreBoard < Scene_Base
  232.  
  233.   def start
  234.     super
  235.     @title = Window_HScoreTitle.new
  236.     @score = Window_HScoreBoard.new(@title.height)
  237.     @footer = Window_HScoreFooter.new
  238.     @confirm = Window_HScoreConfirm.new
  239.     create_confirm
  240.   end
  241.  
  242.   def create_confirm
  243.     @command = Window_HScoreCommand.new
  244.     @command.x = (Graphics.width - @command.width) / 2
  245.     @command.y = @confirm.y + @confirm.height
  246.     @command.set_handler(:yes, method(:reset_score))
  247.     @command.set_handler(:cancel, method(:close_confirm))
  248.   end
  249.  
  250.   def update
  251.     super
  252.     if Input.trigger?(:B) && !@command.active
  253.       Input.update
  254.       Sound.play_cancel
  255.       return_scene
  256.     elsif Input.trigger?(Theo::HScore::Reset_Key) && !@command_active
  257.       open_confirmation
  258.     end
  259.   end
  260.  
  261.   def open_confirmation
  262.     @command.activate
  263.     @command.open
  264.     @confirm.open
  265.   end
  266.  
  267.   def reset_score
  268.     DataManager.reset_score
  269.     @score.refresh
  270.     close_confirm
  271.   end
  272.  
  273.   def close_confirm
  274.     @command.deactivate
  275.     @command.close
  276.     @confirm.close
  277.   end
  278.  
  279. end
  280.  
  281. class Window_ScoreName < Window_NameEdit
  282.   class PseudoChar
  283.     attr_accessor :name
  284.    
  285.     def initialize
  286.       @name = ""
  287.     end
  288.   end
  289.   attr_reader :actor
  290.  
  291.   def initialize(max_char)
  292.     super(PseudoChar.new, max_char)
  293.   end
  294.  
  295.   def draw_actor_face(actor, x, y)
  296.     rect = Rect.new(0,0,100,line_height)
  297.     change_color(system_color)
  298.     draw_text(rect, Theo::HScore::YourScore)
  299.     rect.y += line_height
  300.     change_color(normal_color)
  301.     draw_text(rect, DataManager.score)
  302.   end
  303. end
  304.  
  305. class Scene_ScoreName < Scene_MenuBase
  306.  
  307.   def prepare(max_char)
  308.     @max_char = max_char
  309.   end
  310.  
  311.   def start
  312.     super
  313.     @edit_window = Window_ScoreName.new(@max_char)
  314.     @input_window = Window_NameInput.new(@edit_window)
  315.     @input_window.set_handler(:ok, method(:on_input_ok))
  316.   end
  317.  
  318.   def on_input_ok
  319.     $scoreboard.push(ScoreBoard.new(@edit_window.name, DataManager.score))
  320.     DataManager.update_score
  321.     return_scene
  322.   end
  323.  
  324.   def return_scene
  325.     SceneManager.goto(Scene_ScoreBoard)
  326.   end
  327.  
  328. end
  329.  
  330. class Window_TitleCommand < Window_Command
  331.  
  332.   alias theo_hscore_make_command make_command_list
  333.   def make_command_list
  334.     theo_hscore_make_command
  335.     hash = {
  336.       :name=> Theo::HScore::ScoreCommand,
  337.       :symbol=> :hscore,
  338.       :enabled=> true,
  339.       :ext=> nil
  340.     }
  341.     @list.insert(2, hash)
  342.   end
  343.  
  344. end
  345.  
  346. class Scene_Title < Scene_Base
  347.  
  348.   alias theo_hscore_start start
  349.   def start
  350.     theo_hscore_start
  351.     @command_window.set_handler(:hscore,method(:goto_hscore)) if @command_window
  352.   end
  353.  
  354.   def goto_hscore
  355.     SceneManager.goto_scoreboard
  356.   end
  357.  
  358. end
  359.  
  360. class << SceneManager
  361.  
  362.   def goto_scoreboard
  363.     call(Scene_ScoreBoard)
  364.   end
  365.  
  366.   def input_scorename(max_char = 8)
  367.     call(Scene_ScoreName)
  368.     scene.prepare(max_char)
  369.   end
  370.  
  371. end
Advertisement
Add Comment
Please, Sign In to add comment