设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 3130|回复: 2
打印 上一主题 下一主题

关于血槽的各种脚本

[复制链接]

…あたしは天使なんかじゃないわ

梦石
0
星屑
2207
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

跳转到指定楼层
1
发表于 2014-5-21 15:38:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 taroxd 于 2015-2-13 21:15 编辑

RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● require Taroxd基础设置
  3. #    给值槽增加动态的滚动效果
  4. #--------------------------------------------------------------------------
  5.  
  6. class Taroxd::Transition
  7.  
  8.   # value: 当前值。changing: 当前是否正在变化
  9.   attr_reader :value, :changing
  10.  
  11.   # get_target.call 获取到变化的数据。可以使用 block 代替 get_target。
  12.   def initialize(duration, get_target = nil, &block)
  13.     @duration = duration
  14.     @get_target = get_target || block
  15.     @value = @target = @get_target.call
  16.     @d = 0
  17.   end
  18.  
  19.   # 更新值槽的值。如果值槽发生变化,返回 true。
  20.   def update
  21.     @target = @get_target.call
  22.     @changing = @value != @target
  23.     update_transition if @changing
  24.     @changing
  25.   end
  26.  
  27.   private
  28.  
  29.   def update_transition
  30.     @d = @duration if @d.zero?
  31.     @d -= 1
  32.     @value = if @d.zero?
  33.       @target
  34.     else
  35.       (@value * @d + @target).fdiv(@d + 1)
  36.     end
  37.   end
  38. end
  39.  
  40. # include 之后,可用 @gauge_transitions[actor][:hp] 等
  41. # 获取 Taroxd::Transition 的实例。
  42. module Taroxd::RollGauge
  43.  
  44.   Transition = Taroxd::Transition
  45.  
  46.   def initialize(*)
  47.     @gauge_transitions = make_gauge_transitions
  48.     @gauge_roll_count = 0
  49.     super
  50.   end
  51.  
  52.   def update
  53.     super
  54.     if (@gauge_roll_count += 1) >= gauge_roll_interval
  55.       roll_all_gauge if update_gauge_transitions
  56.       @gauge_roll_count = 0
  57.     end
  58.   end
  59.  
  60.   def draw_actor_hp(actor, x, y, width = 124)
  61.     hp = @gauge_transitions[actor][:hp].value
  62.     rate = hp.fdiv(actor.mhp)
  63.     draw_gauge(x, y, width, rate, hp_gauge_color1, hp_gauge_color2)
  64.     change_color(system_color)
  65.     draw_text(x, y, 30, line_height, Vocab::hp_a)
  66.     draw_current_and_max_values(x, y, width, hp.to_i, actor.mhp,
  67.       hp_color(actor), normal_color)
  68.   end
  69.  
  70.   def draw_actor_mp(actor, x, y, width = 124)
  71.     mp = @gauge_transitions[actor][:mp].value
  72.     mmp = actor.mmp
  73.     rate = mmp.zero? ? 0 : mp.fdiv(actor.mmp)
  74.     draw_gauge(x, y, width, rate, mp_gauge_color1, mp_gauge_color2)
  75.     change_color(system_color)
  76.     draw_text(x, y, 30, line_height, Vocab::mp_a)
  77.     draw_current_and_max_values(x, y, width, mp.to_i, actor.mmp,
  78.       mp_color(actor), normal_color)
  79.   end
  80.  
  81.   def draw_actor_tp(actor, x, y, width = 124)
  82.     tp = @gauge_transitions[actor][:tp].value
  83.     rate = tp.fdiv(actor.max_tp)
  84.     draw_gauge(x, y, width, rate, tp_gauge_color1, tp_gauge_color2)
  85.     change_color(system_color)
  86.     draw_text(x, y, 30, line_height, Vocab::tp_a)
  87.     change_color(tp_color(actor))
  88.     draw_text(x + width - 42, y, 42, line_height, tp.to_i, 2)
  89.   end
  90.  
  91.   private
  92.  
  93.   # 获取 make_gauge_transitions 生成的对象
  94.   attr_reader :gauge_transitions
  95.  
  96.   # 值槽滚动所需的帧数
  97.   def gauge_roll_frame
  98.     30
  99.   end
  100.  
  101.   # 每隔多少帧更新一次值槽
  102.   def gauge_roll_interval
  103.     1
  104.   end
  105.  
  106.   # 生成值槽变化的数据。可在子类重定义。
  107.   # 默认的定义中,可以通过 gauge_transitions[actor][:hp] 等方式获取数据。
  108.   def make_gauge_transitions
  109.     Hash.new { |hash, actor|
  110.       hash[actor] = Hash.new do |h, k|
  111.         h[k] = Transition.new(gauge_roll_times, actor.method(k))
  112.       end
  113.     }.compare_by_identity
  114.   end
  115.  
  116.   # 更新渐变的值。
  117.   # 返回真值则触发一次刷新。
  118.   # 每 gauge_roll_interval 帧调用一次。
  119.   def update_gauge_transitions
  120.     need_roll = false
  121.     gauge_transitions.each_value do |hash|
  122.       hash.each_value do |t|
  123.         need_roll = true if t.update
  124.       end
  125.     end
  126.     need_roll
  127.   end
  128.  
  129.   # 值槽滚动所需的次数。
  130.   def gauge_roll_times
  131.     gauge_roll_frame / gauge_roll_interval
  132.   end
  133.  
  134.   # 滚动所有值槽。可在子类重定义。
  135.   def roll_all_gauge
  136.     refresh
  137.   end
  138. end
  139.  
  140. class Window_BattleStatus
  141.   include Taroxd::RollGauge
  142. end
  143.  
  144. class Window_MenuStatus < Window_Selectable
  145.  
  146.   include Taroxd::RollGauge
  147.  
  148.   def roll_all_gauge
  149.     item_max.times do |i|
  150.       actor = $game_party.members[i]
  151.       rect = item_rect(i)
  152.       rect.x += 108
  153.       rect.y += line_height / 2
  154.       contents.clear_rect(rect)
  155.       draw_actor_simple_status(actor, rect.x, rect.y)
  156.     end
  157.   end
  158. end
  

…あたしは天使なんかじゃないわ

梦石
0
星屑
2207
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

2
 楼主| 发表于 2014-5-21 15:42:00 | 只看该作者
本帖最后由 taroxd 于 2015-2-13 21:16 编辑

地图显示血条(超简易)
RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● require Taroxd基础设置 动态值槽
  3. #    在地图上显示一个简易的血条。
  4. #--------------------------------------------------------------------------
  5.  
  6. class Sprite_MapHP < Sprite
  7.  
  8.   Taroxd::MapHP = self
  9.  
  10.   include Taroxd::DisposeBitmap
  11.   include Taroxd::RollGauge
  12.  
  13.   # 颜色
  14.   HP_COLOR1 = Color.new(223, 127, 63)
  15.   HP_COLOR2 = Color.new(239, 191, 63)
  16.   BACK_COLOR = Color.new(31, 31, 63)
  17.  
  18.   # 大小
  19.   WIDTH = 124
  20.   HEIGHT = 100
  21.  
  22.   def initialize(_)
  23.     super
  24.     self.z = 170
  25.     self.bitmap = Bitmap.new(WIDTH, HEIGHT)
  26.     roll_all_gauge
  27.   end
  28.  
  29.   def roll_all_gauge
  30.     bitmap.clear
  31.     $game_party.each_with_index do |actor, i|
  32.       rate = gauge_transitions[actor][:hp].value.fdiv(actor.mhp)
  33.       fill_w = (width * rate).to_i
  34.       gauge_y = i * 16 + 12
  35.       bitmap.fill_rect(fill_w, gauge_y, WIDTH - fill_w, 6, BACK_COLOR)
  36.       bitmap.gradient_fill_rect(0, gauge_y, fill_w, 6, HP_COLOR1, HP_COLOR2)
  37.     end
  38.   end
  39. end
  40.  
  41. Spriteset_Map.use_sprite(Sprite_MapHP) { @viewport2 }
回复 支持 反对

使用道具 举报

…あたしは天使なんかじゃないわ

梦石
0
星屑
2207
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

3
 楼主| 发表于 2014-5-21 15:42:17 | 只看该作者
本帖最后由 taroxd 于 2015-2-13 20:37 编辑

战斗中敌人显示血条(超简易)
RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● require Taroxd基础设置,动态值槽
  3. #    战斗中敌人显示血条
  4. #    如不想显示,可在敌人处备注 <hide hp>
  5. #    可在敌人处备注 <hp width w>、<hp height h>、<hp dxy dx dy> 调整血槽。
  6. #    其中 w 表示宽度、h 表示高度,dx、dy 表示坐标的偏移。
  7. #--------------------------------------------------------------------------
  8.  
  9. Taroxd::EnemyHP = true
  10.  
  11. class RPG::Enemy < RPG::BaseItem
  12.  
  13.   note_any :hp_dxy, [0, 0], /\s+(-?\d+)\s+(-?\d+)/, '[$1.to_i, $2.to_i]'
  14.   note_i :hp_width, 80
  15.   note_i :hp_height, 6
  16.   note_bool :hide_hp?
  17.  
  18.   # 初始化并获取战斗图的尺寸
  19.   def init_width_height
  20.     bitmap = Bitmap.new("Graphics/Battlers/#{@battler_name}")
  21.     @width = bitmap.width
  22.     @height = bitmap.height
  23.     bitmap.dispose
  24.   end
  25.  
  26.   def width
  27.     return @width if @width
  28.     init_width_height
  29.     @width
  30.   end
  31.  
  32.   def height
  33.     return @height if @height
  34.     init_width_height
  35.     @height
  36.   end
  37. end
  38.  
  39. class Sprite_EnemyHP < Sprite
  40.  
  41.   include Taroxd::RollGauge
  42.   include Taroxd::DisposeBitmap
  43.  
  44.   HP_COLOR1 = Color.new(223, 127, 63)
  45.   HP_COLOR2 = Color.new(239, 191, 63)
  46.   BACK_COLOR = Color.new(31, 31, 63)
  47.  
  48.   def initialize(viewport, enemy)
  49.     @enemy = enemy
  50.     super(viewport)
  51.     data = enemy.enemy
  52.     @width = data.hp_width
  53.     @height = data.hp_height
  54.     self.bitmap = Bitmap.new(@width, @height)
  55.     dx, dy = enemy.enemy.hp_dxy
  56.     self.ox = @width / 2
  57.     self.oy = @height
  58.     self.x = enemy.screen_x + dx
  59.     self.y = enemy.screen_y + dy
  60.     self.z = enemy.screen_z + 10
  61.     refresh
  62.   end
  63.  
  64.   def make_gauge_transitions
  65.     Transition.new(gauge_roll_times) do
  66.       @enemy.hp.fdiv(@enemy.mhp)
  67.     end
  68.   end
  69.  
  70.   def update_gauge_transitions
  71.     gauge_transitions.update
  72.   end
  73.  
  74.   def refresh
  75.     bitmap.clear
  76.     rate = gauge_transitions.value
  77.     return if rate.zero?
  78.     fill_w = (bitmap.width * rate).to_i
  79.     bitmap.fill_rect(fill_w, 0, @width - fill_w, @height, BACK_COLOR)
  80.     bitmap.gradient_fill_rect(0, 0, fill_w, @height, HP_COLOR1, HP_COLOR2)
  81.   end
  82. end
  83.  
  84. class Spriteset_Battle
  85.  
  86.   # 导入精灵组
  87.   def_after :create_enemies do
  88.     @enemy_hp_sprites = $game_troop.members.map { |enemy|
  89.       Sprite_EnemyHP.new(@viewport1, enemy) unless enemy.enemy.hide_hp?
  90.     }.compact
  91.   end
  92.  
  93.   def_after(:update_enemies)  { @enemy_hp_sprites.each(&:update)  }
  94.   def_after(:dispose_enemies) { @enemy_hp_sprites.each(&:dispose) }
  95. end
     
回复 支持 反对

使用道具 举报

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-1 20:51

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表