Project1

标题: 如何设置一个状态普攻耗SP? [打印本页]

作者: 爆焰    时间: 2019-2-6 05:23
标题: 如何设置一个状态普攻耗SP?
就是附加了这个状态之后普攻会耗自己定的SP量,当SP低于我设置的量之后改变战斗图或者减个状态什么的,请问该如何实现?
作者: KB.Driver    时间: 2019-2-6 14:06
RUBY 代码复制
  1. #==============================================================================
  2. # ■ 附加状态后 普通攻击消耗SP
  3. #------------------------------------------------------------------------------
  4. #  使用方法:在状态名字里写[SP消费 xxx]来使用
  5. #   例: 毒[SP消费 50]    => 每次普通攻击消耗50SP
  6. #        沉默[SP消费 50%] => 每次普通攻击消耗一半SP
  7. #   备注:有多个状态时,优先计算百分比扣除,再计算定值扣除。
  8. #         多个百分比扣除会取最大百分比。
  9. #==============================================================================
  10.  
  11. class RPG::State
  12.  
  13.   REG_SP_COST_STATE = /\[SP消费[ ]*(\d+%?)\]/i
  14.  
  15.   alias name_for_sp_cost_state name
  16.   def name
  17.     name_for_sp_cost_state.gsub(REG_SP_COST_STATE, '')
  18.   end
  19.  
  20.   def sp_cost_state?
  21.     name_for_sp_cost_state =~ REG_SP_COST_STATE
  22.   end
  23.  
  24.   def sp_cost_percentage?
  25.     (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.include?("%") : false
  26.   end
  27.  
  28.   def sp_cost
  29.     (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.gsub("%", '').to_i : 0
  30.   end
  31.  
  32. end
  33.  
  34. class Scene_Battle
  35.   #--------------------------------------------------------------------------
  36.   # ● 生成基本行动结果
  37.   #--------------------------------------------------------------------------
  38.   alias make_basic_action_result_for_sp_cost make_basic_action_result
  39.   def make_basic_action_result
  40.     # 攻击的情况下
  41.     if @active_battler.current_action.basic == 0
  42.       # 消耗 SP
  43.       sp_cost_states = @active_battler.states.map{|i|$data_states[i]}.select{|s|s.sp_cost_state?}
  44.       if !sp_cost_states.empty?
  45.         # 百分比消耗
  46.         sp_percentage_states = sp_cost_states.select{|s|s.sp_cost_percentage?}
  47.         if !sp_percentage_states.empty?
  48.           max_cost_percentage = sp_percentage_states.sort_by{|s|s.sp_cost}.first.sp_cost
  49.           max_cost_percentage = [max_cost_percentage.to_i, 100].min
  50.           @active_battler.sp = @active_battler.sp * (100 - max_cost_percentage) / 100
  51.         end
  52.         # 固定值消耗
  53.         sp_states = sp_cost_states - sp_percentage_states
  54.         if !sp_states.empty?
  55.           @active_battler.sp -= sp_states.collect{|s|s.sp_cost}.inject{|s,n|s+n}
  56.         end        
  57.       end
  58.     end
  59.     make_basic_action_result_for_sp_cost
  60.   end
  61. end


自己写了两个小时,新建工程测试通过。
Project1.zip (199.14 KB, 下载次数: 81)
作者: KB.Driver    时间: 2019-2-7 19:49
谢谢,就是这个效果。但是有没有办法实现SP不足以消耗时无法普通攻击?


更新了代码,增加了SP不足时无法普通攻击的效果。
RUBY 代码复制
  1. #==============================================================================
  2. # ■ 附加状态后 普通攻击消耗SP
  3. #------------------------------------------------------------------------------
  4. #  使用方法:在状态名字里写[SP消费 xxx]来使用
  5. #   例: 毒[SP消费 50]    => 每次普通攻击消耗50SP
  6. #        沉默[SP消费 50%] => 每次普通攻击消耗一半SP
  7. #   备注:有多个状态时,优先计算百分比扣除,再计算定值扣除。
  8. #         多个百分比扣除会取最大百分比。
  9. #==============================================================================
  10.  
  11. class RPG::State
  12.  
  13.   SP_COST_FORBID_ATTACK = true
  14.   # SP不足时,是否导致无法普通攻击
  15.  
  16.   REG_SP_COST_STATE = /\[SP消费[ ]*(\d+%?)\]/i
  17.   # 不要编辑这个
  18.  
  19.   alias name_for_sp_cost_state name
  20.   def name
  21.     name_for_sp_cost_state.gsub(REG_SP_COST_STATE, '')
  22.   end
  23.  
  24.   def sp_cost_state?
  25.     name_for_sp_cost_state =~ REG_SP_COST_STATE
  26.   end
  27.  
  28.   def sp_cost_percentage?
  29.     (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.include?("%") : false
  30.   end
  31.  
  32.   def sp_cost
  33.     (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.gsub("%", '').to_i : 0
  34.   end
  35.  
  36. end
  37.  
  38. module Sound
  39.   def self.play_buzzer
  40.     $game_system.se_play($data_system.buzzer_se)
  41.   end
  42. end
  43.  
  44. class Game_Actor
  45.   def attack_sp_enough?
  46.     return true if !RPG::State::SP_COST_FORBID_ATTACK
  47.     return true if !@states.map{|i|$data_states[i]}.any?{|s|s.sp_cost_state?}
  48.     @sp >= attack_sp_cost
  49.   end
  50.  
  51.   def attack_sp_cost
  52.     cost = 0
  53.     sp_cost_states = @states.map{|i|$data_states[i]}.select{|s|s.sp_cost_state?}
  54.     return cost if sp_cost_states.empty?
  55.     # 百分比消耗
  56.     sp_percentage_states = sp_cost_states.select{|s|s.sp_cost_percentage?}
  57.     if !sp_percentage_states.empty?
  58.       max_cost_percentage = sp_percentage_states.sort_by{|s|s.sp_cost}.first.sp_cost
  59.       max_cost_percentage = [max_cost_percentage.to_i, 100].min
  60.       cost += @sp * (100 - max_cost_percentage) / 100
  61.     end
  62.     # 固定值消耗
  63.     sp_states = sp_cost_states - sp_percentage_states
  64.     if !sp_states.empty?
  65.       cost += sp_states.collect{|s|s.sp_cost}.inject{|s,n|s+n}
  66.     end
  67.     return cost
  68.   end
  69. end
  70.  
  71. class Scene_Battle
  72.   #--------------------------------------------------------------------------
  73.   # ● 刷新画面
  74.   #--------------------------------------------------------------------------
  75.   alias cld99_update update
  76.   def update
  77.     if @actor_command_window && @active_battler && @active_battler.is_a?(Game_Actor)
  78.       if !@active_battler.attack_sp_enough?
  79.         @actor_command_window.disable_item(0)
  80.       else
  81.         @actor_command_window.refresh
  82.       end
  83.     end
  84.     cld99_update
  85.   end
  86.   #--------------------------------------------------------------------------
  87.   # ● 刷新画面 (角色命令回合 : 基本命令)
  88.   #--------------------------------------------------------------------------
  89.   alias cld99_update_phase3_basic_command update_phase3_basic_command
  90.   def update_phase3_basic_command
  91.     if Input.trigger?(Input::C)
  92.       if @actor_command_window.index == 0 #攻击
  93.         return Sound.play_buzzer if !@active_battler.attack_sp_enough?
  94.       end
  95.     end
  96.     cld99_update_phase3_basic_command
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # ● 生成基本行动结果
  100.   #--------------------------------------------------------------------------
  101.   alias make_basic_action_result_for_sp_cost make_basic_action_result
  102.   def make_basic_action_result
  103.     # 攻击的情况下
  104.     if @active_battler.current_action.basic == 0
  105.       @active_battler.sp -= @active_battler.attack_sp_cost if @active_battler.is_a?(Game_Actor)
  106.     end
  107.     make_basic_action_result_for_sp_cost
  108.   end
  109. end






欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1