Project1
标题: 关于血槽的各种脚本 [打印本页]
作者: taroxd 时间: 2014-5-21 15:38
标题: 关于血槽的各种脚本
本帖最后由 taroxd 于 2015-2-13 21:15 编辑
#--------------------------------------------------------------------------
# ● require Taroxd基础设置
# 给值槽增加动态的滚动效果
#--------------------------------------------------------------------------
class Taroxd::Transition
# value: 当前值。changing: 当前是否正在变化
attr_reader :value, :changing
# get_target.call 获取到变化的数据。可以使用 block 代替 get_target。
def initialize(duration, get_target = nil, &block)
@duration = duration
@get_target = get_target || block
@value = @target = @get_target.call
@d = 0
end
# 更新值槽的值。如果值槽发生变化,返回 true。
def update
@target = @get_target.call
@changing = @value != @target
update_transition if @changing
@changing
end
private
def update_transition
@d = @duration if @d.zero?
@d -= 1
@value = if @d.zero?
@target
else
(@value * @d + @target).fdiv(@d + 1)
end
end
end
# include 之后,可用 @gauge_transitions[actor][:hp] 等
# 获取 Taroxd::Transition 的实例。
module Taroxd::RollGauge
Transition = Taroxd::Transition
def initialize(*)
@gauge_transitions = make_gauge_transitions
@gauge_roll_count = 0
super
end
def update
super
if (@gauge_roll_count += 1) >= gauge_roll_interval
roll_all_gauge if update_gauge_transitions
@gauge_roll_count = 0
end
end
def draw_actor_hp(actor, x, y, width = 124)
hp = @gauge_transitions[actor][:hp].value
rate = hp.fdiv(actor.mhp)
draw_gauge(x, y, width, rate, hp_gauge_color1, hp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::hp_a)
draw_current_and_max_values(x, y, width, hp.to_i, actor.mhp,
hp_color(actor), normal_color)
end
def draw_actor_mp(actor, x, y, width = 124)
mp = @gauge_transitions[actor][:mp].value
mmp = actor.mmp
rate = mmp.zero? ? 0 : mp.fdiv(actor.mmp)
draw_gauge(x, y, width, rate, mp_gauge_color1, mp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::mp_a)
draw_current_and_max_values(x, y, width, mp.to_i, actor.mmp,
mp_color(actor), normal_color)
end
def draw_actor_tp(actor, x, y, width = 124)
tp = @gauge_transitions[actor][:tp].value
rate = tp.fdiv(actor.max_tp)
draw_gauge(x, y, width, rate, tp_gauge_color1, tp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::tp_a)
change_color(tp_color(actor))
draw_text(x + width - 42, y, 42, line_height, tp.to_i, 2)
end
private
# 获取 make_gauge_transitions 生成的对象
attr_reader :gauge_transitions
# 值槽滚动所需的帧数
def gauge_roll_frame
30
end
# 每隔多少帧更新一次值槽
def gauge_roll_interval
1
end
# 生成值槽变化的数据。可在子类重定义。
# 默认的定义中,可以通过 gauge_transitions[actor][:hp] 等方式获取数据。
def make_gauge_transitions
Hash.new { |hash, actor|
hash[actor] = Hash.new do |h, k|
h[k] = Transition.new(gauge_roll_times, actor.method(k))
end
}.compare_by_identity
end
# 更新渐变的值。
# 返回真值则触发一次刷新。
# 每 gauge_roll_interval 帧调用一次。
def update_gauge_transitions
need_roll = false
gauge_transitions.each_value do |hash|
hash.each_value do |t|
need_roll = true if t.update
end
end
need_roll
end
# 值槽滚动所需的次数。
def gauge_roll_times
gauge_roll_frame / gauge_roll_interval
end
# 滚动所有值槽。可在子类重定义。
def roll_all_gauge
refresh
end
end
class Window_BattleStatus
include Taroxd::RollGauge
end
class Window_MenuStatus < Window_Selectable
include Taroxd::RollGauge
def roll_all_gauge
item_max.times do |i|
actor = $game_party.members[i]
rect = item_rect(i)
rect.x += 108
rect.y += line_height / 2
contents.clear_rect(rect)
draw_actor_simple_status(actor, rect.x, rect.y)
end
end
end
#--------------------------------------------------------------------------
# ● require Taroxd基础设置
# 给值槽增加动态的滚动效果
#--------------------------------------------------------------------------
class Taroxd::Transition
# value: 当前值。changing: 当前是否正在变化
attr_reader :value, :changing
# get_target.call 获取到变化的数据。可以使用 block 代替 get_target。
def initialize(duration, get_target = nil, &block)
@duration = duration
@get_target = get_target || block
@value = @target = @get_target.call
@d = 0
end
# 更新值槽的值。如果值槽发生变化,返回 true。
def update
@target = @get_target.call
@changing = @value != @target
update_transition if @changing
@changing
end
private
def update_transition
@d = @duration if @d.zero?
@d -= 1
@value = if @d.zero?
@target
else
(@value * @d + @target).fdiv(@d + 1)
end
end
end
# include 之后,可用 @gauge_transitions[actor][:hp] 等
# 获取 Taroxd::Transition 的实例。
module Taroxd::RollGauge
Transition = Taroxd::Transition
def initialize(*)
@gauge_transitions = make_gauge_transitions
@gauge_roll_count = 0
super
end
def update
super
if (@gauge_roll_count += 1) >= gauge_roll_interval
roll_all_gauge if update_gauge_transitions
@gauge_roll_count = 0
end
end
def draw_actor_hp(actor, x, y, width = 124)
hp = @gauge_transitions[actor][:hp].value
rate = hp.fdiv(actor.mhp)
draw_gauge(x, y, width, rate, hp_gauge_color1, hp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::hp_a)
draw_current_and_max_values(x, y, width, hp.to_i, actor.mhp,
hp_color(actor), normal_color)
end
def draw_actor_mp(actor, x, y, width = 124)
mp = @gauge_transitions[actor][:mp].value
mmp = actor.mmp
rate = mmp.zero? ? 0 : mp.fdiv(actor.mmp)
draw_gauge(x, y, width, rate, mp_gauge_color1, mp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::mp_a)
draw_current_and_max_values(x, y, width, mp.to_i, actor.mmp,
mp_color(actor), normal_color)
end
def draw_actor_tp(actor, x, y, width = 124)
tp = @gauge_transitions[actor][:tp].value
rate = tp.fdiv(actor.max_tp)
draw_gauge(x, y, width, rate, tp_gauge_color1, tp_gauge_color2)
change_color(system_color)
draw_text(x, y, 30, line_height, Vocab::tp_a)
change_color(tp_color(actor))
draw_text(x + width - 42, y, 42, line_height, tp.to_i, 2)
end
private
# 获取 make_gauge_transitions 生成的对象
attr_reader :gauge_transitions
# 值槽滚动所需的帧数
def gauge_roll_frame
30
end
# 每隔多少帧更新一次值槽
def gauge_roll_interval
1
end
# 生成值槽变化的数据。可在子类重定义。
# 默认的定义中,可以通过 gauge_transitions[actor][:hp] 等方式获取数据。
def make_gauge_transitions
Hash.new { |hash, actor|
hash[actor] = Hash.new do |h, k|
h[k] = Transition.new(gauge_roll_times, actor.method(k))
end
}.compare_by_identity
end
# 更新渐变的值。
# 返回真值则触发一次刷新。
# 每 gauge_roll_interval 帧调用一次。
def update_gauge_transitions
need_roll = false
gauge_transitions.each_value do |hash|
hash.each_value do |t|
need_roll = true if t.update
end
end
need_roll
end
# 值槽滚动所需的次数。
def gauge_roll_times
gauge_roll_frame / gauge_roll_interval
end
# 滚动所有值槽。可在子类重定义。
def roll_all_gauge
refresh
end
end
class Window_BattleStatus
include Taroxd::RollGauge
end
class Window_MenuStatus < Window_Selectable
include Taroxd::RollGauge
def roll_all_gauge
item_max.times do |i|
actor = $game_party.members[i]
rect = item_rect(i)
rect.x += 108
rect.y += line_height / 2
contents.clear_rect(rect)
draw_actor_simple_status(actor, rect.x, rect.y)
end
end
end
作者: taroxd 时间: 2014-5-21 15:42
本帖最后由 taroxd 于 2015-2-13 21:16 编辑
地图显示血条(超简易)
#--------------------------------------------------------------------------
# ● require Taroxd基础设置 动态值槽
# 在地图上显示一个简易的血条。
#--------------------------------------------------------------------------
class Sprite_MapHP < Sprite
Taroxd::MapHP = self
include Taroxd::DisposeBitmap
include Taroxd::RollGauge
# 颜色
HP_COLOR1 = Color.new(223, 127, 63)
HP_COLOR2 = Color.new(239, 191, 63)
BACK_COLOR = Color.new(31, 31, 63)
# 大小
WIDTH = 124
HEIGHT = 100
def initialize(_)
super
self.z = 170
self.bitmap = Bitmap.new(WIDTH, HEIGHT)
roll_all_gauge
end
def roll_all_gauge
bitmap.clear
$game_party.each_with_index do |actor, i|
rate = gauge_transitions[actor][:hp].value.fdiv(actor.mhp)
fill_w = (width * rate).to_i
gauge_y = i * 16 + 12
bitmap.fill_rect(fill_w, gauge_y, WIDTH - fill_w, 6, BACK_COLOR)
bitmap.gradient_fill_rect(0, gauge_y, fill_w, 6, HP_COLOR1, HP_COLOR2)
end
end
end
Spriteset_Map.use_sprite(Sprite_MapHP) { @viewport2 }
#--------------------------------------------------------------------------
# ● require Taroxd基础设置 动态值槽
# 在地图上显示一个简易的血条。
#--------------------------------------------------------------------------
class Sprite_MapHP < Sprite
Taroxd::MapHP = self
include Taroxd::DisposeBitmap
include Taroxd::RollGauge
# 颜色
HP_COLOR1 = Color.new(223, 127, 63)
HP_COLOR2 = Color.new(239, 191, 63)
BACK_COLOR = Color.new(31, 31, 63)
# 大小
WIDTH = 124
HEIGHT = 100
def initialize(_)
super
self.z = 170
self.bitmap = Bitmap.new(WIDTH, HEIGHT)
roll_all_gauge
end
def roll_all_gauge
bitmap.clear
$game_party.each_with_index do |actor, i|
rate = gauge_transitions[actor][:hp].value.fdiv(actor.mhp)
fill_w = (width * rate).to_i
gauge_y = i * 16 + 12
bitmap.fill_rect(fill_w, gauge_y, WIDTH - fill_w, 6, BACK_COLOR)
bitmap.gradient_fill_rect(0, gauge_y, fill_w, 6, HP_COLOR1, HP_COLOR2)
end
end
end
Spriteset_Map.use_sprite(Sprite_MapHP) { @viewport2 }
作者: taroxd 时间: 2014-5-21 15:42
本帖最后由 taroxd 于 2015-2-13 20:37 编辑
战斗中敌人显示血条(超简易)
#--------------------------------------------------------------------------
# ● require Taroxd基础设置,动态值槽
# 战斗中敌人显示血条
# 如不想显示,可在敌人处备注 <hide hp>
# 可在敌人处备注 <hp width w>、<hp height h>、<hp dxy dx dy> 调整血槽。
# 其中 w 表示宽度、h 表示高度,dx、dy 表示坐标的偏移。
#--------------------------------------------------------------------------
Taroxd::EnemyHP = true
class RPG::Enemy < RPG::BaseItem
note_any :hp_dxy, [0, 0], /\s+(-?\d+)\s+(-?\d+)/, '[$1.to_i, $2.to_i]'
note_i :hp_width, 80
note_i :hp_height, 6
note_bool :hide_hp?
# 初始化并获取战斗图的尺寸
def init_width_height
bitmap = Bitmap.new("Graphics/Battlers/#{@battler_name}")
@width = bitmap.width
@height = bitmap.height
bitmap.dispose
end
def width
return @width if @width
init_width_height
@width
end
def height
return @height if @height
init_width_height
@height
end
end
class Sprite_EnemyHP < Sprite
include Taroxd::RollGauge
include Taroxd::DisposeBitmap
HP_COLOR1 = Color.new(223, 127, 63)
HP_COLOR2 = Color.new(239, 191, 63)
BACK_COLOR = Color.new(31, 31, 63)
def initialize(viewport, enemy)
@enemy = enemy
super(viewport)
data = enemy.enemy
@width = data.hp_width
@height = data.hp_height
self.bitmap = Bitmap.new(@width, @height)
dx, dy = enemy.enemy.hp_dxy
self.ox = @width / 2
self.oy = @height
self.x = enemy.screen_x + dx
self.y = enemy.screen_y + dy
self.z = enemy.screen_z + 10
refresh
end
def make_gauge_transitions
Transition.new(gauge_roll_times) do
@enemy.hp.fdiv(@enemy.mhp)
end
end
def update_gauge_transitions
gauge_transitions.update
end
def refresh
bitmap.clear
rate = gauge_transitions.value
return if rate.zero?
fill_w = (bitmap.width * rate).to_i
bitmap.fill_rect(fill_w, 0, @width - fill_w, @height, BACK_COLOR)
bitmap.gradient_fill_rect(0, 0, fill_w, @height, HP_COLOR1, HP_COLOR2)
end
end
class Spriteset_Battle
# 导入精灵组
def_after :create_enemies do
@enemy_hp_sprites = $game_troop.members.map { |enemy|
Sprite_EnemyHP.new(@viewport1, enemy) unless enemy.enemy.hide_hp?
}.compact
end
def_after(:update_enemies) { @enemy_hp_sprites.each(&:update) }
def_after(:dispose_enemies) { @enemy_hp_sprites.each(&:dispose) }
end
#--------------------------------------------------------------------------
# ● require Taroxd基础设置,动态值槽
# 战斗中敌人显示血条
# 如不想显示,可在敌人处备注 <hide hp>
# 可在敌人处备注 <hp width w>、<hp height h>、<hp dxy dx dy> 调整血槽。
# 其中 w 表示宽度、h 表示高度,dx、dy 表示坐标的偏移。
#--------------------------------------------------------------------------
Taroxd::EnemyHP = true
class RPG::Enemy < RPG::BaseItem
note_any :hp_dxy, [0, 0], /\s+(-?\d+)\s+(-?\d+)/, '[$1.to_i, $2.to_i]'
note_i :hp_width, 80
note_i :hp_height, 6
note_bool :hide_hp?
# 初始化并获取战斗图的尺寸
def init_width_height
bitmap = Bitmap.new("Graphics/Battlers/#{@battler_name}")
@width = bitmap.width
@height = bitmap.height
bitmap.dispose
end
def width
return @width if @width
init_width_height
@width
end
def height
return @height if @height
init_width_height
@height
end
end
class Sprite_EnemyHP < Sprite
include Taroxd::RollGauge
include Taroxd::DisposeBitmap
HP_COLOR1 = Color.new(223, 127, 63)
HP_COLOR2 = Color.new(239, 191, 63)
BACK_COLOR = Color.new(31, 31, 63)
def initialize(viewport, enemy)
@enemy = enemy
super(viewport)
data = enemy.enemy
@width = data.hp_width
@height = data.hp_height
self.bitmap = Bitmap.new(@width, @height)
dx, dy = enemy.enemy.hp_dxy
self.ox = @width / 2
self.oy = @height
self.x = enemy.screen_x + dx
self.y = enemy.screen_y + dy
self.z = enemy.screen_z + 10
refresh
end
def make_gauge_transitions
Transition.new(gauge_roll_times) do
@enemy.hp.fdiv(@enemy.mhp)
end
end
def update_gauge_transitions
gauge_transitions.update
end
def refresh
bitmap.clear
rate = gauge_transitions.value
return if rate.zero?
fill_w = (bitmap.width * rate).to_i
bitmap.fill_rect(fill_w, 0, @width - fill_w, @height, BACK_COLOR)
bitmap.gradient_fill_rect(0, 0, fill_w, @height, HP_COLOR1, HP_COLOR2)
end
end
class Spriteset_Battle
# 导入精灵组
def_after :create_enemies do
@enemy_hp_sprites = $game_troop.members.map { |enemy|
Sprite_EnemyHP.new(@viewport1, enemy) unless enemy.enemy.hide_hp?
}.compact
end
def_after(:update_enemies) { @enemy_hp_sprites.each(&:update) }
def_after(:dispose_enemies) { @enemy_hp_sprites.each(&:dispose) }
end
欢迎光临 Project1 (https://rpg.blue/) |
Powered by Discuz! X3.1 |