﻿#==============================================================================
# ★ ExState_StepsRemove
#------------------------------------------------------------------------------
# 　走過特定的步數之後就會解除某個狀態。
#==============================================================================

# 使用步數解除的狀態指定用文字。
# 在「備註」中填入： 指定用文字列 + [平均步數] 
# 在走了近指定步數後就會自然解除。
# 範例： 平均 120 步解除的狀態 => *STEPS_REMOVE[120]
EXSTA_STEPSRMV_SIGNATURE = "*STEPS_REMOVE"

# 狀態解除的資訊視窗設定。
# [此狀態解除時的資訊] 的設定會在視窗中顯示
# 視窗的背景和顯示位置可以在此設定
# 背景設定：(0:普通 1:變暗 2:透明)、
# 顯示位置：(0:上 1:中 2:下) 
EXSTA_STEPSRMV_WINDOW = [0, 2]

#------------------------------------------------------------------------------

# 狀態附加時生成持續步數，步數增加時則減少持續步數，
# 接觸事件時判定是否解除狀態，持續步數為0時解除狀態。
# (考慮其他事件與地形利用等判定)

class Game_Battler
  alias _exsrstep_add_state add_state
  alias _exsrstep_remove_state remove_state
  #--------------------------------------------------------------------------
  # ● 付加狀態 (追加定義)
  #     state_id : ステート ID
  #--------------------------------------------------------------------------
  def add_state(state_id)
    _exsrstep_add_state(state_id)
    $game_party.make_state_steps_count(state_id)
  end
  #--------------------------------------------------------------------------
  # ● 解除狀態 (追加定義)
  #     state_id : ステート ID
  #--------------------------------------------------------------------------
  def remove_state(state_id)
    _exsrstep_remove_state(state_id)
    $game_party.clear_state_steps_count(state_id)
  end
end

class Game_Party
  alias _exsrstep_initialize initialize
  alias _exsrstep_on_player_walk on_player_walk
  #--------------------------------------------------------------------------
  # ●初始化物件 (追加定義)
  #--------------------------------------------------------------------------
  def initialize
    _exsrstep_initialize
    @state_steps_count = {} # 狀態持續步數 (雜湊表)
  end
  #--------------------------------------------------------------------------
  # ● 角色步數增加處理 (追加定義)
  #--------------------------------------------------------------------------
  def on_player_walk
    # 優先處理持續傷害與自動回復
    _exsrstep_on_player_walk
    for i in @state_steps_count.keys
      @state_steps_count[i] -= 1 if @state_steps_count[i] > 0
    end
  end
  #--------------------------------------------------------------------------
  # ☆ 獲取狀態持續步數
  #--------------------------------------------------------------------------
  def state_steps_count
    return @state_steps_count
  end
  #--------------------------------------------------------------------------
  # ☆ 生成狀態隨機步數
  #--------------------------------------------------------------------------
  def make_state_steps_count(state_id)
    state = $data_states[state_id]
    sig = EXSTA_STEPSRMV_SIGNATURE
    if state.note[/#{Regexp.quote sig}\[(\d+)\]/].to_a[0]
      n = $1.to_i
      @state_steps_count[state_id] = rand(n) + rand(n) + 1
    end
  end
  #--------------------------------------------------------------------------
  # ☆ 狀態持續步數歸零
  #--------------------------------------------------------------------------
  def clear_state_steps_count(state_id)
    @state_steps_count.delete(state_id)
  end
  #--------------------------------------------------------------------------
  # ☆ 使用步數解除狀態判定
  #--------------------------------------------------------------------------
  def check_steps_state
    for i in @state_steps_count.keys
      if @state_steps_count[i] == 0
        for actor in members
          actor.remove_state(i)
        end
        state = $data_states[i]
        next if state.message4.empty?
        $game_message.background = EXSTA_STEPSRMV_WINDOW[0]
        $game_message.position = EXSTA_STEPSRMV_WINDOW[1]
        $game_message.texts.push(name + state.message4)
      end
    end
  end
end

class Game_Player
  alias _exsrstep_check_touch_event check_touch_event
  #--------------------------------------------------------------------------
  # ● 判斷事件是否由接觸觸發（重疊） (追加定義)
  #--------------------------------------------------------------------------
  def check_touch_event
    # 優先判斷步數解除
    $game_party.check_steps_state
    _exsrstep_check_touch_event
  end
end

