Jump to content
Rpg²S Forum

*Book Scene v. 1.2


..::Dark::..
 Share

Recommended Posts

Book Scene

Descrizione


Permette al giocatore di leggere/vedere libri che ha raccolto durante il gioco, praticamente grazie ad un documento esterno il programmatore può decidere di far leggere ad esempio il libro che hanno trovato in biblioteca i pg configurando lo script


Autore


ForeverZer0
Blizzard per lo "slice_text"
Taiine per averlo richiesto

 

Allegati


Demo


Istruzioni per l'uso


Classe sopra main per copiaincollare lo script.

 

 

#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Book/Library Scene
# Author: ForeverZer0
# Version: 1.2
# Date: 9.1.2010
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#
# Version History:
# v. 1.0 9.1.2010
# - Original version
# v. 1.1 9.3.2010
# - Added option for full-screen text
# - Made the page number window slightly wider
# v. 1.2 9.25.2010
# - Added option to simply call scene with the book ID argument and have
# that book already displayed.
#
# Introduction:
# Calls a scene that will allow player to read through books that they have
# collected.
#
# Features:
# - Easy to configure, text is read from an external text document
# - Text will automatically fit itself into the proper size for the window
# width and pages, regardless of font or font size.
#
# Instructions:
# 1. Fill in the configuration below.
# 2. Create a new folder within the "Data" folder, and name it "Books".
# 3. Within this folder, create simple text documents (.txt).
# 4. Name them EXACTLY as you did for its respective title (below).
# 5. Turn word-wrap OFF.
# 6. Create as large as document as you would like, but only break to a new
# line in between paragraphs, not when the text reaches the edge of the
# window. Every time a new line is started, it guarantees that the current
# line will start on a new line in the scene, regardless of the font size.
#
# Use the following Script Calls:
# Books.unlock(BOOK_ID)
# - Unlocks the book with BOOK_ID
# $scene = Scene_Book.new
# - Calls the Book scene.
# $scene = Scene_Book.new(BOOK_ID)
# - Calls the book scene. The book with BOOK_ID will already be displayed.
# This will also disable the book list feature. The book does not have
# to be unlocked to call this way.
#
# Credits/Thanks:
# ForeverZer0, for the script
# Blizzard, for the "slice_text" method
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
module Books
	#===============================================================================
	# BEGIN CONFIGURATION
	#===============================================================================
	RETURN_SCENE = Scene_Map
	# Class of the scene that the game return to after you exit the book scene.
	SORT_BY_ID = true
	# If true, the books will be listed in order of their Book ID, else they will
	# be listed in the order they were aquired.
	PAGE_TURN_SE = ['046-Book01', 80, 100]
	# Set to the name of the SE that will be played when a player turns a page.
	# Set to nil to disable this feature. [FILENAME, VOLUME, PITCH]
	FULLSCREEN_TEXT = false
	# If true, the text window will extend over the book list and take up the
	# entire screen (minus the header).
	def self.title(book_id)
		# Here is where you define the labels used for the books. The titles will
		# be used as the filename to be loaded as well.
		# when BOOK_ID then 'TITLE'
		return case book_id
	when 0 then 'Genesis'
	when 1 then 'Exodus'
	when 2 then 'Leviticus'
	when 3 then 'Numbers'
	when 4 then 'Deuteronomy'
	end
end
#===============================================================================
# END CONFIGURATION
#===============================================================================
def self.unlock(book_id)
	# Adds the Book ID to the array.
	unless $game_system.books.include?(book_id)
		$game_system.books.push(book_id)
		# Sorts the array by the Book IDs if so configured.
		if SORT_BY_ID
			$game_system.books.sort!
		end
	end
end
def self.get_text(book_id)
	# Opens a text document, makes an array whose elements consist of the lines
	# of the document, and returns it.
	filename = 'Data/Books/' + title(book_id) + '.txt'
	begin
		file = File.open(filename, 'r')
		rescue
		raise("Error occured while attempting to open the following file:nn" +
		"t#{filename}nn" +
		"Please make sure that the file exist before you continue.")
	end
	book = []
	file.each_line {|line| book.push(line)}
	file.close
	return book
end
end
#===============================================================================
# ** Game_System
#===============================================================================
class Game_System
attr_accessor :books # Array: Stores the IDs of all unlocked books.
alias zer0_book_init initialize
def initialize
	zer0_book_init
	@books = []
end
end
#===============================================================================
# ** Scene_Book
#===============================================================================
class Scene_Book
def initialize(book_id = nil)
	@book_id = book_id
	@no_list = @book_id != nil
end
def main
	# Create a variable that is set to the window width (depending on config)
	@window_width = Books::FULLSCREEN_TEXT ? 640 : 512
	@window_width = 640 if @no_list
	# Create a blank dummy window.
	@dummy_window = Window_Base.new(128, 64, 512, 416)
	# Create Help_Window to display the book titles.
	@help_window = Window_Base.new(0, 0, 448, 64)
	@help_window.contents = Bitmap.new(416, 32)
	# Create the page number window
	@page_window = Window_Base.new(448, 0, 192, 64)
	@page_window.contents = Bitmap.new(160, 32)
	# Create the window that text will be displayed on.
	x = @window_width == 640 ? 0 : 128
	@text_window = Window_Base.new(x, 64, @window_width, 416)
	@text_window.contents = Bitmap.new(@window_width - 32, 384)
	@text_window.z = @help_window.z + 20
	@text_window.visible = false
	# Make a copy of the Books array.
	@books = $game_system.books.clone
	# Create instance variable for updating the text window.
	@update_text = false
	# Make list of all books, if possible, else make list empty.
	if @books.empty?
		help_string = 'No books are currently available.'
		@command_window = Window_Base.new(0, 64, 128, 416)
	else
		help_string = 'Whick book would you like to read?'
		@titles = []
		@books.each {|book_id| @titles.push(Books.title(book_id))}
		@command_window = Window_Command.new(128, @titles)
		@command_window.height, @command_window.y = 416, 64
	end
	# Display text on the help screen
	@help_window.contents.draw_text(0, 0, 416, 32, help_string, 1)
	# Create array that holds all the windows
	@windows = [@dummy_window, @help_window, @text_window,
	@command_window, @page_window]
	if @no_list
		@page_number = 1
		$game_system.se_play($data_system.decision_se)
		@help_window.contents.clear
		@help_window.contents.draw_text(0, 0, 416, 32, Books.title(@book_id), 1)
		@command_window.active, @update_text = false, true
		compile_text
		@text_window.visible = true
	end
	Graphics.transition
	# Main loop
	loop {Graphics.update; Input.update; update; break if $scene != self}
	Graphics.freeze
	@windows.each {|window| window.dispose}
end
def update
	# Update windows.
	@windows.each {|window| window.update}
	if @command_window.is_a?(Window_Command) && @command_window.active
		update_command_window
		return
	elsif @update_text
		update_text_window
		return
	end
	if Input.trigger?(Input::B)
		$game_system.se_play($data_system.cancel_se)
		$scene = Books::RETURN_SCENE.new
	end
end
def update_command_window
	if Input.trigger?(Input::B)
		$game_system.se_play($data_system.cancel_se)
		$scene = Books::RETURN_SCENE.new
	elsif Input.trigger?(Input::C)
		@page_number = 1
		$game_system.se_play($data_system.decision_se)
		@book_id = @books[@command_window.index]
		@help_window.contents.clear
		@help_window.contents.draw_text(0, 0, 416, 32, Books.title(@book_id), 1)
		@command_window.active, @update_text = false, true
		compile_text
		@text_window.visible = true
	end
end
def update_text_window
	if Input.trigger?(Input::LEFT) || Input.trigger?(Input::RIGHT)
		old_page = @page_number
		@page_number += Input.trigger?(Input::LEFT) ? -1 : 1
		@page_number = [[@page_number, @pages.size].min, 1].max
		# Play SE if configured as such.
		if old_page != @page_number && Books::PAGE_TURN_SE != nil
			se = Books::PAGE_TURN_SE
			audio = RPG::AudioFile.new(se[0], se[1], se[2])
			$game_system.se_play(audio)
		end
		# Refresh the window
		refresh_text
	elsif Input.trigger?(Input::B)
		$game_system.se_play($data_system.cancel_se)
		if @no_list
			$scene = Books::RETURN_SCENE.new
		else
			@update_text, @command_window.active = false, true
			@text_window.visible = false
		end
	end
end
def compile_text
	# Gets an array whose elements are the lines of the text document.
	text = Books.get_text(@book_id)
	# Iterate over each line of the text. Break each line up into segments that
	# will fit into the window, then add them all together.
	@pages, lines = [], []
	text.each {|paragraph|
	segment = @text_window.contents.slice_text(paragraph, @window_width - 32)
	segment.each {|line| lines.push(line)}
	}
	# Organizes the lines (12 per page) into pages.
	page_numbers = (lines.size / 12) + 1
	(0...page_numbers).each {|i| @pages[i] = lines[i*12, 12]}
	refresh_text
end
def refresh_text
	[@text_window.contents, @page_window.contents].each {|bitmap| bitmap.clear}
	# Displays the lines of the current page onto the window.
	page = @pages[@page_number - 1]
	page.each_index {|i|
	@text_window.contents.draw_text(0, i*32, @window_width - 32, 32, page[i])}
	# Set the proper text to the page screen.
	string = "Page #{@page_number}/#{@pages.size}"
	@page_window.contents.draw_text(0, 0, 160, 32, string, 1)
end
end
class Bitmap
# The slice_text method was created by Blizzard. I take no credit for it.
def slice_text(text, width)
	words = text.split(' ')
	return words if words.size == 1
	result, current_text = [], words.shift
	words.each_index {|i|
	if self.text_size("#{current_text} #{words[i]}").width > width
		result.push(current_text)
		current_text = words[i]
	else
		current_text = "#{current_text} #{words[i]}"
	end
	result.push(current_text) if i >= words.size - 1}
	return result
end
end

 



Bugs e Conflitti Noti

 

N/A


Altri Dettagli


Solo in inglese purtroppo D:

Edited by Dilos
Script monoriga sistemato.

Sono stanco e devo rinnovare la firma

-cut-

Link to comment
Share on other sites

  • 1 month later...

Lo riporto su perchè mi sarebbe stato molto utile... peccato che nel mio gioco non funzioni.

Mi dà il Type error alle righe 161-162

 

# Make a copy of the Books array.

@books = $game_system.books.clone

 

Ci deve essere qualche problema.

Gioco in Sviluppo:

http://www.studibizantini.it/docs/Logo.png

 

Blog: Ode to my Forthcoming Winter

Riferimento

 

Contest:

http://rpg2s.net/gif/SCContest2Oct.gifx2 http://rpg2s.net/gif/SCContest1Oct.gifx1

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...