Advertisement
thiago_d_d

Book System Ace

Jan 15th, 2013
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.32 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. #===============================================================================
  14. # Don't forget to configure the TSDA module!
  15. #===============================================================================
  16.  -----> To create new books
  17.     To create a new book, you have to type the text in this format, in the TSDA module:
  18.  
  19. BookName = [
  20. "paragraph1",
  21. "paragraph2",
  22. "paragraph3",
  23. ]
  24.  
  25. Don't put new lines in every paragraph, the script will break it.
  26. You can use only letters and underlines in the book name. This isn't a problem, as the
  27. book title will be shown as a image you have done in the Pictures folder.
  28.  
  29. After that, modify this line in the TSDA module:
  30. BOOKS = [blablabla]
  31. Put your book name in this list.
  32.  
  33.  
  34.  -----> Calling the book viewer
  35.  $book_name = "bookname"
  36.  SceneManager.call(Scene_Book)
  37.  
  38.  -----> Changing text color
  39.  Use this tag:
  40.  \\C[color number]
  41.  
  42.  -----> About the BG
  43.  Must be in the Pictures folder.
  44. #===============================================================================
  45. =end
  46. module TSDA
  47.   # This is the example book
  48.   Exemplo = [
  49.     "        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.",
  50.     "        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.",
  51.   ]
  52.   #Windowskin used to retrive the color used by the tag \\C
  53.   WINDOWSKIN = "Window"
  54.   #Books list
  55.   BOOKS = ["Exemplo"]
  56.   #Scene to transfer after reading the book
  57.   SCENE_TO_GO = Scene_Map
  58.   # Put here if the book will have a cover with the book title
  59.   # Format:
  60.   # {
  61.   #     "Livro" => true or false,
  62.   #     "Livro2" => true or false
  63.   # }
  64.   USE_TITLE = {
  65.     "Exemplo" => false
  66.   }
  67.   #Put here the picture used as a cover in the book, if the book use the cover
  68.   # Format:
  69.   # {
  70.   #     "Livro" => "IMg1",
  71.   #     "Livro2" => "IMg2"
  72.   # }
  73.   TITLE_IMAGE = {
  74.   }
  75.   # Put here if the book uses a BG or the windowskin
  76.   # Format:
  77.   # {
  78.   #     "Livro" => true or false,
  79.   #     "Livro2" => true or false
  80.   # }
  81.   USE_WINDOWSKIN = {
  82.     "Exemplo" => false
  83.   }
  84.   # Put there the BG name used by the book, in the Pictures folder.
  85.   # Format:
  86.   # {
  87.   #     "Livro" => "BG1",
  88.   #     "Livro2" => "GB2"
  89.   # }
  90.   BG_IMAGE = {
  91.     "Exemplo" => "ExemploBG"
  92.   }
  93.   # Put here the sound used when changing pages
  94.   # Format:
  95.   # {
  96.   #     "Livro" => "Sound1",
  97.   #     "Livro2" => "Sound1"
  98.   # }
  99.   PAGE_SOUND = {
  100.     "Exemplo" => "Book1"
  101.   }
  102.   # Put here the text font used by the book
  103.   # Format:
  104.   # {
  105.   #     "Livro" => "Trebuchet MS",
  106.   #     "Livro2" => "Arial"
  107.   # }
  108.   FONT_NAME = {
  109.     "Exemplo" => "Comic Sans MS"
  110.   }
  111. end
  112. #===============================================================================
  113. class String
  114.   def true_size
  115.     copy = self.clone;size = 0
  116.     while(copy.slice!(/./m)) != nil;size += 1;end;size
  117.   end
  118.   #=============================================================================
  119.   def starts_with?(string2)
  120.     if self[0,string2.size] == string2
  121.       $ind = string2.size;return true;else;return false;end
  122.   end
  123.   #=============================================================================
  124.   def substring(starting_index,final_index)
  125.     self[starting_index,final_index - starting_index]
  126.   end
  127.   #=============================================================================
  128.   def index_of(starting_index,st)
  129.     return -1 if starting_index > size or starting_index < 0
  130.     for i in starting_index...size
  131.       return i if self[i,1] == st;end;return -1
  132.   end
  133.   #=============================================================================
  134.   def last_index_of(st)
  135.     pos = -1
  136.     while (pos = index_of(pos + 1," ")) != -1
  137.       tpos = pos;end;return tpos
  138.   end
  139.   #=============================================================================
  140.   def contains_starting_space
  141.     (self[0,1] == " ") ? (return true) : (return false)
  142.   end
  143.   #=============================================================================
  144.   def rot13
  145.     dup.unpack("C*").map{|i| (i + 13) % 256}.pack("C*")
  146.   end
  147.   #=============================================================================
  148.   def unrot13
  149.     dup.unpack("C*").map{|i| (i - 13) % 256}.pack("C*")
  150.   end
  151. end
  152. #===============================================================================
  153. module DataManager
  154.   class << self
  155.     alias book_system_datamanager_create_game_objects create_game_objects
  156.     def create_game_objects
  157.       book_system_datamanager_create_game_objects
  158.       TSDA.make_books(TSDA::BOOKS)
  159.     end
  160.   end
  161. end
  162. #===============================================================================
  163. module TSDA
  164.   class Book
  165.     attr_reader            :name
  166.     attr_accessor            :contents
  167.     #===========================================================================
  168.     def initialize(name)
  169.       @name = name
  170.       @contents = []
  171.     end
  172.     #===========================================================================
  173.     def obfuscate
  174.       @name = @name.rot13
  175.       for i in 0...@contents.size
  176.         @contents[i] = @contents[i].rot13
  177.       end
  178.     end
  179.     #===========================================================================
  180.     def unobfuscate
  181.       @name = @name.unrot13
  182.       for i in 0...@contents.size
  183.         @contents[i] = @contents[i].unrot13
  184.       end
  185.     end
  186.   end
  187.   #=============================================================================
  188.   module_function
  189.   #=============================================================================
  190.   def make_books(list)
  191.     books = {}
  192.     for book_name in list
  193.       book = Book.new(book_name)
  194.       ary = eval("TSDA::" + book_name)
  195.       book.contents = ary
  196.       books[book_name] = book
  197.     end
  198.     $data_books = books
  199.   end
  200. end
  201. #===============================================================================
  202. class Scene_Book < Scene_Base
  203.   attr_reader       :book
  204.   include TSDA
  205.   #=============================================================================
  206.   def start
  207.     book_name = $book_name
  208.     @book = $data_books[book_name]
  209.     @pages = []
  210.     test_bitmap = Bitmap.new(512,384)
  211.     test_bitmap.font.name = FONT_NAME[book_name]
  212.     page = []
  213.     temp_string = ""
  214.     for l in @book.contents
  215.       copy = l.clone
  216.       copy.gsub!(/\\C\[([0-9]+)\]/i) { "\x01[#{$1}]" }
  217.       x = 0
  218.       while(letter = copy.slice!(/./m)) != nil
  219.         if page.size == 16
  220.           @pages.push(page)
  221.           page = []
  222.         end
  223.         if letter == "\x01"
  224.           temp_string.concat(letter)
  225.           copy.sub!(/\[([0-9]+)\]/, "")
  226.           temp_string.concat("[" + $1.to_i.to_s + "]")
  227.           next
  228.         end
  229.         l_width = test_bitmap.text_size(letter).width
  230.         temp_string.concat(letter)
  231.         x += l_width
  232.         if x >= 512
  233.           pos = temp_string.last_index_of(" ")
  234.           use_string = temp_string.substring(0,pos)
  235.           temp_string = temp_string.substring(pos,temp_string.size)
  236.           if temp_string.contains_starting_space
  237.             temp_string = temp_string.substring(1,temp_string.size)
  238.           end
  239.           page.push(use_string)
  240.           x = test_bitmap.text_size(temp_string).width
  241.         end
  242.       end
  243.       page.push(temp_string)
  244.       temp_string = ""
  245.     end
  246.     page.push(temp_string)
  247.     @pages.push(page)
  248.     if USE_TITLE[@book.name]
  249.       bg = Sprite.new
  250.       bg.bitmap = Cache.picture(TITLE_IMAGE[@book.name])
  251.       while true
  252.         Graphics.update
  253.         Input.update
  254.         bg.update
  255.         if Input.trigger?(:C)
  256.           Sound.play_decision
  257.           break
  258.         end
  259.       end
  260.     end
  261.     create_pages
  262.     if USE_WINDOWSKIN[@book.name]
  263.       @book_window = Window_Base.new(0,0,544,416)
  264.       @book_window.contents = @bt_pages[0]
  265.       @page_index = 0
  266.     else
  267.       @page_index = 0
  268.       @bg = Sprite.new
  269.       @bg.bitmap = Cache.picture(BG_IMAGE[@book.name])
  270.       @page_sprite = Sprite.new
  271.       @page_sprite.z = 10
  272.       @page_sprite.x = 16
  273.       @page_sprite.y = 16
  274.       @page_sprite.bitmap = @bt_pages[@page_index]
  275.     end
  276.   end
  277.   #=============================================================================
  278.   def update
  279.     super
  280.     @bg.update unless @bg.nil?
  281.     @page_sprite.update unless @page_sprite.nil?
  282.     if Input.trigger?(:RIGHT)
  283.       last_index = @page_index
  284.       @page_index += 1
  285.       if @page_index > @pages.size - 1
  286.         @page_index = @pages.size - 1
  287.       end
  288.       Audio.se_play("Audio/SE/" + PAGE_SOUND[@book.name]) if last_index != @page_index
  289.       wait(40)
  290.       if last_index != @page_index
  291.         if USE_WINDOWSKIN[@book.name]
  292.           @book_window.contents = @bt_pages[@page_index]
  293.         else
  294.           @page_sprite.bitmap = @bt_pages[@page_index]
  295.         end
  296.       end
  297.     elsif Input.trigger?(:LEFT)
  298.       last_index = @page_index
  299.       @page_index -= 1
  300.       if @page_index < 0
  301.         @page_index = 0
  302.       end
  303.       Audio.se_play("Audio/SE/" + PAGE_SOUND[@book.name]) if last_index != @page_index
  304.       wait(40)
  305.       if last_index != @page_index
  306.         if USE_WINDOWSKIN[@book.name]
  307.           @book_window.contents = @bt_pages[@page_index]
  308.         else
  309.           @page_sprite.bitmap = @bt_pages[@page_index]
  310.         end
  311.       end
  312.     elsif Input.trigger?(:B)
  313.       Sound.play_cancel
  314.       SceneManager.call(SCENE_TO_GO)
  315.     end
  316.   end
  317.   #=============================================================================
  318.   def wait(value)
  319.     value.times{|n| Graphics.update}
  320.   end
  321.   #=============================================================================
  322.   def terminate
  323.     if USE_WINDOWSKIN[@book.name]
  324.       @book_window.dispose
  325.     else
  326.       @bg.dispose
  327.       @page_sprite.dispose
  328.     end
  329.   end
  330.   #=============================================================================
  331.   def create_pages
  332.     @bt_pages = []
  333.     for page in @pages
  334.       bt = Bitmap.new(512,384)
  335.       for i in 0...page.size
  336.         draw_item(i,page[i],bt)
  337.       end
  338.       @bt_pages.push(bt)
  339.     end
  340.   end
  341.   #=============================================================================
  342.   def draw_item(index,item,bt)
  343.     width = bt.text_size(item).width
  344.     if width > 380
  345.       draw_justified_text(0,index * 24,512,24,item,bt)
  346.     else
  347.       draw_text2(0,index * 24,512,24,item,bt)
  348.     end
  349.   end
  350.   #=============================================================================
  351.   def draw_justified_text(x,y,width,height,text,bt)
  352.     bt.font.name = FONT_NAME[@book.name]
  353.     bt.font.color = @font unless @font.nil?
  354.     use_x = x
  355.     copy = text.clone
  356.     tmp = ""
  357.     copy2 = text.clone
  358.     while ((letter = copy2.slice!(/./m)).nil? == false)
  359.       if letter == "\x01"
  360.         copy2.sub!(/\[([0-9]+)\]/, "")
  361.         next
  362.       end
  363.       tmp.concat(letter)
  364.     end
  365.     total_width = bt.text_size(tmp).width
  366.     plus = ((width.to_f - total_width.to_f - 5) / tmp.true_size)
  367.     while ((letter = copy.slice!(/./m)).nil? == false)
  368.       if letter == "\x01"
  369.         copy.sub!(/\[([0-9]+)\]/, "")
  370.         bt.font.color = text_color($1.to_i)
  371.         @font = bt.font.color
  372.         next
  373.       end
  374.       letter_width = bt.text_size(letter).width
  375.       bt.draw_text(use_x.to_i,y,letter_width * 2,height,letter)
  376.       use_x += (letter_width + plus)
  377.     end
  378.   end
  379.   #=============================================================================
  380.   def text_color(n)
  381.     windowskin = Cache.system(WINDOWSKIN)
  382.     windowskin.get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
  383.   end
  384.   #=============================================================================
  385.   def draw_text2(x,y,width,height,text,bt)
  386.     use_x = x
  387.     copy = text.clone
  388.     while ((letter = copy.slice!(/./m)).nil? == false)
  389.       if letter == "\x01"
  390.         copy.sub!(/\[([0-9]+)\]/, "")
  391.         bt.font.color = text_color($1.to_i)
  392.         next
  393.       end
  394.       letter_width = bt.text_size(letter).width
  395.       bt.draw_text(use_x.to_i,y,letter_width * 2,height,letter)
  396.       use_x += letter_width
  397.     end
  398.   end
  399. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement