def get_access_control(sym)
return :public if public_method_defined? sym
return :protected if protected_method_defined? sym
return :private if private_method_defined? sym
nil
end
template = lambda do |singleton|
if singleton
klass = 'singleton_class'
get_method = 'method'
define = 'define_singleton_method'
else
klass = 'self'
get_method = 'instance_method'
define = 'define_method'
end
%(
def <name>(sym, hook = nil, &b)
access = #{klass}.get_access_control sym
old = #{get_method} sym
if b
#{define} sym, &b
hook = #{get_method} sym
end
if hook.respond_to? :to_sym
hook = hook.to_sym
#{define} sym do |*args, &block|
<pattern_sym>
end
elsif hook.respond_to? :call
#{define} sym do |*args, &block|
<pattern_call>
end
elsif hook.kind_of? UnboundMethod
#{define} sym do |*args, &block|
<pattern_unbound>
end
end
#{klass}.__send__ access, sym
sym
end
)
end
# 备注模板
def note_any(name, default, re, capture)
name = name.to_s
mark = name.slice!(/[?!]\Z/)
re = re.source if re.kind_of?(Regexp)
re = "/<#{name.gsub('_', '\s*')}#{re}>/i"
default = default.inspect
class_eval %{
def #{name}
return @#{name} if instance_variable_defined? :@#{name}
@#{name} = #{note_method} =~ #{re} ? (#{capture}) : (#{default})
end
}, __FILE__, __LINE__
alias_method name + mark, name if mark
end
def note_i(name, default = 0)
note_any(name, default, '\s*(-?\d+)', '$1.to_i')
end
def note_f(name, default = 0.0)
note_any(name, default, '\s*(-?\d+(?:\.\d+)?)', '$1.to_f')
end
def note_s(name, default = '')
note_any(name, default, '\s*(\S.*)', '$1')
end
def note_bool(name)
note_any(name, false, '', 'true')
end
end
module Taroxd::DisposeBitmap
def dispose
bitmap.dispose if bitmap
super
end
end
# 定义管理精灵的方法
def self.extended(klass)
klass.class_eval do
sprites = nil
define_method CREATE_METHOD_NAME do
sprites = klass.sprite_list.map do |sprite_class, get_args|
if get_args
sprite_class.new(*instance_eval(&get_args))
else
sprite_class.new
end
end
end
define_method(UPDATE_METHOD_NAME) { sprites.each(&:update) }
define_method(DISPOSE_METHOD_NAME) { sprites.each(&:dispose) }
end
end
def use_sprite(klass, &get_args)
sprite_list.push [klass, get_args]
end
def sprite_list
@_taroxd_use_sprite ||= []
end
# 在一系列方法上触发钩子
def sprite_method_hook(name)
def_after :"create_#{name}", CREATE_METHOD_NAME
def_after :"update_#{name}", UPDATE_METHOD_NAME
def_after :"dispose_#{name}", DISPOSE_METHOD_NAME
end
end
class Fixnum < Integer
alias_method :id, :to_int
end
module Enumerable
def sum(base = 0)
block_given? ? inject(base) { |a, e| a + yield(e) } : inject(base, :+)
end
def pi(base = 1)
block_given? ? inject(base) { |a, e| a * yield(e) } : inject(base, :*)
end
end
class Game_BaseItem
attr_reader :item_id
alias_method :id, :item_id
end
class Game_BattlerBase
def initialized?
true
end
def mtp
max_tp
end
end
class Game_Battler < Game_BattlerBase
# 迭代拥有备注的对象
def note_objects
return to_enum(__method__) unless block_given?
states.each { |e| yield e }
equips.each { |e| yield e if e }
skills.each { |e| yield e }
yield data_object
yield self.class if actor?
end
def regenerate_tp
self.tp += max_tp * trg
end
def data_object
raise NotImplementedError
end
def note
data_object.note
end
def id
data_object.id
end
def skills
(basic_skills | added_skills).sort.map { |id| $data_skills[id] }
end