#==============================================================================
# ■ 地图式 可传递触发事件 字幕滚动 V1.0版 (By TERENCE龙皇)
#------------------------------------------------------------------------------
# 使用效果:
# (1) 可以配合地图上的事件或某些特效处理
# (2) 字幕滚动的效果永远会显示在最前面
# (3) 字幕滚动中,能侦测到事件的传递,藉此触发可以处理一些事情与特效
#
# 如何使用:
# (1) 字幕使用 UTF-8 格式的 TXT 档传写,请放在 Credit 文件夹之下
# (2) TXT 档名,请使用英文,才不会导致不同语言作业系统上读取失败
# (3) 其他部分,详见发布帖子,或研究范例工程
#==============================================================================
CREDIT_DETECTE_Y = 240 # 触发侦测线
CREDIT_NONEVISIBLE_HEIGHT = 25 # 字幕上下两侧不可视区域
#==============================================================================
# ■ Game_System
#------------------------------------------------------------------------------
# 处理系统附属资料的类别。(扩增字幕)
# 本类别的实例请参考 $game_system。
#==============================================================================
class Game_System
#--------------------------------------------------------------------------
# ● 定义实例变数
#--------------------------------------------------------------------------
attr_accessor :credit_obj # 字幕物件暂存
attr_accessor :credit_working # 字幕执行中的标志
attr_accessor :credit_filename # 读取字幕的TXT档名
#--------------------------------------------------------------------------
# ● 初始化物件
#--------------------------------------------------------------------------
alias original_initialize initialize
def initialize
@credit_working = false
@credit_filename = ""
original_initialize
end
#--------------------------------------------------------------------------
# ● 字幕启用
#--------------------------------------------------------------------------
def credit_start(filename)
@credit_working = true
@credit_filename = "Credit/#{filename}.txt"
end
#--------------------------------------------------------------------------
# ● 字幕取消
#--------------------------------------------------------------------------
def credit_canceled
if @credit_working
@credit_obj.y = -(@credit_obj.bitmap_hieght)
end
end
#--------------------------------------------------------------------------
# ● 字幕讯息事件是否被传递
#--------------------------------------------------------------------------
def credit_mesgpost?
# 若启动字幕滚动
if @credit_working
# 侦测字幕滚动是否有事件被触发
if @credit_obj.message_flag
@credit_obj.message_flag = false
return true
else; return false; end
else; return false; end
end
end
#==============================================================================
# ■ Sprite_Credit
#------------------------------------------------------------------------------
# 显示字幕滚动用的活动区块。监视 $game_system 丶活动区块状态
# 自动变化。
#==============================================================================
class Sprite_Credit < Sprite
#--------------------------------------------------------------------------
# ● 定义实例变数
#--------------------------------------------------------------------------
attr_accessor :message_flag # 讯息标志
attr_accessor :bitmap_hieght # 字幕高
#--------------------------------------------------------------------------
# ● 初始化物件
#--------------------------------------------------------------------------
def initialize(viewport)
super(viewport)
self.x = 0
self.y = 430
self.z = 0
@new_bitmap_flag = false
@line_counts = 0
@bitmap_hieght = 0
@message_line = []
@message_flag = false
update
end
#--------------------------------------------------------------------------
# ● 字幕 Bitmap 初始建立
#--------------------------------------------------------------------------
def create(filename)
unless FileTest.exist?(filename)
$game_system.credit_working = false
@new_bitmap_flag = false
@line_counts = 0
return
end
@bitmap_hieght = File.read(filename).count("\n")
@bitmap_hieght = 32*(1+@bitmap_hieght)
self.bitmap = Bitmap.new(640, @bitmap_hieght)
File.open(filename) do |file|
while line = file.gets
dump_lines(line)
end
end
end
#--------------------------------------------------------------------------
# ● TXT 文件 行处理
#--------------------------------------------------------------------------
def dump_lines(line)
yy = 32*@line_counts
if (line.gsub!(/\<PostMessage\>/){""})!=nil
@message_line.push(yy+self.y)
end
if @line_counts == 0
self.bitmap.draw_text(0, yy, 640, 32, line[3, line.size], 1)
else
self.bitmap.draw_text(0, yy, 640, 32, line, 1)
end
@line_counts += 1
end
#--------------------------------------------------------------------------
# ● 释放
#--------------------------------------------------------------------------
def dispose
if self.bitmap != nil
self.bitmap.dispose
end
super
end
#--------------------------------------------------------------------------
# ● 更新画面
#--------------------------------------------------------------------------
def update
super
# 设定计时器执行中为可见
self.visible = $game_system.credit_working
# 若启动字幕滚动
if $game_system.credit_working
unless @new_bitmap_flag
@new_bitmap_flag = true
create($game_system.credit_filename)
end
scroll_update
message_detected
end
end
#--------------------------------------------------------------------------
# ● 字幕滚动
#--------------------------------------------------------------------------
def scroll_update
if scroll_end?
$game_system.credit_working = false
@new_bitmap_flag = false
@message_flag = false
@message_line.clear
@line_counts = 0
self.y = 430
if self.bitmap != nil
self.bitmap.dispose
end
else
self.y -= 1
end
end
#--------------------------------------------------------------------------
# ● 字幕滚动结束 ?
#--------------------------------------------------------------------------
def scroll_end?
if self.y <= -(@bitmap_hieght)
return true
else
return false
end
end
#--------------------------------------------------------------------------
# ● 事件讯息祯测
#--------------------------------------------------------------------------
def message_detected
for i in 0...(@message_line.size)
if @message_line[i] == nil; break; end
@message_line[i] -= 1
if @message_line[i] <= CREDIT_DETECTE_Y-32 # 触发侦测线
@message_line.delete_at(i)
@message_flag = true
end
end
end
end
#==============================================================================
# ■ Spriteset_Map
#------------------------------------------------------------------------------
# 处理地图画面活动区块和元件的类别。 (扩增字幕)
# 本类别在 Scene_Map 类别的内部使用。
#==============================================================================
class Spriteset_Map
#--------------------------------------------------------------------------
# ● 初始化物件
#--------------------------------------------------------------------------
alias original_initialize initialize
def initialize
# 字幕尚未启动才 生成
unless $game_system.credit_working
hh = CREDIT_NONEVISIBLE_HEIGHT
credit_viewport = Viewport.new(0,hh,640,480-hh*2)
credit_viewport.z = 5000 + 200
$game_system.credit_obj = Sprite_Credit.new(credit_viewport)
end
original_initialize
end
#--------------------------------------------------------------------------
# ● 释放
#--------------------------------------------------------------------------
alias original_dispose dispose
def dispose
# 场景非地图 字幕滚动区块可视关闭
unless $scene.is_a?(Scene_Map)
$game_system.credit_obj.visible = false
end
# 字幕尚未启动才 释放字幕滚动区块
unless $game_system.credit_working
# 释放显示连接埠
$game_system.credit_obj.viewport.dispose
$game_system.credit_obj.dispose
end
original_dispose
end
#--------------------------------------------------------------------------
# ● 更新画面
#--------------------------------------------------------------------------
alias original_update update
def update
# 更新字幕滚动区块
$game_system.credit_obj.update
# 更新显示连接埠
$game_system.credit_obj.viewport.update
original_update
end
end