thiago_d_d

Book System VX

Jan 15th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.02 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3. # TSDA Book System
  4. #===============================================================================
  5. # by: thiago_d_d
  6. # http://thiagodd.blogspot.com.br
  7. # Versions: 1.0
  8. #===============================================================================
  9. # This scripts is a database/viewer of books:
  10. #  - It breaks lines.
  11. #  - Justify the text.You can use custom BG, fonts, sounds.
  12. #  - You can change text color using the \\C tag
  13. #  - The books are protected in the Books.rvdata2 in you Data folder.
  14. #===============================================================================
  15. # Don't forget to configure the TSDA module!
  16. #===============================================================================
  17.  -----> To create new books
  18.     To create a new book, you have to type the text in this format, in the TSDA module:
  19.  
  20. BookName = [
  21. "paragraph1",
  22. "paragraph2",
  23. "paragraph3",
  24. ]
  25.  
  26. Don't put new lines in every paragraph, the script will break it.
  27. You can use only letters and underlines in the book name. This isn't a problem, as the
  28. book title will be shown as a image you have done in the Pictures folder.
  29.  
  30. After that, modify this line in the TSDA module:
  31. BOOKS = [blablabla]
  32. Put your book name in this list.
  33.  
  34. And then remove the # in the last line of this script, and execute the game.
  35. The books.rvdata2 will be created in you Data folder. Put the # in the line again
  36.  
  37. You can delete the book text used above after you finish you project, ad the book is stored
  38. in the Books.rvdata2
  39.  
  40.  -----> Calling the book viewer
  41.  $book_name = "bookname"
  42.  SceneManager.call(Scene_Book)
  43.  
  44.  -----> Changing text color
  45.  Use this tag:
  46.  \\C[color number]
  47.  
  48.  -----> About the BG
  49.  Must be in the Pictures folder.
  50. #===============================================================================
  51. =end
  52. module TSDA
  53.   # This is the example book
  54.   Exemplo = [
  55.     "        No mundo de \\C[2]Stella Anima\\C[0], o planeta Terra é constituído por uma substância imperceptível porém tão abundante quanto o próprio ar que os seres humanos respiram. Alguns chamam essa tal substância de “mana”, outros a chamam de “pó dos deuses”, mas para regra geral, foi oficialmente adotado o nome de aura estelar.A partir da aura estelar o universo foi dividido em diversas “dimensões” ou como foram oficialmente chamadas: dobras astrais.Não se sabe muito sobre as dobras astrais, apenas que Chronus é a dobra onde os humanos vivem, e outra chamada Mitrandis, onde habitam os espíritos.",
  56.     "        Na dimensão Chronus, cada ser humano tem sua aura estelar, catalizada num ponto específico do corpo chamado vortex polar, podendo ser lunar ou solar. Neste plano as pessoas estão acostumadas a utilizar sua aura estelar para alterar a realidade, o que alguns chamam de Astralogia. Porém, nem todos os humanos tem interesse e habilidade para dominar sua aura, preferindo pagar serviços de outras pessoas. Os seres humanos que usam sua aura estelar são chamados de Espiritualistas.",
  57.   ]
  58.   #Windowskin used to retrive the color used by the tag \\C
  59.   WINDOWSKIN = "Window"
  60.   #Books list
  61.   BOOKS = ["Exemplo"]
  62.   #Scene to transfer after reading the book
  63.   SCENE_TO_GO = Scene_Map
  64.   # Put here if the book will have a cover with the book title
  65.   # Format:
  66.   # {
  67.   #     "Livro" => true or false,
  68.   #     "Livro2" => true or false
  69.   # }
  70.   USE_TITLE = {
  71.     "Exemplo" => false
  72.   }
  73.   #Put here the picture used as a cover in the book, if the book use the cover
  74.   # Format:
  75.   # {
  76.   #     "Livro" => "IMg1",
  77.   #     "Livro2" => "IMg2"
  78.   # }
  79.   TITLE_IMAGE = {
  80.   }
  81.   # Put here if the book uses a BG or the windowskin
  82.   # Format:
  83.   # {
  84.   #     "Livro" => true or false,
  85.   #     "Livro2" => true or false
  86.   # }
  87.   USE_WINDOWSKIN = {
  88.     "Exemplo" => false
  89.   }
  90.   # Put there the BG name used by the book, in the Pictures folder.
  91.   # Format:
  92.   # {
  93.   #     "Livro" => "BG1",
  94.   #     "Livro2" => "GB2"
  95.   # }
  96.   BG_IMAGE = {
  97.     "Exemplo" => "ExemploBG"
  98.   }
  99.   # Put here the sound used when changing pages
  100.   # Format:
  101.   # {
  102.   #     "Livro" => "Sound1",
  103.   #     "Livro2" => "Sound1"
  104.   # }
  105.   PAGE_SOUND = {
  106.     "Exemplo" => "Book1"
  107.   }
  108.   # Put here the text font used by the book
  109.   # Format:
  110.   # {
  111.   #     "Livro" => "Trebuchet MS",
  112.   #     "Livro2" => "Arial"
  113.   # }
  114.   FONT_NAME = {
  115.     "Exemplo" => "Comic Sans MS"
  116.   }
  117. end
  118. #===============================================================================
  119. class String
  120.   def true_size
  121.     copy = self.clone;size = 0
  122.     while(copy.slice!(/./m)) != nil;size += 1;end;size
  123.   end
  124.   #=============================================================================
  125.   def starts_with?(string2)
  126.     if self[0,string2.size] == string2
  127.       $ind = string2.size;return true;else;return false;end
  128.   end
  129.   #=============================================================================
  130.   def substring(starting_index,final_index)
  131.     self[starting_index,final_index - starting_index]
  132.   end
  133.   #=============================================================================
  134.   def index_of(starting_index,st)
  135.     return -1 if starting_index > size or starting_index < 0
  136.     for i in starting_index...size
  137.       return i if self[i,1] == st;end;return -1
  138.   end
  139.   #=============================================================================
  140.   def last_index_of(st)
  141.     pos = -1
  142.     while (pos = index_of(pos + 1," ")) != -1
  143.       tpos = pos;end;return tpos
  144.   end
  145.   #=============================================================================
  146.   def contains_starting_space
  147.     (self[0,1] == " ") ? (return true) : (return false)
  148.   end
  149.   #=============================================================================
  150.   def rot13
  151.     dup.unpack("C*").map{|i| (i + 13) % 256}.pack("C*")
  152.   end
  153.   #=============================================================================
  154.   def unrot13
  155.     dup.unpack("C*").map{|i| (i - 13) % 256}.pack("C*")
  156.   end
  157. end
  158. #===============================================================================
  159. class Scene_Title
  160.   alias book_script_load_database load_database
  161.   #=============================================================================
  162.   def load_database
  163.     book_script_load_database
  164.     if FileTest.exist?("Data/Books.rvdata")
  165.       $data_books         = load_data("Data/Books.rvdata")
  166.       $data_books.each{|book_name,book|
  167.         book.unobfuscate
  168.       }
  169.     end
  170.   end
  171. end
  172. #===============================================================================
  173. module TSDA
  174.   class Book
  175.     attr_reader            :name
  176.     attr_accessor            :contents
  177.     #===========================================================================
  178.     def initialize(name)
  179.       @name = name
  180.       @contents = []
  181.     end
  182.     #===========================================================================
  183.     def obfuscate
  184.       @name = @name.rot13
  185.       for i in 0...@contents.size
  186.         @contents[i] = @contents[i].rot13
  187.       end
  188.     end
  189.     #===========================================================================
  190.     def unobfuscate
  191.       @name = @name.unrot13
  192.       for i in 0...@contents.size
  193.         @contents[i] = @contents[i].unrot13
  194.       end
  195.     end
  196.   end
  197.   #=============================================================================
  198.   module_function
  199.   #=============================================================================
  200.   def make_books(list)
  201.     books = {}
  202.     for book_name in list
  203.       book = Book.new(book_name)
  204.       ary = eval("TSDA::" + book_name)
  205.       book.contents = ary
  206.       book.obfuscate
  207.       books[book_name] = book
  208.     end
  209.     save_data(books, "Data/Books.rvdata")
  210.   end
  211. end
  212. #===============================================================================
  213. class Scene_Book < Scene_Base
  214.   attr_reader       :book
  215.   include TSDA
  216.   #=============================================================================
  217.   def initialize(book_name)
  218.     @book = $data_books[book_name]
  219.     @pages = []
  220.     test_bitmap = Bitmap.new(512,384)
  221.     test_bitmap.font.name = FONT_NAME[book_name]
  222.     page = []
  223.     temp_string = ""
  224.     for l in @book.contents
  225.       copy = l.clone
  226.       copy.gsub!(/\\C\[([0-9]+)\]/i) { "\x01[#{$1}]" }
  227.       x = 0
  228.       while(letter = copy.slice!(/./m)) != nil
  229.         if page.size == 16
  230.           @pages.push(page)
  231.           page = []
  232.         end
  233.         if letter == "\x01"
  234.           temp_string.concat(letter)
  235.           copy.sub!(/\[([0-9]+)\]/, "")
  236.           temp_string.concat("[" + $1.to_i.to_s + "]")
  237.           next
  238.         end
  239.         l_width = test_bitmap.text_size(letter).width
  240.         temp_string.concat(letter)
  241.         x += l_width
  242.         if x >= 512
  243.           pos = temp_string.last_index_of(" ")
  244.           use_string = temp_string.substring(0,pos)
  245.           temp_string = temp_string.substring(pos,temp_string.size)
  246.           if temp_string.contains_starting_space
  247.             temp_string = temp_string.substring(1,temp_string.size)
  248.           end
  249.           page.push(use_string)
  250.           x = test_bitmap.text_size(temp_string).width
  251.         end
  252.       end
  253.       page.push(temp_string)
  254.       temp_string = ""
  255.     end
  256.     page.push(temp_string)
  257.     @pages.push(page)
  258.   end
  259.   #=============================================================================
  260.   def start
  261.     if USE_TITLE[@book.name]
  262.       bg = Sprite.new
  263.       bg.bitmap = Cache.picture(TITLE_IMAGE[@book.name])
  264.       while true
  265.         Graphics.update
  266.         Input.update
  267.         bg.update
  268.         if Input.trigger?(Input::C)
  269.           Sound.play_decision
  270.           break
  271.         end
  272.       end
  273.     end
  274.     create_pages
  275.     if USE_WINDOWSKIN[@book.name]
  276.       @book_window = Window_Base.new(0,0,544,416)
  277.       @book_window.contents = @bt_pages[0]
  278.       @page_index = 0
  279.     else
  280.       @page_index = 0
  281.       @bg = Sprite.new
  282.       @bg.bitmap = Cache.picture(BG_IMAGE[@book.name])
  283.       @page_sprite = Sprite.new
  284.       @page_sprite.z = 10
  285.       @page_sprite.x = 16
  286.       @page_sprite.y = 16
  287.       @page_sprite.bitmap = @bt_pages[@page_index]
  288.     end
  289.   end
  290.   #=============================================================================
  291.   def update
  292.     @book_window.update unless @book_window.nil?
  293.     @bg.update unless @bg.nil?
  294.     @page_sprite.update unless @page_sprite.nil?
  295.     if Input.trigger?(Input::RIGHT)
  296.       last_index = @page_index
  297.       @page_index += 1
  298.       if @page_index > @pages.size - 1
  299.         @page_index = @pages.size - 1
  300.       end
  301.       Audio.se_play("Audio/SE/" + PAGE_SOUND[@book.name]) if last_index != @page_index
  302.       wait(40)
  303.       if last_index != @page_index
  304.         if USE_WINDOWSKIN[@book.name]
  305.           @book_window.contents = @bt_pages[@page_index]
  306.         else
  307.           @page_sprite.bitmap = @bt_pages[@page_index]
  308.         end
  309.       end
  310.     elsif Input.trigger?(Input::LEFT)
  311.       last_index = @page_index
  312.       @page_index -= 1
  313.       if @page_index < 0
  314.         @page_index = 0
  315.       end
  316.       Audio.se_play("Audio/SE/" + PAGE_SOUND[@book.name]) if last_index != @page_index
  317.       wait(40)
  318.       if last_index != @page_index
  319.         if USE_WINDOWSKIN[@book.name]
  320.           @book_window.contents = @bt_pages[@page_index]
  321.         else
  322.           @page_sprite.bitmap = @bt_pages[@page_index]
  323.         end
  324.       end
  325.     elsif Input.trigger?(Input::B)
  326.       Sound.play_cancel
  327.       eval("$scene = " + SCENE_TO_GO)
  328.     end
  329.   end
  330.   #=============================================================================
  331.   def wait(value)
  332.     value.times{|n| Graphics.update}
  333.   end
  334.   #=============================================================================
  335.   def terminate
  336.     if USE_WINDOWSKIN[@book.name]
  337.       @book_window.dispose
  338.     else
  339.       @bg.dispose
  340.       @page_sprite.dispose
  341.     end
  342.   end
  343.   #=============================================================================
  344.   def create_pages
  345.     @bt_pages = []
  346.     for page in @pages
  347.       bt = Bitmap.new(512,384)
  348.       for i in 0...page.size
  349.         draw_item(i,page[i],bt)
  350.       end
  351.       @bt_pages.push(bt)
  352.     end
  353.   end
  354.   #=============================================================================
  355.   def draw_item(index,item,bt)
  356.     width = bt.text_size(item).width
  357.     if width > 380
  358.       draw_justified_text(0,index * 24,512,24,item,bt)
  359.     else
  360.       draw_text2(0,index * 24,512,24,item,bt)
  361.     end
  362.   end
  363.   #=============================================================================
  364.   def draw_justified_text(x,y,width,height,text,bt)
  365.     bt.font.name = FONT_NAME[@book.name]
  366.     bt.font.color = @font unless @font.nil?
  367.     use_x = x
  368.     copy = text.clone
  369.     tmp = ""
  370.     copy2 = text.clone
  371.     while ((letter = copy2.slice!(/./m)).nil? == false)
  372.       if letter == "\x01"
  373.         copy2.sub!(/\[([0-9]+)\]/, "")
  374.         next
  375.       end
  376.       tmp.concat(letter)
  377.     end
  378.     total_width = bt.text_size(tmp).width
  379.     plus = ((width.to_f - total_width.to_f - 5) / tmp.true_size)
  380.     while ((letter = copy.slice!(/./m)).nil? == false)
  381.       if letter == "\x01"
  382.         copy.sub!(/\[([0-9]+)\]/, "")
  383.         bt.font.color = text_color($1.to_i)
  384.         @font = bt.font.color
  385.         next
  386.       end
  387.       letter_width = bt.text_size(letter).width
  388.       bt.draw_text(use_x.to_i,y,letter_width,height,letter)
  389.       use_x += (letter_width + plus)
  390.     end
  391.   end
  392.   #=============================================================================
  393.   def text_color(n)
  394.     x = 64 + (n % 8) * 8
  395.     y = 96 + (n / 8) * 8
  396.     return Cache.system(WINDOWSKIN).get_pixel(x, y)
  397.   end
  398.   #=============================================================================
  399.   def draw_text2(x,y,width,height,text,bt)
  400.     use_x = x
  401.     copy = text.clone
  402.     while ((letter = copy.slice!(/./m)).nil? == false)
  403.       if letter == "\x01"
  404.         copy.sub!(/\[([0-9]+)\]/, "")
  405.         bt.font.color = text_color($1.to_i)
  406.         next
  407.       end
  408.       letter_width = bt.text_size(letter).width
  409.       bt.draw_text(use_x.to_i,y,letter_width,height,letter)
  410.       use_x += letter_width
  411.     end
  412.   end
  413. end
  414. #TSDA.make_books(TSDA::BOOKS)
Advertisement
Add Comment
Please, Sign In to add comment